http

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2019 License: MIT Imports: 14 Imported by: 2

Documentation

Overview

Package http wraps net/http

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDefaultClient

func NewDefaultClient() *http.Client

NewDefaultClient returns a client using NewDefaultTransport

func NewDefaultTransport

func NewDefaultTransport() *http.Transport

func NewLoggingHandler

func NewLoggingHandler(h http.Handler, log *dlog.Logger) http.Handler

Types

type JsonFunc

type JsonFunc func(ctx context.Context, req interface{}) (res interface{}, err error)

type JsonHandler

type JsonHandler interface {
	HasPayload() bool
	NewPayload() interface{}
	Func() JsonFunc
}

type JsonHandlerMux

type JsonHandlerMux struct {
	// contains filtered or unexported fields
}

func NewJsonHandlerMux

func NewJsonHandlerMux() *JsonHandlerMux

func (*JsonHandlerMux) AddHandlerFunc

func (m *JsonHandlerMux) AddHandlerFunc(path string, payloadFactory func() interface{}, f JsonFunc)

func (*JsonHandlerMux) MountToStd

func (m *JsonHandlerMux) MountToStd(mux *http.ServeMux)

type JsonHandlerRegister

type JsonHandlerRegister func(mux *JsonHandlerMux)

type Server

type Server struct {
	// contains filtered or unexported fields
}

func NewServer

func NewServer(cfg config.HttpServerConfig, h http.Handler, tracer opentracing.Tracer) (*Server, error)

TODO: check if there is any error in config and return error

func (*Server) GetLogger

func (srv *Server) GetLogger() *dlog.Logger

func (*Server) LoggerIdentity

func (srv *Server) LoggerIdentity(justCallMe func() dlog.Identity) dlog.Identity

func (*Server) Port

func (srv *Server) Port() int

func (*Server) Run

func (srv *Server) Run() error

func (*Server) RunWithContext

func (srv *Server) RunWithContext(ctx context.Context) error

func (*Server) ServeHTTP

func (srv *Server) ServeHTTP(w http.ResponseWriter, r *http.Request)

TODO: there could be more than just logging handler, panic, cors etc. TODO: http log might need special logger, we are using struct's logger for now...

func (*Server) SetLogger

func (srv *Server) SetLogger(logger *dlog.Logger)

func (*Server) Shutdown

func (srv *Server) Shutdown(ctx context.Context) error

type TrackedWriter

type TrackedWriter struct {
	// contains filtered or unexported fields
}

TrackedWriter keeps track of status code and bytes written so it can be used by logger. It proxies all the interfaces except Hijacker, since it is not supported by HTTP/2. Most methods comments are copied from net/http It is based on https://github.com/gorilla/handlers but put all interface on one struct

func (*TrackedWriter) CloseNotify

func (tracker *TrackedWriter) CloseNotify() <-chan bool

CloseNotify returns nil if underlying ResponseWriter does not implement http.CloseNotifier

The CloseNotifier interface is implemented by ResponseWriters which allow detecting when the underlying connection has gone away

func (*TrackedWriter) Flush

func (tracker *TrackedWriter) Flush()

Flush calls Flush on underlying ResponseWriter if it implemented http.Flusher

Flusher interface is implemented by ResponseWriters that allow an HTTP handler to flush buffered data to the client. The default HTTP/1.x and HTTP/2 ResponseWriter implementations support Flusher

func (*TrackedWriter) Header

func (tracker *TrackedWriter) Header() http.Header

Header returns the header map of the underlying ResponseWriter

Changing the header map after a call to WriteHeader (or Write) has no effect unless the modified headers are trailers.

func (*TrackedWriter) Push

func (tracker *TrackedWriter) Push(target string, opts *http.PushOptions) error

Push returns http.ErrNotSupported if underlying ResponseWriter does not implement http.Pusher

Push initiates an HTTP/2 server push, returns ErrNotSupported if the client has disabled push or if push is not supported on the underlying connection.

func (*TrackedWriter) Size

func (tracker *TrackedWriter) Size() int

Size return number of bytes written through Write, returns 0 if Write has not been called

func (*TrackedWriter) Status

func (tracker *TrackedWriter) Status() int

Status return the tracked status code, returns 0 if WriteHeader has not been called

func (*TrackedWriter) Write

func (tracker *TrackedWriter) Write(b []byte) (int, error)

Write keeps track of bytes written of the underlying ResponseWriter

Write writes the data to the connection as part of an HTTP reply.

If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK) before writing the data. If the Header does not contain a Content-Type line, Write adds a Content-Type set to the result of passing the initial 512 bytes of written data to DetectContentType.

func (*TrackedWriter) WriteHeader

func (tracker *TrackedWriter) WriteHeader(status int)

WriteHeader keep track of status code and call the underlying ResponseWriter

WriteHeader sends an HTTP response header with status code. If WriteHeader is not called explicitly, the first call to Write will trigger an implicit WriteHeader(http.StatusOK). Thus explicit calls to WriteHeader are mainly used to send error codes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL