Documentation
¶
Index ¶
- Constants
- Variables
- func AccessLogHandler(next http.Handler) http.Handler
- func AccessMetricsHandler(next http.Handler, registration *PrometheusRegistration) http.Handler
- func AddGracefulShutdown(ctx context.Context, wg *sync.WaitGroup, shutdowner Shutdowner, ...)
- func CspHeaderHandler(next http.Handler, variableName string) http.Handler
- func FallbackHandler(next http.Handler, fallbackPath string, fallbackCodes ...int) http.Handler
- func HealthCheckConditionalHandler(condition func() bool) http.Handler
- func HealthCheckHandler() http.Handler
- func NewCacheHandler(next http.Handler) *cacheHandler
- func SessionCookieHandler(next http.Handler, cookieName string, cookieTimeToLife time.Duration) http.Handler
- func ShutdownAfterWaitGroup(ctx context.Context, wg *sync.WaitGroup, server *http.Server, ...) error
- func SigTermCtx(ctx context.Context, cancelDelay time.Duration) context.Context
- func ValidateHandler(next http.Handler) http.Handler
- type AngularCspReplaceConfig
- type Config
- type ContextKey
- type CspFileHandler
- type HandlerMiddleware
- func AccessLog() HandlerMiddleware
- func AccessMetrics(registration *PrometheusRegistration) HandlerMiddleware
- func Caching() HandlerMiddleware
- func CspFileReplace(variableName string, mediaTypeMap map[string]string) HandlerMiddleware
- func CspHeaderReplace(variableName string) HandlerMiddleware
- func Fallback(fallbackPath string, fallbackCodes ...int) HandlerMiddleware
- func H2C(h2cPort uint16) HandlerMiddleware
- func Header(headers map[string]string) HandlerMiddleware
- func Optional(middleware HandlerMiddleware, isActive bool) HandlerMiddleware
- func SessionId(cookieName string, cookieMaxAge time.Duration) HandlerMiddleware
- func Validate() HandlerMiddleware
- type HeaderHandler
- type PrometheusRegistration
- type ReplacerCollection
- type Server
- type Shutdowner
Constants ¶
const CspHeaderName = "Content-Security-Policy"
CspHeaderName is the Content-Security-Policy HTTP-Header name
Variables ¶
var ( ErrHttpStatusNotOk = errors.New("non HTTP 200 status code from underlying handler") ErrCachingTemplate = errors.New("error caching initial file for csp replacement") )
var DomainLabel = "domain"
var ServerName = &ContextKey{val: "serverName"}
var SessionIdKey = &ContextKey{val: "sessionId"}
SessionIdKey is the ContextKey under which the current sessionId can be found
var StatusLabel = "status"
Functions ¶
func AccessLogHandler ¶
AccessLogHandler returns a http.Handler that adds access-logging on the info level.
func AccessMetricsHandler ¶
func AccessMetricsHandler(next http.Handler, registration *PrometheusRegistration) http.Handler
AccessMetricsHandler collects the bytes send out as well as the status codes as prometheus metrics and writes them to the registry. The registerer has to be prepared via the AccessMetricsRegister function.
func AddGracefulShutdown ¶
func AddGracefulShutdown(ctx context.Context, wg *sync.WaitGroup, shutdowner Shutdowner, timeout time.Duration)
AddGracefulShutdown intercepts the cancel function of the received ctx and calls the shutdowner.Shutdown interface instead. if timeout is not null a context with a deadline is prepared prior to the Shutdown call. It is the responsibility of the Shutdowner interface implementer to honor this context deadline. The waitgroup is incremented by one immediately and one is released when the shutdown has finished.
func CspHeaderHandler ¶
CspHeaderHandler replaces the nonce placerholder in the Content-Security-header
func FallbackHandler ¶
FallbackHandler routes the request to a fallback route on of the given HTTP fallback status codes
func HealthCheckConditionalHandler ¶
HealthCheckConditionalHandler is a conditional healthcheck handler that returns HTTP 200 when the condition argument function returns true and HTTP 503 if not.
func HealthCheckHandler ¶
HealthCheckHandler is a dummy handler that always returns HTTP 200.
func NewCacheHandler ¶
NewCacheHandler computes and stores the hashes for all files
func SessionCookieHandler ¶
func SessionCookieHandler(next http.Handler, cookieName string, cookieTimeToLife time.Duration) http.Handler
SessionCookieHandler reads the cookieName cookie from the request and adds if to the context unter the SessionIdKey if present. If absent it generates a new sessionId and adds it to the context and the HTTP Set-Cookie Response header.
func ShutdownAfterWaitGroup ¶ added in v3.4.0
func ShutdownAfterWaitGroup(ctx context.Context, wg *sync.WaitGroup, server *http.Server, timeout time.Duration) error
ShutdownAfterWaitGroup shutdown the server argument after the WaitGroup wg finished via a graceful shutdown with the given timeout argument is executed. Blocks till then.
Types ¶
type AngularCspReplaceConfig ¶
type AngularCspReplaceConfig struct { // (secret) placeholder which will be replaced with the session id when serving VariableName string `json:"variable-name"` // Regex for which files the Variable-Name should be replaced FilePathPattern string `json:"file-path-regex,omitempty"` // Name of the session-id cookie CookieName string `json:"cookie-name"` // Max-Age setting for the session-id cookie, 30 seconds should be sufficient CookieMaxAge int `json:"cookie-max-age"` }
AngularCspReplaceConfig holds the config options for fixing the syle-src CSP issue in Angular.
type Config ¶
type Config struct { // Static headers to be set Headers map[string]string `json:"headers,omitempty"` AngularCspReplace *AngularCspReplaceConfig `json:"angular-csp-replace,omitempty"` // Mapping of file extension to Media-Type, needed due to https://github.com/golang/go/issues/32350 MediaTypeMap map[string]string `json:"media-type-map,omitempty"` // Media-Types for which gzipping should be applied (if activated and client has set the Accept-Encoding: gzip HTTP-Header) GzipMediaTypes []string `json:"gzip-media-types,omitempty"` }
Config holds the advanced server config options
func (*Config) GzipFileExtensions ¶
GzipFileExtensions computes the file extensions relevant for gzipping.
type ContextKey ¶
type ContextKey struct {
// contains filtered or unexported fields
}
ContextKey is a struct used for storing relevant keys in the request context.
type CspFileHandler ¶
type CspFileHandler struct { Next http.Handler VariableName string MediaTypeMap map[string]string // contains filtered or unexported fields }
CspFileHandler implements the http.Handler interface and fixes the Angular style-src CSP issue. The variableName is replaced in all response contents.
func NewCspFileHandler ¶ added in v3.3.0
func NewCspFileHandler(next http.Handler, variableName string, mediaTypeMap map[string]string) *CspFileHandler
NewCspFileHandler returns a CspFileHandler, it implements the http.Handler interface and fixes the Angular style-src CSP issue. The variableName is replaced in all response contents.
func (*CspFileHandler) ServeHTTP ¶
func (handler *CspFileHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type HandlerMiddleware ¶
HandlerMiddleware wraps a received handler with another wrapper handler to add functionality
func AccessMetrics ¶
func AccessMetrics(registration *PrometheusRegistration) HandlerMiddleware
AccessMetrics collects metrics about bytes send and response status codes and writes them to the provided prometheus registerer.
func Caching ¶
func Caching() HandlerMiddleware
Caching adds a caching middleware handler which uses the ETag HTTP response and If-None-Match HTTP request headers. This requires that all following handler only serve static resources. Following handlers will only be called when a cache mismatch occurs.
func CspFileReplace ¶
func CspFileReplace(variableName string, mediaTypeMap map[string]string) HandlerMiddleware
CspFileReplace replaces the nonce variable in all content responses and has the hard requirement that a session cookie is present in the context, see server.SessionCookie to add one.
func CspHeaderReplace ¶
func CspHeaderReplace(variableName string) HandlerMiddleware
CspHeaderReplace replaces the nonce variable in the Content-Security-Header.
func Fallback ¶
func Fallback(fallbackPath string, fallbackCodes ...int) HandlerMiddleware
Fallback adds a fallback route handler. THis routes the request to a fallback route on of the given HTTP fallback status codes
func H2C ¶
func H2C(h2cPort uint16) HandlerMiddleware
H2C adds a middleware that supports h2c (unencrypted http2)
func Header ¶
func Header(headers map[string]string) HandlerMiddleware
Header adds a static HTTP header adding middleware
func Optional ¶
func Optional(middleware HandlerMiddleware, isActive bool) HandlerMiddleware
Optional sets the middleware if the isActive condition is fulfilled
func SessionId ¶
func SessionId(cookieName string, cookieMaxAge time.Duration) HandlerMiddleware
SessionId adds a session cookie adding middleware
func Validate ¶
func Validate() HandlerMiddleware
Validate adds to the validate middleware and prevent path transversal attacks by cleaning the request path.
type HeaderHandler ¶
HeaderHandler implements the http.Handler interface and adds the static headers provided in the Headers map to the response.
func (*HeaderHandler) ServeHTTP ¶
func (handler *HeaderHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
type PrometheusRegistration ¶
type PrometheusRegistration struct {
// contains filtered or unexported fields
}
PrometheusRegistration wraps a prometheus registerer and corresponding registered types.
func AccessMetricsRegister ¶
func AccessMetricsRegister(registerer prometheus.Registerer, prometheusNamespace string) (*PrometheusRegistration, error)
AccessMetricsRegister registrates the relevant prometheus types and returns a custom registration type
type ReplacerCollection ¶
type ReplacerCollection struct {
// contains filtered or unexported fields
}
ReplacerCollection is a series of replacer implementations which are used to effectively replace a given string by pre-splitting the target template.
func ReplacerCollectionFromInput ¶
func ReplacerCollectionFromInput(data []byte, toReplace string, mediaType string) *ReplacerCollection
ReplacerCollectionFromInput constructs a replacer that prepares the input data into a template where the toReplace string will be replaced.
type Server ¶ added in v3.4.0
func Build ¶
func Build(port uint16, readTimeout time.Duration, writeTimeout time.Duration, idleTimeout time.Duration, handler http.Handler, handlerSetups ...HandlerMiddleware) *Server
Build a http server from the provided options.
func (*Server) ListenGoServe ¶ added in v3.4.0
ListenGoServe is a half-asynchronous version of ListenAnDServe from http.Server. This blocks till net.Listen has returned, the actual Serve of the http.Server is done in a separate (automatically spawned) goroutine. All errors (including http.ErrServerClosed) are returned via the error channel.
type Shutdowner ¶
Shutdowner are functions that support a Shutdown operation. It is the responsibility of the interface implementer to honor the context deadline.