Documentation ¶
Index ¶
- Constants
- func AddLogOutput(w interface{}, key, value string) error
- func DecodeResponseBody(res *http.Response) ([]byte, error)
- func DecodingReader(header http.Header, body io.ReadCloser) (io.ReadCloser, error)
- func ENUM(values ...string) []string
- func ErrorFromHttpResponseBody(r io.ReadCloser) (error, error)
- func ExtractAndRestoreRequestBody(r *http.Request) []byte
- func GetAsBool(key string, r *http.Request) *bool
- func GetAsBoolFromContext(key string, ctx context.Context) *bool
- func GetAsDuration(key string, r *http.Request) *time.Duration
- func GetAsDurationFromContext(key string, ctx context.Context) *time.Duration
- func GetAsFloat32(key string, r *http.Request) *float32
- func GetAsFloat32FromContext(key string, ctx context.Context) *float32
- func GetAsFloat64(key string, r *http.Request) *float64
- func GetAsFloat64FromContext(key string, ctx context.Context) *float64
- func GetAsInt(key string, r *http.Request) *int
- func GetAsInt32(key string, r *http.Request) *int32
- func GetAsInt32FromContext(key string, ctx context.Context) *int32
- func GetAsInt64(key string, r *http.Request) *int64
- func GetAsInt64FromContext(key string, ctx context.Context) *int64
- func GetAsIntFromContext(key string, ctx context.Context) *int
- func GetAsString(key string, r *http.Request) *string
- func GetAsStringFromContext(key string, ctx context.Context) *string
- func GetAsTime(key string, r *http.Request) *time.Time
- func GetAsTimeFromContext(key string, ctx context.Context) *time.Time
- func GzipEncodeRequestBody(body []byte) (io.ReadCloser, error)
- func HandleMetrics(metrics map[string]interface{}, method string, status int, uri string, ...) error
- func IsAutomaticCacheExecution(r *http.Request) bool
- func StaticFilesHandler(u *UHTTP) http.HandlerFunc
- type ContextKey
- type Handler
- type HandlerFunc
- type HandlerFuncWithModel
- type HandlerOption
- func WithAutomaticCacheUpdates(interval time.Duration, skipMiddleware *string, parameters []map[string]string) HandlerOption
- func WithCache(maxAge time.Duration) HandlerOption
- func WithCacheFailedRequests() HandlerOption
- func WithCachePersistEncodings() HandlerOption
- func WithDebugRawRequestBody(fn func([]byte)) HandlerOption
- func WithDelete(h HandlerFunc) HandlerOption
- func WithDeleteModel(m interface{}, h HandlerFuncWithModel) HandlerOption
- func WithDisableAccessLogging() HandlerOption
- func WithGet(h HandlerFunc) HandlerOption
- func WithGetModel(m interface{}, h HandlerFuncWithModel) HandlerOption
- func WithMiddlewares(m ...Middleware) HandlerOption
- func WithOptionalGet(r R) HandlerOption
- func WithPost(h HandlerFunc) HandlerOption
- func WithPostModel(m interface{}, h HandlerFuncWithModel) HandlerOption
- func WithPreProcess(p PreProcessFunc) HandlerOption
- func WithRequiredGet(r R) HandlerOption
- func WithTimeout(timeout time.Duration, timeoutMessage string) HandlerOption
- type HttpResponseErrorModel
- type Logger
- type LoggingResponseWriter
- func (lrw *LoggingResponseWriter) AddLogOutput(key, value string)
- func (lrw *LoggingResponseWriter) Header() http.Header
- func (lrw *LoggingResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (lrw *LoggingResponseWriter) Write(data []byte) (int, error)
- func (lrw *LoggingResponseWriter) WriteHeader(code int)
- type Middleware
- type PreProcessFunc
- type R
- type UHTTP
- func (u *UHTTP) AddContext(key ContextKey, value interface{}) error
- func (u *UHTTP) CORS() string
- func (u *UHTTP) ExposeCacheHandlers(middlewares ...Middleware)
- func (u *UHTTP) Handle(pattern string, handler Handler)
- func (u *UHTTP) ListenAndServe() error
- func (u *UHTTP) Log() Logger
- func (u *UHTTP) MetricsServeMux() (*http.ServeMux, error)
- func (u *UHTTP) RegisterStaticFilesHandler(root string) error
- func (u *UHTTP) Render(w http.ResponseWriter, r *http.Request, model interface{})
- func (u *UHTTP) RenderError(w http.ResponseWriter, r *http.Request, err error)
- func (u *UHTTP) RenderErrorWithStatusCode(w http.ResponseWriter, r *http.Request, statusCode int, err error, logOut bool)
- func (u *UHTTP) RenderWithStatusCode(w http.ResponseWriter, r *http.Request, statusCode int, model interface{})
- func (u *UHTTP) ServeMux() *http.ServeMux
- func (u *UHTTP) ValidateParams(requirement R, actual map[string]string, destination R, required bool) error
- type UhttpOption
- func WithAddress(address string) UhttpOption
- func WithBrotliCompression(enable bool, level int) UhttpOption
- func WithCORS(cors string) UhttpOption
- func WithCacheTTLEnforcerInterval(i time.Duration) UhttpOption
- func WithDeflateCompression(enable bool, level int) UhttpOption
- func WithGlobalMiddlewares(middlewares ...Middleware) UhttpOption
- func WithGranularLogging(logHandlerCalls bool, logHandlerRegistrations bool, logStaticFileAccess bool) UhttpOption
- func WithGzipCompression(enable bool, level int) UhttpOption
- func WithHandleHandlerPanics(fn func(r *http.Request, err error)) UhttpOption
- func WithIdleTimeout(idleTimeout time.Duration) UhttpOption
- func WithLogCacheRuns(logCacheRuns bool) UhttpOption
- func WithLogCustomMiddlewareRegistration() UhttpOption
- func WithLogEncodingError(fn func(template string, args ...interface{})) UhttpOption
- func WithLogHandlerError(fn func(template string, args ...interface{})) UhttpOption
- func WithLogParseModelError(fn func(template string, args ...interface{})) UhttpOption
- func WithLogger(logger Logger) UhttpOption
- func WithMetrics(metricsSocket string, metricsPath string) UhttpOption
- func WithReadHeaderTimeout(readHeaderTimeout time.Duration) UhttpOption
- func WithReadTimeout(readTimeout time.Duration) UhttpOption
- func WithSendPanicInfoToClient(sendPanicInfoToClient bool) UhttpOption
- func WithSilentStaticFileRegistration(makeFileRegistrationSilent bool) UhttpOption
- func WithTLS(certPath string, keyPath string, tlsErrorLogger *log.Logger) UhttpOption
- func WithWriteTimeout(writeTimeout time.Duration) UhttpOption
Constants ¶
const ( HEADER_ACCEPT_ENCODING = "Accept-Encoding" HEADER_CONTENT_ENCODING = "Content-Encoding" ENCODING_PLAIN = "" ENCODING_BROTLI = "br" ENCODING_GZIP = "gzip" ENCODING_DEFLATE = "deflate" )
const ( Metric_Requests_Total string = "uhttp_requests_total" Metric_Requests_Duration string = "uhttp_requests_durations" )
const ( STRING string = "string" BOOL string = "bool" INT string = "int" INT32 string = "int32" INT64 string = "int64" FLOAT32 string = "float32" FLOAT64 string = "float64" SHORT_DATE string = "shortDate" // 2006-01-02 RFC3339_DATE string = "rfc3339Date" DURATION string = "duration" )
const CACHE_HEADER = "X-UHTTP-CACHE"
const CACHE_HEADER_AGE_HUMAN_READABLE = "X-UHTTP-CACHE-AGE-HUMAN-READABLE"
const CACHE_HEADER_AGE_MS = "X-UHTTP-CACHE-AGE-MS"
const NO_LOG_MAGIC_URL_FORCE_CACHE = "UHTTP_NO_LOG_FORCE_CACHE"
Variables ¶
This section is empty.
Functions ¶
func AddLogOutput ¶ added in v1.0.41
func DecodeResponseBody ¶ added in v1.2.2
func DecodingReader ¶ added in v1.0.86
func DecodingReader(header http.Header, body io.ReadCloser) (io.ReadCloser, error)
Wraps a reader in the correct decoder based on http-headers inspired by https://medium.com/axiomzenteam/put-your-http-requests-on-a-diet-3e1e52333014
func ErrorFromHttpResponseBody ¶ added in v1.0.70
func ErrorFromHttpResponseBody(r io.ReadCloser) (error, error)
func ExtractAndRestoreRequestBody ¶ added in v1.0.84
func GetAsBoolFromContext ¶ added in v1.0.41
func GetAsDuration ¶ added in v1.0.88
func GetAsDurationFromContext ¶ added in v1.0.88
func GetAsFloat32FromContext ¶ added in v1.0.41
func GetAsFloat64FromContext ¶ added in v1.0.41
func GetAsInt32FromContext ¶ added in v1.0.41
func GetAsInt64FromContext ¶ added in v1.0.41
func GetAsIntFromContext ¶ added in v1.0.41
func GetAsStringFromContext ¶ added in v1.0.41
func GetAsTimeFromContext ¶ added in v1.0.41
func GzipEncodeRequestBody ¶ added in v1.0.42
func GzipEncodeRequestBody(body []byte) (io.ReadCloser, error)
func HandleMetrics ¶ added in v1.0.63
func IsAutomaticCacheExecution ¶ added in v1.0.86
func StaticFilesHandler ¶ added in v1.2.2
func StaticFilesHandler(u *UHTTP) http.HandlerFunc
static files handler which only works if initialized with "RegisterStaticFilesHandler" (only serves from initialized cache)
Types ¶
type ContextKey ¶ added in v1.0.61
type ContextKey string
const ( CtxKeyPostModel ContextKey = "uhttp.postModel" CtxKeyIsAutomaticCacheExecution ContextKey = "uhttp.isAutomaticCacheExecution" CtxKeyCache ContextKey = "uhttp.cache" CtxKeyGetParams ContextKey = "uhttp.getParams" CtxKeyResponseWriter ContextKey = "uhttp.responseWriter" CtxKeyUHTTP ContextKey = "uhttp.uhttp" CtxKeyTest ContextKey = "uhttp.test" )
func ContextKeysFromMap ¶ added in v1.0.62
func ContextKeysFromMap(m interface{}) []ContextKey
type Handler ¶
type Handler struct {
// contains filtered or unexported fields
}
func NewHandler ¶ added in v1.0.50
func NewHandler(opts ...HandlerOption) Handler
func (Handler) HandlerFunc ¶
func (h Handler) HandlerFunc(u *UHTTP) http.HandlerFunc
func (Handler) WsReady ¶ added in v1.0.32
func (h Handler) WsReady(u *UHTTP) Middleware
type HandlerFunc ¶ added in v1.0.50
type HandlerFuncWithModel ¶ added in v1.0.50
type HandlerOption ¶ added in v1.0.50
type HandlerOption interface {
// contains filtered or unexported methods
}
func WithAutomaticCacheUpdates ¶ added in v1.0.81
func WithAutomaticCacheUpdates(interval time.Duration, skipMiddleware *string, parameters []map[string]string) HandlerOption
Call handler in the background discarding the response (only useful if cache is enabled)
func WithCache ¶ added in v1.0.81
func WithCache(maxAge time.Duration) HandlerOption
Cache handler invocations with a maxAge
func WithCacheFailedRequests ¶ added in v1.1.10
func WithCacheFailedRequests() HandlerOption
Also cache failed requests
func WithCachePersistEncodings ¶ added in v1.0.87
func WithCachePersistEncodings() HandlerOption
When creating the cache, not only keep the response model in the cache but also create all enabled compressed versions of it this will take load of the server if many calls hit the cache, but comes with a heavy memory penalty
func WithDebugRawRequestBody ¶ added in v1.2.4
func WithDebugRawRequestBody(fn func([]byte)) HandlerOption
Register callback for raw request-body (for debugging)
func WithDelete ¶ added in v1.0.50
func WithDelete(h HandlerFunc) HandlerOption
Func to be called when the request is invoked with `DELETE`
func WithDeleteModel ¶ added in v1.0.50
func WithDeleteModel(m interface{}, h HandlerFuncWithModel) HandlerOption
Func to be called when the request is invoked with `DELETE` and a request-body should be parsed into a model
func WithDisableAccessLogging ¶ added in v1.1.12
func WithDisableAccessLogging() HandlerOption
Disable access-log for this handler
func WithGet ¶ added in v1.0.50
func WithGet(h HandlerFunc) HandlerOption
Func to be called when the request is invoked with `GET`
func WithGetModel ¶ added in v1.0.50
func WithGetModel(m interface{}, h HandlerFuncWithModel) HandlerOption
Func to be called when the request is invoked with `GET` and a request-body should be parsed into a model
func WithMiddlewares ¶ added in v1.0.50
func WithMiddlewares(m ...Middleware) HandlerOption
Add additional middlewares
func WithOptionalGet ¶ added in v1.0.53
func WithOptionalGet(r R) HandlerOption
Add optional query-parameters which will be parsed and validated
func WithPost ¶ added in v1.0.50
func WithPost(h HandlerFunc) HandlerOption
Func to be called when the request is invoked with `POST`
func WithPostModel ¶ added in v1.0.50
func WithPostModel(m interface{}, h HandlerFuncWithModel) HandlerOption
Func to be called when the request is invoked with `POST` and a request-body should be parsed into a model
func WithPreProcess ¶ added in v1.0.50
func WithPreProcess(p PreProcessFunc) HandlerOption
Execute a function before the handler is invoked
func WithRequiredGet ¶ added in v1.0.50
func WithRequiredGet(r R) HandlerOption
Add required query-parameters which will be parsed and validated The framework will make sure they are present
func WithTimeout ¶ added in v1.0.50
func WithTimeout(timeout time.Duration, timeoutMessage string) HandlerOption
Execute the handler with a timeout (wrapped in an original golang `http.TimeoutHandler`)
type HttpResponseErrorModel ¶ added in v1.0.70
type HttpResponseErrorModel struct {
Error string `json:"error"`
}
func NewHttpErrorResponse ¶ added in v1.0.70
func NewHttpErrorResponse(err error) HttpResponseErrorModel
type Logger ¶ added in v1.0.41
type Logger interface { Infof(template string, args ...interface{}) Errorf(template string, args ...interface{}) }
func NewDefaultLogger ¶ added in v1.1.18
func NewDefaultLogger() Logger
func NewDiscardLogger ¶ added in v1.1.18
func NewDiscardLogger() Logger
type LoggingResponseWriter ¶ added in v1.0.41
type LoggingResponseWriter struct {
// contains filtered or unexported fields
}
func (*LoggingResponseWriter) AddLogOutput ¶ added in v1.0.41
func (lrw *LoggingResponseWriter) AddLogOutput(key, value string)
func (*LoggingResponseWriter) Header ¶ added in v1.0.66
func (lrw *LoggingResponseWriter) Header() http.Header
Delegate Header() to underlying responseWriter
func (*LoggingResponseWriter) Hijack ¶ added in v1.0.66
func (lrw *LoggingResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
Delegate Hijack() to underlying responseWriter
func (*LoggingResponseWriter) Write ¶ added in v1.0.66
func (lrw *LoggingResponseWriter) Write(data []byte) (int, error)
Delegate Write() to underlying responseWriter
func (*LoggingResponseWriter) WriteHeader ¶ added in v1.0.41
func (lrw *LoggingResponseWriter) WriteHeader(code int)
Delegate WriteHeader() to underlying responseWriter AND save code
type Middleware ¶
type Middleware func(next http.HandlerFunc) http.HandlerFunc
func AuthBasic ¶ added in v1.1.6
func AuthBasic(u *UHTTP, expectedUsername string, expectedHashedPasswordSha256 string) Middleware
func WithContextMiddleware ¶ added in v1.0.41
func WithContextMiddleware(key ContextKey, value interface{}) Middleware
WithContext attaches any object to the context
type PreProcessFunc ¶ added in v1.0.50
type UHTTP ¶ added in v1.0.50
type UHTTP struct {
// contains filtered or unexported fields
}
func NewUHTTP ¶ added in v1.0.50
func NewUHTTP(opts ...UhttpOption) *UHTTP
func (*UHTTP) AddContext ¶ added in v1.0.50
func (u *UHTTP) AddContext(key ContextKey, value interface{}) error
func (*UHTTP) ExposeCacheHandlers ¶ added in v1.1.6
func (u *UHTTP) ExposeCacheHandlers(middlewares ...Middleware)
func (*UHTTP) ListenAndServe ¶ added in v1.0.50
func (*UHTTP) MetricsServeMux ¶ added in v1.1.8
func (*UHTTP) RegisterStaticFilesHandler ¶ added in v1.0.50
RegisterStaticFilesHandler which serves content from a directory and redirects all requests to non-existant files to index.html index.html must be present! - read all files from root directory - create cache for these files containing original, gzip, br - register handlers for main http-mux
func (*UHTTP) Render ¶ added in v1.0.50
func (u *UHTTP) Render(w http.ResponseWriter, r *http.Request, model interface{})
HelperMethod for rendering a JSON model
func (*UHTTP) RenderError ¶ added in v1.0.50
HelperMethod for rendering an error as JSON while automatically setting a 400 statusCode
func (*UHTTP) RenderErrorWithStatusCode ¶ added in v1.0.50
func (u *UHTTP) RenderErrorWithStatusCode(w http.ResponseWriter, r *http.Request, statusCode int, err error, logOut bool)
HelperMethod for rendering an error as JSON with defining a custom statusCode
func (*UHTTP) RenderWithStatusCode ¶ added in v1.0.50
func (u *UHTTP) RenderWithStatusCode(w http.ResponseWriter, r *http.Request, statusCode int, model interface{})
HelperMethod for rendering a JSON model with statusCode in the response
type UhttpOption ¶ added in v1.0.50
type UhttpOption interface {
// contains filtered or unexported methods
}
func WithAddress ¶ added in v1.0.50
func WithAddress(address string) UhttpOption
func WithBrotliCompression ¶ added in v1.0.85
func WithBrotliCompression(enable bool, level int) UhttpOption
func WithCORS ¶ added in v1.0.50
func WithCORS(cors string) UhttpOption
func WithCacheTTLEnforcerInterval ¶ added in v1.0.81
func WithCacheTTLEnforcerInterval(i time.Duration) UhttpOption
func WithDeflateCompression ¶ added in v1.0.85
func WithDeflateCompression(enable bool, level int) UhttpOption
func WithGlobalMiddlewares ¶ added in v1.0.50
func WithGlobalMiddlewares(middlewares ...Middleware) UhttpOption
func WithGranularLogging ¶ added in v1.0.77
func WithGranularLogging(logHandlerCalls bool, logHandlerRegistrations bool, logStaticFileAccess bool) UhttpOption
func WithGzipCompression ¶ added in v1.0.85
func WithGzipCompression(enable bool, level int) UhttpOption
func WithHandleHandlerPanics ¶ added in v1.1.2
func WithHandleHandlerPanics(fn func(r *http.Request, err error)) UhttpOption
func WithIdleTimeout ¶ added in v1.0.50
func WithIdleTimeout(idleTimeout time.Duration) UhttpOption
func WithLogCacheRuns ¶ added in v1.0.86
func WithLogCacheRuns(logCacheRuns bool) UhttpOption
func WithLogCustomMiddlewareRegistration ¶ added in v1.0.82
func WithLogCustomMiddlewareRegistration() UhttpOption
func WithLogEncodingError ¶ added in v1.2.3
func WithLogEncodingError(fn func(template string, args ...interface{})) UhttpOption
func WithLogHandlerError ¶ added in v1.2.3
func WithLogHandlerError(fn func(template string, args ...interface{})) UhttpOption
func WithLogParseModelError ¶ added in v1.2.3
func WithLogParseModelError(fn func(template string, args ...interface{})) UhttpOption
func WithLogger ¶ added in v1.0.50
func WithLogger(logger Logger) UhttpOption
func WithMetrics ¶ added in v1.0.63
func WithMetrics(metricsSocket string, metricsPath string) UhttpOption
func WithReadHeaderTimeout ¶ added in v1.0.50
func WithReadHeaderTimeout(readHeaderTimeout time.Duration) UhttpOption
func WithReadTimeout ¶ added in v1.0.50
func WithReadTimeout(readTimeout time.Duration) UhttpOption
func WithSendPanicInfoToClient ¶ added in v1.0.67
func WithSendPanicInfoToClient(sendPanicInfoToClient bool) UhttpOption
func WithSilentStaticFileRegistration ¶ added in v1.0.80
func WithSilentStaticFileRegistration(makeFileRegistrationSilent bool) UhttpOption
WithSilentStaticFileRegistration disables logging for every static file registration, handy, if dealing with large file trees
func WithTLS ¶ added in v1.0.59
func WithTLS(certPath string, keyPath string, tlsErrorLogger *log.Logger) UhttpOption
func WithWriteTimeout ¶ added in v1.0.50
func WithWriteTimeout(writeTimeout time.Duration) UhttpOption
Source Files ¶
- cache_handlers.go
- context_key_helpers.go
- context_keys.go
- handle_static_files.go
- handler.go
- handler_options.go
- helpers.go
- helpers_decoding.go
- helpers_encoding.go
- helpers_rendering.go
- http_response_error_model.go
- logger.go
- metrics.go
- middleware.go
- middleware_add_logging.go
- middleware_auth_basic.go
- middleware_cache.go
- middleware_cors.go
- middleware_get_params.go
- middleware_parse_model.go
- middleware_pre_process.go
- middleware_select_method.go
- middleware_set_json_response.go
- middleware_with_context.go
- middleware_with_original_response_writer.go
- middleware_with_uhttp.go
- params.go
- params_helpers.go
- params_parse.go
- uhttp.go
- uhttp_options.go