web

package
v0.0.0-...-109c033 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateHandler

func CreateHandler[Req, Res any](endpoint Endpoint[Req, Res],
	requestHandler RequestHandler[Req],
	responseHandler ResponseHandler[Res],
) http.Handler

func EncodeToJSON

func EncodeToJSON(ctx context.Context, w http.ResponseWriter, obj interface{})

func EncodeWithStatus

func EncodeWithStatus[T any](status int, value *T, w http.ResponseWriter) error

EncodeWithStatus will attempt to encode the provided `value` into the response writer `w` and will write the status header. If the encoding fails, the http status will not be written to the response writer and the function will return an error instead.

func SendAPIErrorResponse

func SendAPIErrorResponse(ctx context.Context, w http.ResponseWriter, apiErr common.APIError)

SendAPIErrorResponse will send an api error which contains relevant information about the failed request to the client. The function will also set the http status according to the provided status.

func SendErrorResponse

func SendErrorResponse(ctx context.Context, w http.ResponseWriter, err error)

SendErrorResponse will send HTTPStatusErrorResponse if err is common.APIError.

Otherwise sends internal server error.

func SendErrorWithStatusAndMessage

func SendErrorWithStatusAndMessage(ctx context.Context, w http.ResponseWriter, status int, message common.ErrorMessageCode, details string)

SendErrorWithStatusAndMessage will construct an api error which contains relevant information about the failed request to the client The function will also set the http status according to the provided status.

func SendUnauthorizedResponse

func SendUnauthorizedResponse(ctx context.Context, w http.ResponseWriter, details string)

SendUnauthorizedResponse sends a standardized StatusUnauthorized response to the client.

Types

type CtxKeyRequestURL

type CtxKeyRequestURL struct{}

type Endpoint

type Endpoint[Req, Res any] func(ctx context.Context, request *Req, w http.ResponseWriter) (*Res, error)

type RequestHandler

type RequestHandler[Req any] func(r *http.Request, w http.ResponseWriter) (*Req, error)

type ResponseHandler

type ResponseHandler[Res any] func(ctx context.Context, res *Res, w http.ResponseWriter) error

Jump to

Keyboard shortcuts

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