Documentation
¶
Index ¶
- Constants
- type API
- func (i *API) BindAddress(addr string)
- func (i *API) BindServer(server *http.Server)
- func (i *API) HandleGroup(groupProvider GroupProvider, middlewareProviders ...MiddlewareProvider)
- func (i *API) HandleRoute(routeProvider RouteProvider, middlewareProviders ...MiddlewareProvider)
- func (i *API) ServeHTTP(writer http.ResponseWriter, request *http.Request)
- func (i *API) Shutdown() ShutdownError
- func (i *API) UseMiddleware(providers ...MiddlewareProvider)
- type Context
- type Group
- func (g *Group) GetGroup() (prefix string, routes []*Route)
- func (g *Group) HandleGroup(groupProvider GroupProvider, middlewareProviders ...MiddlewareProvider)
- func (g *Group) HandleRoute(routeProvider RouteProvider, middlewareProviders ...MiddlewareProvider)
- func (g *Group) Use(providers ...MiddlewareProvider)
- type GroupProvider
- type MiddlewareHandler
- type MiddlewareProvider
- type Pattern
- type Response
- func (r *Response) Error(status int)
- func (r *Response) ErrorMessage(status int, message string)
- func (r *Response) Header() http.Header
- func (r *Response) RegisterOnBeforeWriteHeader(callback func(int))
- func (r *Response) SetCookie(cookie *http.Cookie)
- func (r *Response) Write(b []byte) (int, error)
- func (r *Response) WriteHeader(statusCode int)
- func (r *Response) WriteJSON(v any) error
- func (r *Response) WriteString(s string) error
- func (r *Response) Written() bool
- type Route
- type RouteHandler
- type RouteProvider
- type ServerError
- type ShutdownError
Constants ¶
const ( // Default listen address (127.0.0.1:8080). DefaultAddr string = "127.0.0.1:8080" // Default read timeout (5 minutes). DefaultReadTimeout time.Duration = 5 * time.Minute // Default read header timeout (30 seconds). DefaultReadHeaderTimeout time.Duration = 30 * time.Second // Default write timeout (10 minutes). DefaultWriteTimeout time.Duration = 10 * time.Minute // Default idle timeout (5 seconds). DefaultIdleTimeout time.Duration = 5 * time.Second )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type API ¶ added in v0.0.10
type API struct {
// Global middleware handlers applied to all requests handled by this API,
// even if no route is matched. Middleware is executed in the order it is
// added.
MiddlewareHandlers []MiddlewareHandler
// Optional request Context Logger. If nil, then slog.Default() is used.
Logger *slog.Logger
// Optional callback that is called when a listener is created.
Listening func(url string)
// Optional callback that is called to handle server errors. If this is nil,
// server errors will cause a panic.
OnServerError func(err *ServerError)
// contains filtered or unexported fields
}
A wrapper for the Go built-in net/http.Mux with quality-of-life enhancements like non-blocking server binding, graceful shutdown, and middleware support.
func (*API) BindAddress ¶ added in v0.0.10
Bind this API to a new HTTP server listening on the given address. This call is non-blocking.
Listening ¶
If Addr is empty, the listener will bind to DefaultAddr.
Errors ¶
If the server fails and the API's Error field is non-nil, it will be called with the server and the error. If the Error field is nil, a panic will occur.
func (*API) BindServer ¶ added in v0.0.10
Bind this API to the given HTTP server. This function is non-blocking. This function will mutate the given server's fields!
Defaults ¶
Server fields will be updated as follows.
- Handler is set to the API instance.
- Addr is set to the final listener address.
- If ReadTimeout is zero, DefaultReadTimeout is used.
- If ReadHeaderTimeout is zero, DefaultReadHeaderTimeout is used.
- If WriteTimeout is zero, DefaultWriteTimeout is used.
- If IdleTimeout is zero, DefaultIdleTimeout is used.
Listening ¶
If Addr is empty, the listener will bind to DefaultAddr.
TLS ¶
If TLSConfig is non-nil, TLS is used.
Errors ¶
If the server fails and the API's Error field is non-nil, it will be called with the server and the error. If the Error field is nil, a panic will occur.
func (*API) HandleGroup ¶ added in v0.0.10
func (i *API) HandleGroup(groupProvider GroupProvider, middlewareProviders ...MiddlewareProvider)
Add a group of routes to the API with optional middleware.
func (*API) HandleRoute ¶ added in v0.0.10
func (i *API) HandleRoute(routeProvider RouteProvider, middlewareProviders ...MiddlewareProvider)
Add a route to the API with optional middleware.
func (*API) ServeHTTP ¶ added in v0.0.10
func (i *API) ServeHTTP(writer http.ResponseWriter, request *http.Request)
func (*API) Shutdown ¶ added in v0.0.10
func (i *API) Shutdown() ShutdownError
Attempt to gracefully shutdown all servers that were started by the API. If a server does not shutdown within 10 seconds, it will be forcibly closed.
func (*API) UseMiddleware ¶ added in v0.0.10
func (i *API) UseMiddleware(providers ...MiddlewareProvider)
Use middleware in all requests handled by this API, even if no route is matched. Middleware is executed in the order it is added.
type Context ¶
type Context struct {
// A logger specific to this request context.
Logger *slog.Logger
// The associated HTTP request.
Request *http.Request
// The associated HTTP response.
Response *Response
}
Context for an API request, containing request-specific data and convenience methods.
Use the NewContext constructor to create Context instances.
func GetContext ¶ added in v0.0.7
Retrieve the api.Context from the net/http.Request. This will panic if the request was not handled by the given API instance and has no associated api.Context.
func NewContext ¶ added in v0.0.7
Create a new API request Context, and associate it with the given net/http.Request so that it can be retrieved given the same API and request pointers.
type Group ¶
type Group struct {
Prefix string
MiddlewareHandlers []MiddlewareHandler
Routes []*Route
// contains filtered or unexported fields
}
A group of routes with a common prefix and shared middleware.
func (*Group) HandleGroup ¶ added in v0.0.8
func (g *Group) HandleGroup(groupProvider GroupProvider, middlewareProviders ...MiddlewareProvider)
Add a sub-group of routes to the group with optional middleware.
func (*Group) HandleRoute ¶ added in v0.0.8
func (g *Group) HandleRoute(routeProvider RouteProvider, middlewareProviders ...MiddlewareProvider)
Add a route to the group with optional route specific middleware.
func (*Group) Use ¶
func (g *Group) Use(providers ...MiddlewareProvider)
Use middleware in all requests handled by routes in this group. These will only be applied when a route in this group (or a sub-group) is matched. Middleware is executed in the order it is added.
type GroupProvider ¶ added in v0.0.7
type GroupProvider interface {
GetGroup() (prefix string, routes []RouteProvider)
}
Provides a group of routes.
type MiddlewareHandler ¶ added in v0.0.11
type MiddlewareHandler func(ctx *Context, next func())
type MiddlewareProvider ¶ added in v0.0.11
type MiddlewareProvider interface {
GetMiddleware() MiddlewareHandler
}
MiddlewareProvider is a request handler that can process requests before and/or after passing control to the next handler in the chain.
type Pattern ¶
A parsed ServeMux route pattern.
func ParsePattern ¶
Parse a ServeMux route pattern string.
type Response ¶
type Response struct {
// The underlying [io.Writer] for writing response body data. This can be
// wrapped by middleware to provide features like compression.
Writer io.Writer
// contains filtered or unexported fields
}
Response accessor with convenience methods for writing common responses.
Use the NewResponse constructor to create Response instances.
func NewResponse ¶ added in v0.0.7
func NewResponse( responseWriter http.ResponseWriter, request *http.Request, ) *Response
Create a new Response.
func (*Response) Error ¶
Write an error message with the given status code to the response. The plain text message will be the standard HTTP status text for the code.
func (*Response) ErrorMessage ¶
Write an plain text error message with the given status code to the response.
func (*Response) RegisterOnBeforeWriteHeader ¶ added in v0.0.7
Register a callback that is called before the response header is written. This can be used to perform last-minute modifications to the header, log information, or decide whether to replace the response Writer.
func (*Response) WriteHeader ¶ added in v0.0.7
Write the header and status code to the response. This should only be called once per response. After it is called, the header and status code can no longer be modified.
func (*Response) WriteString ¶ added in v0.0.12
type Route ¶
type Route struct {
Pattern string
Handler RouteHandler
}
A single endpoint which maps a pattern to a handler function.
type RouteHandler ¶ added in v0.0.11
type RouteHandler = func(ctx *Context)
type RouteProvider ¶ added in v0.0.7
type RouteProvider interface {
GetRoute() *Route
}
Provides a single route.
type ServerError ¶
type ServerError struct {
// contains filtered or unexported fields
}
Passed to the API.OnServerError callback when an net/http.Server fails that was bound to an API.
func (*ServerError) Error ¶
func (e *ServerError) Error() string
func (*ServerError) Server ¶
func (e *ServerError) Server() *http.Server
type ShutdownError ¶
type ShutdownError []*ServerError
Returned by API.Shutdown if one or more servers failed to shutdown cleanly.
func (ShutdownError) Error ¶
func (e ShutdownError) Error() string
func (ShutdownError) Unwrap ¶
func (e ShutdownError) Unwrap() []error