Documentation
¶
Index ¶
- Variables
- func Chain(handler http.Handler, middleware ...MiddlewareFunc) http.Handler
- func Coalesce[T comparable](values ...T) T
- func HTML(w http.ResponseWriter, r *http.Request, status int, html string)
- func IIF[T any](condition bool, v1, v2 T) T
- func IsHTMX(r *http.Request) bool
- func JSON(w http.ResponseWriter, r *http.Request, status int, data any)
- func Redirect(w http.ResponseWriter, r *http.Request, status int, route string)
- func Render(w http.ResponseWriter, r *http.Request, status int, tpls ...Renderable)
- type MiddlewareFunc
- type Mux
- type Renderable
- type Server
- func (s *Server) Group(pattern string) Mux
- func (s *Server) Handle(pattern string, handler http.Handler, middleware ...MiddlewareFunc)
- func (s *Server) HandleFunc(pattern string, handlerFunc http.HandlerFunc, middleware ...MiddlewareFunc)
- func (s *Server) ListenAndServe(ctx context.Context) error
- func (s *Server) ListenAndServeTLS(ctx context.Context, certFile, keyFile string) error
- func (s *Server) Use(middleware ...MiddlewareFunc)
- type ServerOption
Constants ¶
This section is empty.
Variables ¶
var ( ErrInvalidPrefix = errors.New("invalid prefix") ErrInvalidPattern = errors.New("invalid pattern") )
var ( ErrHTMLResponse = errors.New("HTML response") ErrJSONResponse = errors.New("JSON response") ErrRenderResponse = errors.New("Render response") )
var ( ErrDuplicatePattern = errors.New("pattern already registered") ErrNilContext = errors.New("nil context") ErrServerRunning = errors.New("http server already running") )
var ResponseErrFunc = func(w http.ResponseWriter, r *http.Request, err error) { _ = r slog.Error("response", slog.Any("error", err)) http.Error(w, "internal server error", http.StatusInternalServerError) }
ResponseErrFunc logs the response error using slog.Error and returns a http.Error with status InternalServerError.
Reassign ResponseErrFunc to use a custom handler with signature: func(http.ResponseWriter, *http.Request, error).
Functions ¶
func Chain ¶
func Chain(handler http.Handler, middleware ...MiddlewareFunc) http.Handler
Chain applies middleware to the handler in the given stack order.
func Coalesce ¶ added in v0.2.2
func Coalesce[T comparable](values ...T) T
Coalesce returns the first non-zero value for type T if one exists, otherwise the zero value is returned.
func IIF ¶ added in v0.2.2
IIF is an inline if that returns v1 if the condition is true, otherwise v2 is returned.
func IsHTMX ¶ added in v0.2.0
IsHTMX returns true if the request originated from HTMX, otherwise false.
func JSON ¶ added in v0.2.0
JSON encodes the given data and writes it to the response with the given status.
func Redirect ¶ added in v0.2.0
Redirect is a HTMX aware redirect.
- On a non-HTMX initiated request, a standard http.Redirect is performed.
- On a HTMX initiated request, the Hx-Redirect header is set and the status 200 OK is used.
Reason for status override: https://github.com/bigskysoftware/htmx/issues/2052#issuecomment-1979805051
func Render ¶ added in v0.2.0
func Render(w http.ResponseWriter, r *http.Request, status int, tpls ...Renderable)
RenderTempl writes any number of Renderables to the response with the given status.
Types ¶
type Mux ¶ added in v0.2.2
type Mux interface {
Handle(pattern string, handler http.Handler, middleware ...MiddlewareFunc)
HandleFunc(pattern string, handlerFunc http.HandlerFunc, middleware ...MiddlewareFunc)
ServeHTTP(w http.ResponseWriter, r *http.Request)
}
Mux is a thin wrapper extending the net/http ServeMux with handler based middleware registration.
type Renderable ¶ added in v0.2.2
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func New ¶
func New(opts ...ServerOption) *Server
New returns a new Server instance with default configuration.
Defaults:
- Addr: ":8000"
- IdleTimeout: 1 minute
- MaxHeaderBytes: 1MB
- ReadHeaderTimeout: 5 seconds
- ShutdownTimeout: 5 seconds
func (*Server) Handle ¶ added in v0.2.2
func (s *Server) Handle(pattern string, handler http.Handler, middleware ...MiddlewareFunc)
Handle registers the handler for the given pattern.
If the given pattern conflicts with on that is already registered or if the pattern is invalid, Handle panics.
func (*Server) HandleFunc ¶ added in v0.2.2
func (s *Server) HandleFunc(pattern string, handlerFunc http.HandlerFunc, middleware ...MiddlewareFunc)
HandleFunc registers the handlerFunc for the given pattern.
If the given pattern conflicts with on that is already registered or if the pattern is invalid, HandleFunc panics.
func (*Server) ListenAndServe ¶
ListenAndServe listens on the TCP network address configured to handle requests on incoming connections. Accepted connections are configured to enable TCP keep-alives.
Graceful shutdown is handled automatically via context cancellation.
func (*Server) ListenAndServeTLS ¶
ListenAndServeTLS acts identically to ListenAndServe, except that it expects HTTPS connections. Additionally, files containing a certificate and matching private key for the server must be provided. If the certificate is signed by a certificate authority, the certFile should be the concatenation of the server's certificate, any intermediates, and the CA's certificate.
Graceful shutdown is handled automatically via context cancellation.
func (*Server) Use ¶ added in v0.2.2
func (s *Server) Use(middleware ...MiddlewareFunc)
Use applies middleware globally to all handlers in the given stack order.
type ServerOption ¶ added in v0.2.2
type ServerOption func(*Server)
ServerOption provides a functional way to configure the Server.
func WithHttpServer ¶ added in v0.2.2
func WithHttpServer(srv *http.Server) ServerOption
WithHttpServer uses the http.Server as a configuration. All fields are respected except for Handler.
func WithPort ¶ added in v0.3.0
func WithPort(port uint16) ServerOption
func WithShutdownTimeout ¶ added in v0.2.2
func WithShutdownTimeout(d time.Duration) ServerOption
WithShutdownTimeout specifies the allowed time the server has to gracefully shutdown before force closing. Values that are less than or equal to zero are ignored.