Documentation ¶
Index ¶
- func NewClientErrorResponse(err, details, action string) clientErrorResponse
- type WebApp
- func (app *WebApp) AuthenticationRequiredResponse(w http.ResponseWriter, r *http.Request)
- func (app *WebApp) Background(fn func())
- func (app *WebApp) BadRequestResponse(w http.ResponseWriter, r *http.Request, err error)
- func (app *WebApp) EditConflictResponse(w http.ResponseWriter, r *http.Request)
- func (app *WebApp) EnableCORS(cfg config.Cors, next http.Handler) http.Handler
- func (app *WebApp) FailResponse(w http.ResponseWriter, r *http.Request, status int, data any)
- func (app *WebApp) FailedValidationResponse(w http.ResponseWriter, r *http.Request, errors map[string]string)
- func (app *WebApp) HealthCheckHandler(w http.ResponseWriter, r *http.Request)
- func (app *WebApp) InactiveAccountResponse(w http.ResponseWriter, r *http.Request)
- func (app *WebApp) InvalidAuthenticationTokenResponse(w http.ResponseWriter, r *http.Request)
- func (app *WebApp) InvalidCredentialsResponse(w http.ResponseWriter, r *http.Request)
- func (app *WebApp) MethodNotAllowedError(w http.ResponseWriter, r *http.Request)
- func (app *WebApp) Metrics(next http.Handler) http.Handler
- func (app *WebApp) NotFoundResponse(w http.ResponseWriter, r *http.Request)
- func (app *WebApp) NotPermittedResponse(w http.ResponseWriter, r *http.Request)
- func (app *WebApp) RateLimit(cfg config.Limiter, next http.Handler) http.Handler
- func (app *WebApp) RateLimitExceededResponse(w http.ResponseWriter, r *http.Request)
- func (app *WebApp) ReadCSV(qs url.Values, key string, defaultValue []string) []string
- func (app *WebApp) ReadIDParam(r *http.Request) (int64, error)
- func (app *WebApp) ReadInt(qs url.Values, key string, defaultValue int, v *validator.Validator) int
- func (app *WebApp) ReadString(qs url.Values, key string, defaultValue string) string
- func (app *WebApp) RecoverPanic(next http.Handler) http.Handler
- func (app *WebApp) Serve(routes http.Handler) error
- func (app *WebApp) ServerErrorResponse(w http.ResponseWriter, r *http.Request, err error)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewClientErrorResponse ¶
func NewClientErrorResponse(err, details, action string) clientErrorResponse
NewClientErrorResponse returns a new clientErrorResponse struct with the given values.
Types ¶
type WebApp ¶
type WebApp struct { ServerConfig config.Server Logger *slog.Logger Router *httprouter.Router Started time.Time Wg *sync.WaitGroup }
WebApp represents a generic, base web application or API that provides core functionality for receiving and responding to requests, logging, health checking, panic and error handling and various middlewares and functions.
func (*WebApp) AuthenticationRequiredResponse ¶
func (app *WebApp) AuthenticationRequiredResponse(w http.ResponseWriter, r *http.Request)
AuthenticationRequiredResponse returns an HTTP 401 (Unathorized) reponse along with an appropriate error message and help text.
func (*WebApp) Background ¶
func (app *WebApp) Background(fn func())
func (*WebApp) BadRequestResponse ¶
BadRequestResponse returns an HTTP 400 (Bad Request) response with an appropriate error message.
func (*WebApp) EditConflictResponse ¶
func (app *WebApp) EditConflictResponse(w http.ResponseWriter, r *http.Request)
EditConflictResponse means that a resource could not be updated due to a recent update preventing it. An HTTP 409 (Conflict) response is returned with an appropriate error message.
func (*WebApp) EnableCORS ¶
EnableCORS is a middleware function that handles CORS (Cross-Origin Resource Sharing) requests to prmit a web browser to make requests to a different origin (domain, scheme or port) to the main we page.
func (*WebApp) FailResponse ¶
FailResponse requests an arbitrary JSend-formatted HTTP response for a client-side error be sent to the client with the given HTTP status and data.
func (*WebApp) FailedValidationResponse ¶
func (app *WebApp) FailedValidationResponse(w http.ResponseWriter, r *http.Request, errors map[string]string)
FailedValidationResponse returns an HTTP 422 (Unprocessable Entity) response with an appropriate error message.
func (*WebApp) HealthCheckHandler ¶
func (app *WebApp) HealthCheckHandler(w http.ResponseWriter, r *http.Request)
HealthCheckHandler provides a basic health check response.
func (*WebApp) InactiveAccountResponse ¶
func (app *WebApp) InactiveAccountResponse(w http.ResponseWriter, r *http.Request)
InactiveAccountResponse returns an HTTP 403 (Forbidden) reponse along with an appropriate error message and help text.
func (*WebApp) InvalidAuthenticationTokenResponse ¶
func (app *WebApp) InvalidAuthenticationTokenResponse(w http.ResponseWriter, r *http.Request)
InvalidAuthenticationTokenResponse returns an HTTP 401 (Unauthorized) reponse along with an appropriate error message and help text.
func (*WebApp) InvalidCredentialsResponse ¶
func (app *WebApp) InvalidCredentialsResponse(w http.ResponseWriter, r *http.Request)
InvalidCredentialsResponse returns an HTTP 401 (Unauthorized) response and an appropriate error message.
func (*WebApp) MethodNotAllowedError ¶
func (app *WebApp) MethodNotAllowedError(w http.ResponseWriter, r *http.Request)
MethodNotAllowedError returns an HTTP 405 (Method Not Allowed) response with an appropriate error message and help text.
func (*WebApp) Metrics ¶
Metrics is a middleware function that keeps track of a number of metrics relating to HTTP requests.
func (*WebApp) NotFoundResponse ¶
func (app *WebApp) NotFoundResponse(w http.ResponseWriter, r *http.Request)
NotFoundResponse returns an HTTP 404 (Not Found) response with an appropriate error message.
func (*WebApp) NotPermittedResponse ¶
func (app *WebApp) NotPermittedResponse(w http.ResponseWriter, r *http.Request)
NotPermittedResponse returns an HTTP 403 (Forbidden) reponse along with an appropriate error message and help text.
func (*WebApp) RateLimit ¶
RateLimit is a middleware function that limits the number of requests a client (based on their IP address) can make in a given period.
func (*WebApp) RateLimitExceededResponse ¶
func (app *WebApp) RateLimitExceededResponse(w http.ResponseWriter, r *http.Request)
RateLimitExceeded returns an HTTP 429 (Too Many Requests) response with an appropriate message and further help details.
func (*WebApp) ReadString ¶
func (*WebApp) RecoverPanic ¶
RecoverPanic recovers any panics that happen in the goroutine that handles the request. The defered function will close the connection, then print a log message including a full stack trace. It's important to note that if the goroutine creates any further goroutines, then these must handle any panics themselves.
func (*WebApp) Serve ¶
Serve configures an http.Server and starts it running whilst also spawning a goroutine to catch certain interrupt signals and handle them more gracefully.
func (*WebApp) ServerErrorResponse ¶
ServerErrorResponse sends a generic error message to the client with an HTTP 500 (Internal Server Error) error code so as not to disclose to much information to the client. Details of the error will be logged locally.