xhttp

package
v0.0.2-rc-ci Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 15, 2024 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

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

func CorsMiddleware(next http.Handler) http.Handler

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 LastModifiedAt(lastModified time.Time) func(next http.Handler) http.Handler

func LogMiddleware

func LogMiddleware(next http.Handler) http.Handler

LogMiddleware returns a new http middleware, that logs every request.

func ReadJSON

func ReadJSON[T any](r *http.Request) (*T, error)

func ReadParamInt

func ReadParamInt(r *http.Request, name string) (int, error)

func ReadQueryInt

func ReadQueryInt(r *http.Request, name string) (int, error)

func ReadQueryInt64

func ReadQueryInt64(r *http.Request, name string) (int64, error)

func ReadQueryJson

func ReadQueryJson(r *http.Request, name string, v interface{}) error

func ReadQueryString

func ReadQueryString(r *http.Request, name string) (string, error)

func ReadQueryStringArray

func ReadQueryStringArray(r *http.Request, name string) ([]string, error)

func ReadQueryTime

func ReadQueryTime(r *http.Request, name, layout string, defaultTime time.Time, tz *time.Location) (time.Time, error)

func RequestIDMiddleware

func RequestIDMiddleware(next http.Handler) http.Handler

RequestID returns a new http middleware, that adds a unique requestID to the request context.

func RequestIsNotModified

func RequestIsNotModified(r *http.Request, lastModified time.Time) bool

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 WriteJSON

func WriteJSON[T any](ctx context.Context, w http.ResponseWriter, data T)

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.

type LastModifiedCache

type LastModifiedCache interface {
	// LastModified indicates the last time an entity was updated.
	LastModified() time.Time
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL