Documentation ¶
Index ¶
- Constants
- func IsStatusHealthy(status string) bool
- func NewIDFactory() func() string
- func NewLogAccessHandler(reporter, preHTTP, postHTTP AccessReporter, next http.Handler) http.Handler
- type AccessEntry
- func (ae *AccessEntry) Duration() time.Duration
- func (ae *AccessEntry) Request() *http.Request
- func (ae *AccessEntry) RequestMethod() string
- func (ae *AccessEntry) RequestURI() string
- func (ae *AccessEntry) RouteName() string
- func (ae *AccessEntry) Size() int64
- func (ae *AccessEntry) StatusCode() int
- type AccessReporter
- type Context
- type CtxConstructor
- type HealthInfo
- type Healthchecker
- type Middleware
- type Response
- func (response *Response) Error(message string, code int) error
- func (response *Response) Forbidden() error
- func (response *Response) Json(result interface{}, code int) error
- func (response *Response) NoContent() error
- func (response *Response) PlainText(content string, code int) error
- func (response *Response) Redirect(location string, code int) error
- func (response *Response) Unauthorized(scheme string) error
- type Server
- func (s *Server) Close()
- func (s *Server) Closing() bool
- func (s *Server) ExitProcess()
- func (s *Server) ExtendAccessLogging()
- func (s *Server) Listen()
- func (s *Server) NewMiddlewareHandler(middlewares []Middleware) http.Handler
- func (s *Server) RegisterRoutes(mux *http.ServeMux, prefix string)
- func (s *Server) Serve(method, urlPath string, middlewares ...Middleware)
- func (s *Server) ServeNotFound(middlewares ...Middleware)
- func (s *Server) ServeStatic(urlPath, fsPath string)
- func (s *Server) SetAppContext(ctxConstructor CtxConstructor)
- func (s *Server) SetCloseListenerDelay(d int)
- func (s *Server) SetLogColor(color bool)
- func (s *Server) SetLogLevel(level string)
- func (s *Server) SetLogger(logger requestcontext.Logger)
- func (s *Server) SetOsExitCode(c int)
- func (s *Server) SetOsExitDelay(d int)
- func (s *Server) SetPostHTTPHandler(reporter AccessReporter)
- func (s *Server) SetPreHTTPHandler(reporter AccessReporter)
Constants ¶
const ( StatusHealthy = "healthy" StatusUnhealthy = "unhealthy" )
const ( DefaultCloseListenerDelay = 0 DefaultOsExitDelay = 5 DefaultOsExitCode = 0 RequestIDKey = "request-id" RequestIDHeader = "X-Request-ID" )
Variables ¶
This section is empty.
Functions ¶
func IsStatusHealthy ¶
func NewIDFactory ¶
func NewIDFactory() func() string
func NewLogAccessHandler ¶
func NewLogAccessHandler(reporter, preHTTP, postHTTP AccessReporter, next http.Handler) http.Handler
NewLogAccessHandler executes the next handler and logs the requests statistics afterwards to the logger.
Types ¶
type AccessEntry ¶
type AccessEntry struct {
// contains filtered or unexported fields
}
func (*AccessEntry) Duration ¶
func (ae *AccessEntry) Duration() time.Duration
func (*AccessEntry) Request ¶
func (ae *AccessEntry) Request() *http.Request
func (*AccessEntry) RequestMethod ¶
func (ae *AccessEntry) RequestMethod() string
func (*AccessEntry) RequestURI ¶
func (ae *AccessEntry) RequestURI() string
func (*AccessEntry) RouteName ¶
func (ae *AccessEntry) RouteName() string
func (*AccessEntry) Size ¶
func (ae *AccessEntry) Size() int64
func (*AccessEntry) StatusCode ¶
func (ae *AccessEntry) StatusCode() int
type AccessReporter ¶
type AccessReporter func(entry *AccessEntry)
func DefaultAccessReporter ¶
func DefaultAccessReporter(ctx requestcontext.Ctx, logger requestcontext.Logger) AccessReporter
func ExtendedAccessReporter ¶
func ExtendedAccessReporter(ctx requestcontext.Ctx, logger requestcontext.Logger) AccessReporter
ExtendedAccessReporter createsan access logger that logs everything that DefaultAccessReporter does with the User-Agent added to that
type Context ¶
type Context struct { // Contains all placeholders from the route. MuxVars map[string]string // Helper to quickly write results to the `http.ResponseWriter`. Response Response // A middleware should call Next() to signal that no problem was encountered // and the next middleware in the chain can be executed after this middleware // finished. Always returns `nil`, so it can be convieniently used with // return to quit the middleware. Next func() error // The app context for this request. Gets prefilled by the // CtxConstructor, if set in the server. App interface{} Request requestcontext.Ctx }
Context is a map getting through all middlewares.
func (*Context) RequestID ¶
RequestID returns ID for the current request. If none is found, an empty string is returned.
func (*Context) SetRequestID ¶
SetRequestID overwrites the request ID of the current request with the given ID.
type CtxConstructor ¶
type CtxConstructor func() interface{}
type HealthInfo ¶
type HealthInfo struct { Status string `json:"status"` App string `json:"app"` Version string `json:"version"` Backends []HealthInfo `json:"backends"` }
type Healthchecker ¶
type Healthchecker func() (HealthInfo, error)
func (Healthchecker) Status ¶
func (hc Healthchecker) Status() (HealthInfo, error)
Status just accumulates the backends status to calculate the main status.
type Middleware ¶
Middleware is a http handler method.
func NewHealthcheckMiddleware ¶
func NewHealthcheckMiddleware(hc Healthchecker) Middleware
NewHealthcheckMiddleware provides a middleware that responds JSON formatted information about a service. E.g. one can register this under /healthcheck.
func NewWelcomeMiddleware ¶
func NewWelcomeMiddleware(appName, version string) Middleware
NewWelcomeMiddleware provides a middleware that responds human readable information about a service. E.g. one can register this under /.
type Response ¶
type Response struct {
// contains filtered or unexported fields
}
func (*Response) Forbidden ¶
Forbidden sends the http.StatusForbidden status. Use it to signal that the requestee has no access to the given resource (but auth itself worked).
func (*Response) Unauthorized ¶
Unauthorized sends the http.StatusUnauthorized status code. Use this to signal the requestee that the authentication failed.
type Server ¶
type Server struct { Logger requestcontext.Logger Router *mux.Router IDFactory func() string // contains filtered or unexported fields }
func (*Server) ExitProcess ¶
func (s *Server) ExitProcess()
func (*Server) ExtendAccessLogging ¶
func (s *Server) ExtendAccessLogging()
ExtendAccessLogging turns on the usage of ExtendedAccessLogger
func (*Server) NewMiddlewareHandler ¶
func (s *Server) NewMiddlewareHandler(middlewares []Middleware) http.Handler
NewMiddlewareHandler wraps the middlewares in a http.Handler. The handler, on activation, calls each middleware in order, if no error was returned and `ctx.Next()` was called. If a middleware wants to finish the processing, it can just write to the `http.ResponseWriter` or use the `ctx.Response` for convienience.
func (*Server) Serve ¶
func (s *Server) Serve(method, urlPath string, middlewares ...Middleware)
func (*Server) ServeNotFound ¶
func (s *Server) ServeNotFound(middlewares ...Middleware)
func (*Server) ServeStatic ¶
ServeStatis registers a middleware that serves files from the filesystem. Example: s.ServeStatic("/v1/public", "./public_html/v1/")
func (*Server) SetAppContext ¶
func (s *Server) SetAppContext(ctxConstructor CtxConstructor)
SetAppContext sets the CtxConstructor object, that is called for every request to provide the initial `Context.App` value, which is available to every middleware.
func (*Server) SetCloseListenerDelay ¶
SetCloseListenerDelay sets the time to delay closing the TCP listener when calling `s.Close()`.
func (*Server) SetLogColor ¶
func (*Server) SetLogLevel ¶
func (*Server) SetLogger ¶
func (s *Server) SetLogger(logger requestcontext.Logger)
SetLogger sets the logger object to which the server logs every request.
func (*Server) SetOsExitCode ¶
SetOsExitCode sets the exit code used in `os.Exit(c)` when calling `s.Close()`.
func (*Server) SetOsExitDelay ¶
SetOsExitDelay sets the time to delay exiting the process when calling `s.Close()`.
func (*Server) SetPostHTTPHandler ¶
func (s *Server) SetPostHTTPHandler(reporter AccessReporter)
func (*Server) SetPreHTTPHandler ¶
func (s *Server) SetPreHTTPHandler(reporter AccessReporter)