hio

package
v0.0.0-...-81557fa Latest Latest
Warning

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

Go to latest
Published: Sep 24, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package hio provides a simple HTTP handler interface that allows chaining handlers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeJSON

func DecodeJSON(from io.Reader, to any) error

DecodeJSON reads and decodes JSON.

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.

func Serve

func Serve(ctx context.Context, h http.Handler, opts ...ServeOption) error

func TrailingSlashRedirector

func TrailingSlashRedirector(next http.Handler) http.Handler

TrailingSlashRedirector is a middleware that redirects requests with a trailing slash to the same URL without the trailing slash.

Types

type Handler

type Handler func(http.ResponseWriter, *http.Request) Handler

Handler is a chainable http.Handler implementation.

func (Handler) ServeHTTP

func (h Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP runs the Handler chain until one returns nil.

type Middleware

type Middleware = func(http.Handler) http.Handler

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

func NewResponder(err func(error) Handler) Responder

NewResponder returns a new Responder. err is called when an error occurs during response writing.

func (Responder) Error

func (rs Responder) Error(err error) Handler

Error responds with a error message.

func (Responder) Errorf

func (rs Responder) Errorf(format string, args ...any) Handler

Errorf responds with a formatted error message.

func (Responder) JSON

func (rs Responder) JSON(code int, from any) Handler

JSON writes a JSON response with the status code.

func (Responder) Redirect

func (rs Responder) Redirect(code int, url string) Handler

Redirect diverts the request to the URL with the status code.

func (Responder) Text

func (rs Responder) Text(code int, message string) Handler

Text writes a text response with the status code.

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

func (ro *Router) Delete(pattern string, handler func(Responder) Handler)

Delete registers a DELETE handler for the given pattern responding with the Responder provided in NewRouter.

func (*Router) Get

func (ro *Router) Get(pattern string, handler func(Responder) Handler)

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

func (ro *Router) Handle(method, pattern string, handler func(Responder) Handler)

Handle registers the handler for the given pattern and method responding with the Responder provided in NewRouter.

func (*Router) Patch

func (ro *Router) Patch(pattern string, handler func(Responder) Handler)

Patch registers a PATCH handler for the given pattern responding with the Responder provided in NewRouter.

func (*Router) Post

func (ro *Router) Post(pattern string, handler func(Responder) Handler)

Post registers a POST handler for the given pattern responding with the Responder provided in NewRouter.

func (*Router) Put

func (ro *Router) Put(pattern string, handler func(Responder) Handler)

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 WithHost

func WithHost(v string) ServeOption

WithHost sets the host.

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 WithPort

func WithPort(v int) ServeOption

WithPort sets the port.

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.