Documentation ¶
Index ¶
- func CorsHandler(w http.ResponseWriter, r *http.Request)
- func CorsMiddleware(next http.Handler) http.Handler
- func LastModified(modifications ...LastModifiedCache) func(next http.Handler) http.Handler
- func LastModifiedAt(lastModified time.Time) func(next http.Handler) http.Handler
- func LogMiddleware(next http.Handler) http.Handler
- func ReadJSON[T any](r *http.Request) (*T, error)
- func ReadParamInt(r *http.Request, name string) (int, error)
- func ReadQueryInt(r *http.Request, name string) (int, error)
- func ReadQueryInt64(r *http.Request, name string) (int64, error)
- func ReadQueryJson(r *http.Request, name string, v interface{}) error
- func ReadQueryString(r *http.Request, name string) (string, error)
- func ReadQueryStringArray(r *http.Request, name string) ([]string, error)
- func ReadQueryTime(r *http.Request, name, layout string, defaultTime time.Time, tz *time.Location) (time.Time, error)
- func RequestIDMiddleware(next http.Handler) http.Handler
- func RequestIsNotModified(r *http.Request, lastModified time.Time) bool
- func WriteBadRequest(ctx context.Context, w http.ResponseWriter, err error)
- func WriteError(ctx context.Context, w http.ResponseWriter, err error)
- func WriteForbidden(w http.ResponseWriter, err error)
- func WriteInternalError(ctx context.Context, w http.ResponseWriter, err error)
- func WriteJSON[T any](ctx context.Context, w http.ResponseWriter, data T)
- func WriteNotModified(w http.ResponseWriter, lastModified time.Time)
- func WriteServiceUnavailable(w http.ResponseWriter)
- func WriteUnauthorized(w http.ResponseWriter, err error)
- func WriteWithLastModified(w http.ResponseWriter, data []byte, lastModified time.Time)
- type Error
- type LastModifiedCache
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CorsHandler ¶
func CorsHandler(w http.ResponseWriter, r *http.Request)
CorsHandler handles an OPTION request for CORS checks. The Access-Control-Allow-Origin header should be set by the middleware.
func CorsMiddleware ¶
CorsMiddleware returns a new http middleware. It currently allows all origins.
func LastModified ¶
func LastModified(modifications ...LastModifiedCache) func(next http.Handler) http.Handler
LastModified is a middleware, that implements a http cache using the Last-Modified header. It takes a list of LastModifiedCache always using the one that was last modified.
func LastModifiedAt ¶
func LogMiddleware ¶
LogMiddleware returns a new http middleware, that logs every request.
func ReadQueryStringArray ¶
func ReadQueryTime ¶
func RequestIDMiddleware ¶
RequestID returns a new http middleware, that adds a unique requestID to the request context.
func RequestIsNotModified ¶
RequestIsNotModified checks for the If-Modified-Since header and compares it with the provided lastModified value. Returns true, if the header is set and its value is before or equal to the lastModified value.
func WriteBadRequest ¶
func WriteBadRequest(ctx context.Context, w http.ResponseWriter, err error)
WriteBadRequest writes a http response with status code 400.
func WriteError ¶
func WriteError(ctx context.Context, w http.ResponseWriter, err error)
WriteError writes a generic error. It checks wether it is an xerror.AuthError or an xerror.BadInputError. It writes an internal server error for all other cases.
func WriteForbidden ¶
func WriteForbidden(w http.ResponseWriter, err error)
WriteForbidden writes a http response with status code 403.
func WriteInternalError ¶
func WriteInternalError(ctx context.Context, w http.ResponseWriter, err error)
WriteInternalError writes a http response with status code 500. Since the error won't be written to the http response, it gets logged instead.
func WriteNotModified ¶
func WriteNotModified(w http.ResponseWriter, lastModified time.Time)
WriteNotModified writes 304 status code and sets the Last-Modified header.
func WriteServiceUnavailable ¶
func WriteServiceUnavailable(w http.ResponseWriter)
WriteInternalError writes a http response with status code 503.
func WriteUnauthorized ¶
func WriteUnauthorized(w http.ResponseWriter, err error)
WriteUnauthorized writes a http response with status code 401.
func WriteWithLastModified ¶
func WriteWithLastModified(w http.ResponseWriter, data []byte, lastModified time.Time)
WriteWithLastModified wirtes a 200 status code with the given data and sets the Last-Modified and Cache-Control headers.
Types ¶
type Error ¶
type Error struct { // Error message. Error string `json:"error"` }
Error defines the structure of an error response.