Documentation
¶
Overview ¶
Package hio provides a simple HTTP handler interface that allows chaining handlers.
Index ¶
- func DecodeJSON(from io.Reader, to any) error
- func EncodeJSON(to http.ResponseWriter, from any, status int) error
- func MaxBytesReader(w http.ResponseWriter, rc io.ReadCloser, max int64) io.ReadCloser
- func Serve(ctx context.Context, h http.Handler, opts ...ServeOption) error
- func TrailingSlashRedirector(next http.Handler) http.Handler
- type Handler
- type Middleware
- type Responder
- type Router
- func (ro *Router) Delete(pattern string, handler func(Responder) Handler)
- func (ro *Router) Get(pattern string, handler func(Responder) Handler)
- func (ro *Router) Group(prefix string, mws ...Middleware) *Router
- func (ro *Router) Handle(method, pattern string, handler func(Responder) Handler)
- func (ro *Router) Patch(pattern string, handler func(Responder) Handler)
- func (ro *Router) Post(pattern string, handler func(Responder) Handler)
- func (ro *Router) Put(pattern string, handler func(Responder) Handler)
- func (ro *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (ro *Router) Use(mws ...Middleware)
- type ServeConfig
- type ServeOption
- func WithConfig(v ServeConfig) ServeOption
- func WithHost(v string) ServeOption
- func WithIdleTimeout(v time.Duration) ServeOption
- func WithOptions(v ...ServeOption) ServeOption
- func WithPort(v int) ServeOption
- func WithReadTimeout(v time.Duration) ServeOption
- func WithShutdownTimeout(v time.Duration) ServeOption
- func WithTLS(caFile, ceFile, keyFile string) ServeOption
- func WithWriteTimeout(v time.Duration) ServeOption
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func EncodeJSON ¶
func EncodeJSON(to http.ResponseWriter, from any, status int) error
EncodeJSON writes JSON to the http.ResponseWriter with the status code.
func MaxBytesReader ¶
func MaxBytesReader(w http.ResponseWriter, rc io.ReadCloser, max int64) io.ReadCloser
MaxBytesReader wraps http.MaxBytesReader to ensure the original http.ResponseWriter is unwrapped so that it can instruct the http.Server to disconnect clients when they reach the max bytes limit.
Types ¶
type Handler ¶
type Handler func(http.ResponseWriter, *http.Request) Handler
Handler is a chainable http.Handler implementation.
type Middleware ¶
Middleware is an alias for a function that takes and returns an http.Handler.
type Responder ¶
type Responder struct {
// contains filtered or unexported fields
}
Responder provides helpers to write HTTP responses.
func NewErrorLoggingResponder ¶
func NewErrorLoggingResponder(l *slog.Logger, fn func(http.ResponseWriter, *http.Request, *slog.Logger, error)) Responder
NewErrorLoggingResponder returns a new Responder that logs errors using the provided logger and function.
func NewResponder ¶
NewResponder returns a new Responder. err is called when an error occurs during response writing.
type Router ¶
type Router struct {
NotFound error
MethodNotAllowed error
// contains filtered or unexported fields
}
Router adapts http.ServeMux to use Handler with an error logging Responder.
func NewRouter ¶
func NewRouter(l *slog.Logger, fn func(http.ResponseWriter, *http.Request, *slog.Logger, error)) *Router
NewRouter returns a new Router that logs errors using the provided logger and function.
func (*Router) Delete ¶
Delete registers a DELETE handler for the given pattern responding with the Responder provided in NewRouter.
func (*Router) Get ¶
Get registers a GET handler for the given pattern responding with the Responder provided in NewRouter.
func (*Router) Group ¶
func (ro *Router) Group(prefix string, mws ...Middleware) *Router
Group creates a new Router with the given prefix and middlewares.
func (*Router) Handle ¶
Handle registers the handler for the given pattern and method responding with the Responder provided in NewRouter.
func (*Router) Patch ¶
Patch registers a PATCH handler for the given pattern responding with the Responder provided in NewRouter.
func (*Router) Post ¶
Post registers a POST handler for the given pattern responding with the Responder provided in NewRouter.
func (*Router) Put ¶
Put registers a PUT handler for the given pattern responding with the Responder provided in NewRouter.
func (*Router) ServeHTTP ¶
func (ro *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP dispatches the request to the handler whose pattern most closely matches the request URL.
func (*Router) Use ¶
func (ro *Router) Use(mws ...Middleware)
Use adds the given middlewares to the Router.
type ServeConfig ¶
type ServeConfig struct {
Host string
Port int
IdleTimeout time.Duration
ReadTimeout time.Duration
WriteTimeout time.Duration
ShutdownTimeout time.Duration
ErrorLog *log.Logger
TLS *tls.Config
// contains filtered or unexported fields
}
ServeConfig configures an HTTP server.
func DefaultServeConfig ¶
func DefaultServeConfig() ServeConfig
DefaultServeConfig returns a ServeConfig with default values.
func (ServeConfig) Addr ¶
func (c ServeConfig) Addr() string
Addr returns the address the server listens on.
func (*ServeConfig) Override ¶
func (c *ServeConfig) Override(other ServeConfig)
Override replaces field values with non-zero values from other.
func (*ServeConfig) Validate ¶
func (c *ServeConfig) Validate() error
Validate checks that the configuration is valid.
type ServeOption ¶
type ServeOption interface {
// contains filtered or unexported methods
}
ServeOption applies options to a ServeConfig.
func WithConfig ¶
func WithConfig(v ServeConfig) ServeOption
WithConfig applies the provided configuration, replacing any existing values.
func WithIdleTimeout ¶
func WithIdleTimeout(v time.Duration) ServeOption
WithIdleTimeout sets the idle timeout.
func WithOptions ¶
func WithOptions(v ...ServeOption) ServeOption
WithOptions applies multiple [ServeOption]s.
func WithReadTimeout ¶
func WithReadTimeout(v time.Duration) ServeOption
WithReadTimeout sets the read timeout.
func WithShutdownTimeout ¶
func WithShutdownTimeout(v time.Duration) ServeOption
WithShutdownTimeout sets the shutdown timeout.
func WithTLS ¶
func WithTLS(caFile, ceFile, keyFile string) ServeOption
WithTLS configures TLS with the provided certificate authority, certificate, and key files.
func WithWriteTimeout ¶
func WithWriteTimeout(v time.Duration) ServeOption
WithWriteTimeout sets the write timeout.