Documentation
¶
Overview ¶
Package vial provides a small, net/http-compatible web application kernel with error-returning handlers, middleware, request helpers, and graceful serving.
Index ¶
- func StatusCode(err error) int
- type App
- func (app *App) Build() error
- func (app *App) Delete(path string, handler Handler, options ...RouteOption)
- func (app *App) Get(path string, handler Handler, options ...RouteOption)
- func (app *App) Go(name string, task Task, options ...TaskOption)
- func (app *App) Group(prefix string) *Group
- func (app *App) Handle(method, path string, handler Handler, options ...RouteOption)
- func (app *App) HandleHTTP(pattern string, handler http.Handler, options ...RouteOption)
- func (app *App) Health(path string)
- func (app *App) OnStart(hooks ...LifecycleHook)
- func (app *App) OnStop(hooks ...LifecycleHook)
- func (app *App) Options(path string, handler Handler, options ...RouteOption)
- func (app *App) Patch(path string, handler Handler, options ...RouteOption)
- func (app *App) Post(path string, handler Handler, options ...RouteOption)
- func (app *App) Put(path string, handler Handler, options ...RouteOption)
- func (app *App) Readiness(path string, checks ...HealthCheck)
- func (app *App) Register(modules ...Module) error
- func (app *App) Routes() ([]Route, error)
- func (app *App) Run(contextValue context.Context, address string) error
- func (app *App) Serve(contextValue context.Context, listener net.Listener) (serveErr error)
- func (app *App) ServeHTTP(writer http.ResponseWriter, request *http.Request)
- func (app *App) SetErrorHandler(handler ErrorHandler)
- func (app *App) Use(middleware ...Middleware)
- type BindingError
- type Context
- func (context *Context) App() *App
- func (context *Context) Bind(destination any) error
- func (context *Context) BindCookie(destination any) error
- func (context *Context) BindForm(destination any) error
- func (context *Context) BindHeader(destination any) error
- func (context *Context) BindJSON(destination any) error
- func (context *Context) BindPath(destination any) error
- func (context *Context) BindQuery(destination any) error
- func (context *Context) BytesWritten() int64
- func (context *Context) ClientIP() (netip.Addr, error)
- func (context *Context) Committed() bool
- func (context *Context) Flush() error
- func (context *Context) FormFile(name string) (multipart.File, *multipart.FileHeader, error)
- func (context *Context) FormValue(name string) (string, error)
- func (context *Context) Get(key string) (any, bool)
- func (context *Context) Header(name string) string
- func (context *Context) JSON(status int, value any) error
- func (context *Context) Logger() *slog.Logger
- func (context *Context) NoContent(status int) error
- func (context *Context) Param(name string) string
- func (context *Context) Query(name string) string
- func (context *Context) Redirect(status int, location string) error
- func (context *Context) Request() *http.Request
- func (context *Context) Response() http.ResponseWriter
- func (context *Context) ResponseController() *http.ResponseController
- func (context *Context) Route() *Route
- func (context *Context) Set(key string, value any)
- func (context *Context) SetLogger(logger *slog.Logger)
- func (context *Context) SetWriteDeadline(deadline time.Time) error
- func (context *Context) Status() int
- func (context *Context) Text(status int, value string) error
- type ErrorHandler
- type Group
- func (group *Group) Delete(path string, handler Handler, options ...RouteOption)
- func (group *Group) Get(path string, handler Handler, options ...RouteOption)
- func (group *Group) Group(prefix string) *Group
- func (group *Group) Handle(method, path string, handler Handler, options ...RouteOption)
- func (group *Group) HandleHTTP(pattern string, handler http.Handler, options ...RouteOption)
- func (group *Group) Options(path string, handler Handler, options ...RouteOption)
- func (group *Group) Patch(path string, handler Handler, options ...RouteOption)
- func (group *Group) Post(path string, handler Handler, options ...RouteOption)
- func (group *Group) Put(path string, handler Handler, options ...RouteOption)
- func (group *Group) Use(middleware ...Middleware)
- type HTTPError
- func BadRequest(code, message string) *HTTPError
- func Conflict(code, message string) *HTTPError
- func Forbidden(code, message string) *HTTPError
- func InternalServerError(cause error) *HTTPError
- func MethodNotAllowed(code, message string) *HTTPError
- func NewHTTPError(status int, code, message string) *HTTPError
- func NotFound(code, message string) *HTTPError
- func RequestEntityTooLarge(code, message string) *HTTPError
- func Unauthorized(code, message string) *HTTPError
- func UnsupportedMediaType(code, message string) *HTTPError
- func WrapHTTPError(status int, code, message string, cause error) *HTTPError
- type Handler
- type HealthCheck
- type LifecycleHook
- type Middleware
- type Module
- type Option
- func WithBaseContext(baseContext func(net.Listener) context.Context) Option
- func WithConnContext(connContext func(context.Context, net.Conn) context.Context) Option
- func WithDisallowUnknownJSONFields(enabled bool) Option
- func WithHealthCheckTimeout(timeout time.Duration) Option
- func WithIdleTimeout(timeout time.Duration) Option
- func WithLogger(logger *slog.Logger) Option
- func WithMaxBodySize(bytes int64) Option
- func WithMaxHeaderBytes(bytes int) Option
- func WithReadHeaderTimeout(timeout time.Duration) Option
- func WithReadTimeout(timeout time.Duration) Option
- func WithServerErrorLog(logger *log.Logger) Option
- func WithShutdownTimeout(timeout time.Duration) Option
- func WithTrustedProxies(values ...string) Option
- func WithWriteTimeout(timeout time.Duration) Option
- type Registrar
- func (registrar *Registrar) Go(name string, task Task, options ...TaskOption)
- func (registrar *Registrar) Group(prefix string) *Group
- func (registrar *Registrar) Handle(method, path string, handler Handler, options ...RouteOption)
- func (registrar *Registrar) HandleHTTP(pattern string, handler http.Handler, options ...RouteOption)
- func (registrar *Registrar) Health(path string)
- func (registrar *Registrar) OnStart(hooks ...LifecycleHook)
- func (registrar *Registrar) OnStop(hooks ...LifecycleHook)
- func (registrar *Registrar) Readiness(path string, checks ...HealthCheck)
- func (registrar *Registrar) Use(middleware ...Middleware)
- type ResponseWriter
- func (writer *ResponseWriter) BytesWritten() int64
- func (writer *ResponseWriter) Committed() bool
- func (writer *ResponseWriter) Status() int
- func (writer *ResponseWriter) Unwrap() http.ResponseWriter
- func (writer *ResponseWriter) Write(data []byte) (int, error)
- func (writer *ResponseWriter) WriteHeader(status int)
- type Route
- type RouteOption
- type Task
- type TaskOption
- type Validator
- type ValueKey
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func StatusCode ¶
StatusCode returns the HTTP status represented by err, defaulting to 500.
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is the framework application and implements http.Handler.
func (*App) Delete ¶
func (app *App) Delete(path string, handler Handler, options ...RouteOption)
Delete registers a DELETE route.
func (*App) Get ¶
func (app *App) Get(path string, handler Handler, options ...RouteOption)
Get registers a GET route.
func (*App) Go ¶ added in v0.7.0
func (app *App) Go(name string, task Task, options ...TaskOption)
Go registers a named background task. Tasks are critical by default and begin running when the application starts.
func (*App) Handle ¶
func (app *App) Handle(method, path string, handler Handler, options ...RouteOption)
Handle registers a route for an HTTP method and path.
func (*App) HandleHTTP ¶
func (app *App) HandleHTTP(pattern string, handler http.Handler, options ...RouteOption)
HandleHTTP mounts a standard-library handler using a native ServeMux pattern. Examples include "/metrics", "GET /health", and "api.example.com/".
func (*App) OnStart ¶ added in v0.4.0
func (app *App) OnStart(hooks ...LifecycleHook)
OnStart registers hooks that run in registration order before components start.
func (*App) OnStop ¶ added in v0.4.0
func (app *App) OnStop(hooks ...LifecycleHook)
OnStop registers hooks that run in reverse registration order after components stop.
func (*App) Options ¶
func (app *App) Options(path string, handler Handler, options ...RouteOption)
Options registers an OPTIONS route.
func (*App) Patch ¶
func (app *App) Patch(path string, handler Handler, options ...RouteOption)
Patch registers a PATCH route.
func (*App) Post ¶
func (app *App) Post(path string, handler Handler, options ...RouteOption)
Post registers a POST route.
func (*App) Put ¶
func (app *App) Put(path string, handler Handler, options ...RouteOption)
Put registers a PUT route.
func (*App) Readiness ¶ added in v0.15.0
func (app *App) Readiness(path string, checks ...HealthCheck)
Readiness registers an endpoint that is ready only while the application is running and every check succeeds within its configured timeout.
func (*App) Routes ¶ added in v0.2.0
Routes validates the application and returns registered route metadata in registration order. The returned slice does not share state with the app.
func (*App) Run ¶
Run listens on address and serves until the parent context or a supported OS shutdown signal is received.
func (*App) Serve ¶
Serve runs the application on an existing listener. This is useful for tests and for callers that need custom listener configuration.
func (*App) ServeHTTP ¶
func (app *App) ServeHTTP(writer http.ResponseWriter, request *http.Request)
ServeHTTP implements http.Handler and builds the application on first use.
func (*App) SetErrorHandler ¶
func (app *App) SetErrorHandler(handler ErrorHandler)
SetErrorHandler replaces the application's error renderer.
func (*App) Use ¶
func (app *App) Use(middleware ...Middleware)
Use adds middleware to the application-wide chain.
type BindingError ¶ added in v0.4.0
BindingError identifies the request field that could not be bound.
func (*BindingError) Error ¶ added in v0.4.0
func (err *BindingError) Error() string
Error describes the binding source, field, and cause.
func (*BindingError) Unwrap ¶ added in v0.4.0
func (err *BindingError) Unwrap() error
Unwrap returns the binding failure cause.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context contains request-scoped framework state while retaining direct access to the standard net/http request and response writer.
func ContextFromRequest ¶ added in v0.15.0
ContextFromRequest returns the Vial context attached to a routed request. It lets standard net/http handlers access route metadata without exported context keys that could collide with application values.
func (*Context) Bind ¶ added in v0.4.0
Bind combines tagged path, query, header, cookie, JSON, and form input.
func (*Context) BindCookie ¶ added in v0.4.0
BindCookie binds request cookies to fields tagged with `cookie`.
func (*Context) BindForm ¶ added in v0.3.0
BindForm binds URL-encoded or multipart form fields to primitive struct fields tagged with `form`. Repeated values bind to slices.
func (*Context) BindHeader ¶ added in v0.4.0
BindHeader binds request headers to fields tagged with `header`.
func (*Context) BindJSON ¶
BindJSON decodes a single JSON value into destination using the application's body limit and unknown-field policy.
func (*Context) BindPath ¶ added in v0.4.0
BindPath binds route parameters to fields tagged with `path`.
func (*Context) BindQuery ¶ added in v0.3.0
BindQuery binds query parameters to primitive struct fields tagged with `query`. Repeated values bind to slices.
func (*Context) BytesWritten ¶
BytesWritten returns the response body bytes written so far.
func (*Context) ClientIP ¶ added in v0.15.0
ClientIP returns the direct peer unless it is an explicitly trusted proxy. Trusted proxy chains are evaluated from the application outward, stopping at the first untrusted address.
func (*Context) Flush ¶ added in v0.15.0
Flush sends buffered response data when the server supports streaming.
func (*Context) FormFile ¶ added in v0.3.0
FormFile returns the first uploaded file for name. The caller must close the file. Vial removes any temporary multipart files after the request finishes.
func (*Context) FormValue ¶ added in v0.10.0
FormValue returns the first URL-encoded or multipart form value for name.
func (*Context) Response ¶
func (context *Context) Response() http.ResponseWriter
Response returns the tracked HTTP response writer.
func (*Context) ResponseController ¶ added in v0.15.0
func (context *Context) ResponseController() *http.ResponseController
ResponseController exposes standard-library optional response operations.
func (*Context) SetWriteDeadline ¶ added in v0.15.0
SetWriteDeadline sets the response write deadline when supported.
type ErrorHandler ¶
ErrorHandler renders errors returned by handlers or middleware.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group applies a common path prefix and middleware chain to related routes.
func (*Group) Delete ¶
func (group *Group) Delete(path string, handler Handler, options ...RouteOption)
Delete registers a DELETE route in the group.
func (*Group) Get ¶
func (group *Group) Get(path string, handler Handler, options ...RouteOption)
Get registers a GET route in the group.
func (*Group) Handle ¶
func (group *Group) Handle(method, path string, handler Handler, options ...RouteOption)
Handle registers a route in the group.
func (*Group) HandleHTTP ¶ added in v0.15.0
func (group *Group) HandleHTTP(pattern string, handler http.Handler, options ...RouteOption)
HandleHTTP registers a standard library handler under the group's prefix.
func (*Group) Options ¶
func (group *Group) Options(path string, handler Handler, options ...RouteOption)
Options registers an OPTIONS route in the group.
func (*Group) Patch ¶
func (group *Group) Patch(path string, handler Handler, options ...RouteOption)
Patch registers a PATCH route in the group.
func (*Group) Post ¶
func (group *Group) Post(path string, handler Handler, options ...RouteOption)
Post registers a POST route in the group.
func (*Group) Put ¶
func (group *Group) Put(path string, handler Handler, options ...RouteOption)
Put registers a PUT route in the group.
func (*Group) Use ¶
func (group *Group) Use(middleware ...Middleware)
Use adds middleware to routes subsequently registered through the group.
type HTTPError ¶
HTTPError is an application error with a stable HTTP representation.
func BadRequest ¶
BadRequest creates a 400 HTTP error.
func InternalServerError ¶
InternalServerError creates a generic 500 HTTP error that retains cause.
func MethodNotAllowed ¶ added in v0.3.0
MethodNotAllowed creates a 405 HTTP error.
func NewHTTPError ¶
NewHTTPError creates an HTTP error with a stable status, code, and message.
func RequestEntityTooLarge ¶
RequestEntityTooLarge creates a 413 HTTP error.
func Unauthorized ¶
Unauthorized creates a 401 HTTP error.
func UnsupportedMediaType ¶
UnsupportedMediaType creates a 415 HTTP error.
func WrapHTTPError ¶
WrapHTTPError creates an HTTP error that retains cause.
type Handler ¶
Handler is the framework's HTTP handler contract. Returning errors keeps transport error rendering centralized and makes middleware composition simple.
type HealthCheck ¶ added in v0.15.0
HealthCheck reports whether one readiness dependency can currently serve.
type LifecycleHook ¶ added in v0.4.0
LifecycleHook runs while the application starts or stops.
type Middleware ¶
Middleware wraps a Handler with cross-cutting behavior.
type Option ¶
type Option func(*config)
Option configures an App.
func WithBaseContext ¶ added in v0.15.0
WithBaseContext sets http.Server.BaseContext.
func WithConnContext ¶ added in v0.15.0
WithConnContext sets http.Server.ConnContext.
func WithDisallowUnknownJSONFields ¶
WithDisallowUnknownJSONFields controls rejection of unknown JSON fields.
func WithHealthCheckTimeout ¶ added in v0.15.0
WithHealthCheckTimeout sets the maximum duration of each readiness check.
func WithIdleTimeout ¶
WithIdleTimeout sets the HTTP server keep-alive idle timeout.
func WithLogger ¶
WithLogger sets the application logger.
func WithMaxBodySize ¶
WithMaxBodySize sets the request body limit when bytes is positive.
func WithMaxHeaderBytes ¶ added in v0.15.0
WithMaxHeaderBytes sets the maximum request-header size.
func WithReadHeaderTimeout ¶
WithReadHeaderTimeout sets the HTTP server header-read timeout.
func WithReadTimeout ¶
WithReadTimeout sets the HTTP server request-read timeout.
func WithServerErrorLog ¶ added in v0.15.0
WithServerErrorLog sets http.Server.ErrorLog.
func WithShutdownTimeout ¶
WithShutdownTimeout sets the graceful shutdown deadline when timeout is positive.
func WithTrustedProxies ¶ added in v0.15.0
WithTrustedProxies allows forwarding headers only from the listed IPs or CIDRs. No proxies are trusted by default.
func WithWriteTimeout ¶
WithWriteTimeout sets the HTTP server response-write timeout.
type Registrar ¶ added in v0.4.0
type Registrar struct {
// contains filtered or unexported fields
}
Registrar exposes the route registration surface available to modules.
func (*Registrar) Go ¶ added in v0.15.0
func (registrar *Registrar) Go(name string, task Task, options ...TaskOption)
Go registers a supervised module background task.
func (*Registrar) Handle ¶ added in v0.4.0
func (registrar *Registrar) Handle(method, path string, handler Handler, options ...RouteOption)
Handle registers a module route.
func (*Registrar) HandleHTTP ¶ added in v0.4.0
func (registrar *Registrar) HandleHTTP(pattern string, handler http.Handler, options ...RouteOption)
HandleHTTP mounts a standard-library handler in the module.
func (*Registrar) OnStart ¶ added in v0.15.0
func (registrar *Registrar) OnStart(hooks ...LifecycleHook)
OnStart registers module startup hooks.
func (*Registrar) OnStop ¶ added in v0.15.0
func (registrar *Registrar) OnStop(hooks ...LifecycleHook)
OnStop registers module shutdown hooks.
func (*Registrar) Readiness ¶ added in v0.15.0
func (registrar *Registrar) Readiness(path string, checks ...HealthCheck)
Readiness registers a module readiness endpoint.
func (*Registrar) Use ¶ added in v0.4.0
func (registrar *Registrar) Use(middleware ...Middleware)
Use adds middleware to every route registered by the module.
type ResponseWriter ¶
type ResponseWriter struct {
http.ResponseWriter
// contains filtered or unexported fields
}
ResponseWriter tracks response state while retaining access to the underlying standard-library writer through Unwrap.
func (*ResponseWriter) BytesWritten ¶
func (writer *ResponseWriter) BytesWritten() int64
BytesWritten returns the response body bytes written so far.
func (*ResponseWriter) Committed ¶
func (writer *ResponseWriter) Committed() bool
Committed reports whether response headers were written.
func (*ResponseWriter) Status ¶
func (writer *ResponseWriter) Status() int
Status returns the written status or 200 before commitment.
func (*ResponseWriter) Unwrap ¶
func (writer *ResponseWriter) Unwrap() http.ResponseWriter
Unwrap returns the underlying standard-library response writer.
func (*ResponseWriter) Write ¶
func (writer *ResponseWriter) Write(data []byte) (int, error)
Write writes response data and records its size.
func (*ResponseWriter) WriteHeader ¶
func (writer *ResponseWriter) WriteHeader(status int)
WriteHeader records and writes the first response status.
type Route ¶
type Route struct {
Method string
Path string
Pattern string
Name string
Module string
MiddlewareCount int
Parameters []string
}
Route contains metadata for a registered endpoint.
type RouteOption ¶ added in v0.4.0
type RouteOption struct {
// contains filtered or unexported fields
}
RouteOption configures route metadata.
func RouteMiddleware ¶ added in v0.15.0
func RouteMiddleware(middleware ...Middleware) RouteOption
RouteMiddleware applies middleware only to the configured route.
func RouteName ¶ added in v0.4.0
func RouteName(name string) RouteOption
RouteName assigns a globally unique, stable observability and diagnostics identifier. Route names are not used for URL generation.
type Task ¶ added in v0.7.0
Task is application work that runs until its context is canceled or it fails.
type TaskOption ¶ added in v0.7.0
type TaskOption struct {
// contains filtered or unexported fields
}
TaskOption configures a background task.
func NonCritical ¶ added in v0.7.0
func NonCritical() TaskOption
NonCritical keeps the application running when the task exits or fails.
type Validator ¶ added in v0.10.0
type Validator interface {
Validate() error
}
Validator validates bound request data. Returning a *fault.Error preserves its field errors for response handling and rendering.
type ValueKey ¶ added in v0.15.0
type ValueKey[T any] struct { // contains filtered or unexported fields }
ValueKey is a collision-safe request value key. Keep and reuse the pointer returned by NewValueKey; identity, not the display name, selects the value.
func NewValueKey ¶ added in v0.15.0
NewValueKey creates a typed request value key.
func (*ValueKey[T]) FromRequest ¶ added in v0.15.0
FromRequest returns the same typed value through the underlying request.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
vial
command
|
|
|
Package config loads typed application configuration from explicit files and environment variables.
|
Package config loads typed application configuration from explicit files and environment variables. |
|
examples
|
|
|
config
command
|
|
|
events
command
|
|
|
hello
command
|
|
|
json-api
command
|
|
|
modules
command
|
|
|
tasks
command
|
|
|
upload
command
|
|
|
web
command
|
|
|
Package fault defines transport-neutral application errors.
|
Package fault defines transport-neutral application errors. |
|
internal
|
|
|
load
Package load runs bounded HTTP load checks for the vial command.
|
Package load runs bounded HTTP load checks for the vial command. |
|
Package middleware contains the framework's optional default HTTP middleware.
|
Package middleware contains the framework's optional default HTTP middleware. |
|
Package render writes html/template output through Vial handlers.
|
Package render writes html/template output through Vial handlers. |
|
Package testkit provides lifecycle-aware HTTP helpers for Vial applications.
|
Package testkit provides lifecycle-aware HTTP helpers for Vial applications. |
|
vialgrpc
module
|
|
|
vialws
module
|