Documentation ¶
Overview ¶
Package handler implements Go HTTP handlers using context.
Index ¶
- func AddMiddleware(middleware Middleware)
- func ClearMiddlewares()
- func ClientErrorMiddleware(ctx context.Context, w http.ResponseWriter, r *http.Request, ...) error
- func Ctx(fn CtxHandler) http.HandlerFunc
- func LoggerMiddleware(ctx context.Context, w http.ResponseWriter, r *http.Request, ...) error
- func ReadJSON(r *http.Request, data interface{}) error
- func ServeGracefully(defaultPort int64, stopTimeout time.Duration)
- func WriteJSON(w http.ResponseWriter, data interface{}) error
- type AppResponseWriter
- type CtxHandler
- type Middleware
- type NextMiddlewareFn
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AddMiddleware ¶
func AddMiddleware(middleware Middleware)
AddMiddleware to the list of middlewares.
func ClearMiddlewares ¶
func ClearMiddlewares()
ClearMiddlewares cleans the list of applied middlewares to let the app choose the order and what ones get activated with the requests.
func ClientErrorMiddleware ¶
func ClientErrorMiddleware(ctx context.Context, w http.ResponseWriter, r *http.Request, next NextMiddlewareFn) error
ClientErrorMiddleware rejects the request with a 500 status code when an error occurs.
func Ctx ¶
func Ctx(fn CtxHandler) http.HandlerFunc
Ctx adapts a context handler to the standard HTTP lib contract.
func LoggerMiddleware ¶
func LoggerMiddleware(ctx context.Context, w http.ResponseWriter, r *http.Request, next NextMiddlewareFn) error
LoggerMiddleware logs to stderr the error.
func ServeGracefully ¶
ServeGracefully creates a new listener that will close gracefully without dropping client connections. If a PORT environment variable is defined it will use that port instead of the default one. If debugging is enabled a normal listener will be used instead to close it quicker in the development cycle.
func WriteJSON ¶
func WriteJSON(w http.ResponseWriter, data interface{}) error
WriteJSON to the response using the correct Content-Type header.
Types ¶
type AppResponseWriter ¶
type AppResponseWriter struct {
// contains filtered or unexported fields
}
AppResponseWriter the package uses to cache the responses. Exposed to be able to build or own middlewares.
func (*AppResponseWriter) Header ¶
func (w *AppResponseWriter) Header() http.Header
Header will call the same function in the standard response writer.
func (*AppResponseWriter) Reset ¶
func (w *AppResponseWriter) Reset()
Reset removes any content in the memory buffer for the response.
func (*AppResponseWriter) Write ¶
func (w *AppResponseWriter) Write(data []byte) (n int, err error)
Write will call the same function in the standard response writer.
func (*AppResponseWriter) WriteHeader ¶
func (w *AppResponseWriter) WriteHeader(statusCode int)
WriteHeader will call the same function in the standard response writer.
type CtxHandler ¶
CtxHandler should be implemented by the handlers.
type Middleware ¶
type Middleware func(ctx context.Context, w http.ResponseWriter, r *http.Request, next NextMiddlewareFn) error
Middleware should be implemented by the functions that want to intercept requests or responses.
type NextMiddlewareFn ¶
NextMiddlewareFn should be called after the processing in the middleware finishes to follow the chain. An instance of this function type will be passed to each middleware call.