webapp

package
v0.0.0-...-9e5d004 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2023 License: MIT Imports: 22 Imported by: 2

Documentation

Index

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 New

func New(cfg config.Server, logger *slog.Logger) WebApp

New returns a new WebApp with the given ServerConfig and Logger set.

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

func (app *WebApp) BadRequestResponse(w http.ResponseWriter, r *http.Request, err error)

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

func (app *WebApp) EnableCORS(cfg config.Cors, next http.Handler) http.Handler

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

func (app *WebApp) FailResponse(w http.ResponseWriter, r *http.Request, status int, data any)

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

func (app *WebApp) Metrics(next http.Handler) http.Handler

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

func (app *WebApp) RateLimit(cfg config.Limiter, next http.Handler) http.Handler

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) ReadCSV

func (app *WebApp) ReadCSV(qs url.Values, key string, defaultValue []string) []string

func (*WebApp) ReadIDParam

func (app *WebApp) ReadIDParam(r *http.Request) (int64, error)

func (*WebApp) ReadInt

func (app *WebApp) ReadInt(qs url.Values, key string, defaultValue int,
	v *validator.Validator) int

func (*WebApp) ReadString

func (app *WebApp) ReadString(qs url.Values, key string, defaultValue string) string

func (*WebApp) RecoverPanic

func (app *WebApp) RecoverPanic(next http.Handler) http.Handler

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

func (app *WebApp) Serve(routes http.Handler) error

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

func (app *WebApp) ServerErrorResponse(w http.ResponseWriter, r *http.Request, err error)

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.

Jump to

Keyboard shortcuts

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