Documentation ¶
Overview ¶
pf (Pickme Framework) is a REST API library based on chi for use by me and my boys on hackathons, olympiads and for personal projects.
Index ¶
- Variables
- func AddSwagger(r *Router, endpoint string, info *SwaggerInfo) error
- func Delete[Req, Res any](r *Router, path string, handler Handler[Req, Res], props ...HandlerProperty)
- func DeleteStd(r *Router, path string, handler http.HandlerFunc, props ...HandlerProperty)
- func Get[Res any](r *Router, path string, handler Handler[struct{}, Res], ...)
- func GetStd(r *Router, path string, handler http.HandlerFunc, props ...HandlerProperty)
- func Handle(r *Router, path string, handler http.Handler)
- func HandleError(w http.ResponseWriter, err error)
- func HandleFunc(r *Router, path string, handler http.HandlerFunc)
- func Head(r *Router, path string, handler Handler[struct{}, struct{}], ...)
- func HeadStd(r *Router, path string, handler http.HandlerFunc, props ...HandlerProperty)
- func Method[Req, Res any](r *Router, method string, path string, handler Handler[Req, Res], ...)
- func MethodStd(r *Router, method string, path string, handler http.HandlerFunc, ...)
- func Mount(r *Router, path string, subrouter *Router)
- func Options[Res any](r *Router, path string, handler Handler[struct{}, Res], ...)
- func OptionsStd(r *Router, path string, handler http.HandlerFunc, props ...HandlerProperty)
- func Patch[Req, Res any](r *Router, path string, handler Handler[Req, Res], props ...HandlerProperty)
- func PatchStd(r *Router, path string, handler http.HandlerFunc, props ...HandlerProperty)
- func Post[Req, Res any](r *Router, path string, handler Handler[Req, Res], props ...HandlerProperty)
- func PostStd(r *Router, path string, handler http.HandlerFunc, props ...HandlerProperty)
- func Put[Req, Res any](r *Router, path string, handler Handler[Req, Res], props ...HandlerProperty)
- func PutStd(r *Router, path string, handler http.HandlerFunc, props ...HandlerProperty)
- func Route(r *Router, path string, fn func(r *Router))
- func Use(r *Router, middlewares ...func(next http.Handler) http.Handler)
- type Handler
- type HandlerProperty
- type Request
- type ResponseWriter
- type Router
- type SwaggerInfo
Constants ¶
This section is empty.
Variables ¶
var ( ErrBadRequest error = httpError(400) ErrPaymentRequired error = httpError(402) ErrForbidden error = httpError(403) ErrNotFound error = httpError(404) ErrMethodNotAllowed error = httpError(405) ErrNotAcceptable error = httpError(406) ErrProxyAuthRequired error = httpError(407) ErrRequestTimeout error = httpError(408) ErrConflict error = httpError(409) ErrGone error = httpError(410) ErrLengthRequired error = httpError(411) ErrPreconditionFailed error = httpError(412) ErrRequestEntityTooLarge error = httpError(413) ErrRequestURITooLong error = httpError(414) ErrUnsupportedMediaType error = httpError(415) ErrRequestedRangeNotSatisfiable error = httpError(416) ErrExpectationFailed error = httpError(417) ErrImATeapot error = httpError(418) ErrMisdirectedRequest error = httpError(421) ErrUnprocessableEntity error = httpError(422) ErrLocked error = httpError(423) ErrFailedDependency error = httpError(424) ErrTooEarly error = httpError(425) ErrUpgradeRequired error = httpError(426) ErrPreconditionRequired error = httpError(428) ErrTooManyRequests error = httpError(429) ErrRequestHeaderFieldsTooLarge error = httpError(431) ErrInternalServerError error = httpError(500) ErrNotImplemented error = httpError(501) ErrBadGateway error = httpError(502) ErrGatewayTimeout error = httpError(504) ErrHTTPVersionNotSupported error = httpError(505) ErrVariantAlsoNegotiates error = httpError(506) ErrInsufficientStorage error = httpError(507) ErrLoopDetected error = httpError(508) ErrNotExtended error = httpError(510) ErrNetworkAuthenticationRequired error = httpError(511) )
Functions ¶
func AddSwagger ¶
func AddSwagger(r *Router, endpoint string, info *SwaggerInfo) error
AddSwagger generates the OpenAPI 2.0 spec from the handlers already created in r and routes the Swagger spec page to endpoint. Only call AddSwagger after routing every handler you wish displayed on the page.
func Delete ¶
func Delete[Req, Res any](r *Router, path string, handler Handler[Req, Res], props ...HandlerProperty)
func DeleteStd ¶ added in v1.0.1
func DeleteStd(r *Router, path string, handler http.HandlerFunc, props ...HandlerProperty)
func Get ¶
func Get[Res any](r *Router, path string, handler Handler[struct{}, Res], props ...HandlerProperty)
func GetStd ¶ added in v1.0.1
func GetStd(r *Router, path string, handler http.HandlerFunc, props ...HandlerProperty)
func Handle ¶
Handle routes handler to the path. You should not use this for regular function handlers, as they won't show up in Swagger.
func HandleError ¶
func HandleError(w http.ResponseWriter, err error)
HandleError handles any errors that might occur in handlers and middlewares. For errors defined in the package, HandleError sets the appropriate status code and responds with the standard message. For other errors, HandleError logs the error with slog.Error and responds with status code 500 and the standard message.
func HandleFunc ¶
func HandleFunc(r *Router, path string, handler http.HandlerFunc)
Handle routes handler to the path. You should not use this for regular function handlers, as they won't show up in Swagger.
func Head ¶
func Head(r *Router, path string, handler Handler[struct{}, struct{}], props ...HandlerProperty)
func HeadStd ¶ added in v1.0.1
func HeadStd(r *Router, path string, handler http.HandlerFunc, props ...HandlerProperty)
func Method ¶
func Method[Req, Res any](r *Router, method string, path string, handler Handler[Req, Res], props ...HandlerProperty)
Method adds routes for path that matches the HTTP method specified by method. Method also adds metadata, consisting of the request and response type, as well as props for use by Swagger and the like.
func MethodStd ¶ added in v1.0.1
func MethodStd(r *Router, method string, path string, handler http.HandlerFunc, props ...HandlerProperty)
MethodStd adds routes for path that matches the HTTP method specified by method. If there are any props, MethodStd also adds metadata from props for use by Swagger and the like. MethodStd is different from Method in that it uses http.HandlerFunc instead of Handler.
func Options ¶
func Options[Res any](r *Router, path string, handler Handler[struct{}, Res], props ...HandlerProperty)
func OptionsStd ¶ added in v1.0.1
func OptionsStd(r *Router, path string, handler http.HandlerFunc, props ...HandlerProperty)
func Patch ¶
func Patch[Req, Res any](r *Router, path string, handler Handler[Req, Res], props ...HandlerProperty)
func PatchStd ¶ added in v1.0.1
func PatchStd(r *Router, path string, handler http.HandlerFunc, props ...HandlerProperty)
func Post ¶
func Post[Req, Res any](r *Router, path string, handler Handler[Req, Res], props ...HandlerProperty)
func PostStd ¶ added in v1.0.1
func PostStd(r *Router, path string, handler http.HandlerFunc, props ...HandlerProperty)
func Put ¶
func Put[Req, Res any](r *Router, path string, handler Handler[Req, Res], props ...HandlerProperty)
func PutStd ¶ added in v1.0.1
func PutStd(r *Router, path string, handler http.HandlerFunc, props ...HandlerProperty)
Types ¶
type Handler ¶
type Handler[Req, Res any] func(w ResponseWriter[Res], r *Request[Req]) error
Handler represents an HTTP callback. Handler takes in a parsed Request (optionally, to do nothing set Req to struct{}), as well as a ResponseWriter that may marshal the response. Request and ResponseWriter contain the underlying http.Request and ResponseWriter from net/http.
type HandlerProperty ¶
HandlerProperty represents a modification to the handler's metadata (summary, description etc.) for Swagger.
func WithConsumes ¶
func WithConsumes(mime ...string) HandlerProperty
WithConsumes sets the MIME types the handler expects as the request body.
func WithDescription ¶
func WithDescription(description string) HandlerProperty
WithSummary sets the handler's description.
func WithProduces ¶
func WithProduces(mime ...string) HandlerProperty
WithProduces sets the MIME types the handler produces as a response.
func WithQuery ¶
func WithQuery(query ...string) HandlerProperty
WithQuery adds query parameters to the handler's metadata.
func WithSummary ¶
func WithSummary(summary string) HandlerProperty
WithSummary sets the handler's summary.
type Request ¶
Request wraps http.Request and provides a Body of type T. Body is parsed depending on T: If T is struct{}, then Body is equal to struct{}{}; If T is []byte, then the request body is read into Body; If T is *multipart.Form, then the form data is fetched using ParseMultipartForm. Otherwise, the response body is assumed to be JSON and deserialized into Body.
type ResponseWriter ¶
type ResponseWriter[T any] struct { http.ResponseWriter }
ResponseWriter wraps an http.ResponseWriter instance, adding convenience methods for marshaling the output.
func (*ResponseWriter[T]) JSON ¶
func (w *ResponseWriter[T]) JSON(status int, response T) error
JSON marshals response as JSON and sends an HTTP response with the status code specified by status.
func (*ResponseWriter[T]) OK ¶
func (w *ResponseWriter[T]) OK(response T) error
OK marshals response as JSON and sends an HTTP response with status code 200.