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) 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) 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) Committed() bool
- 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) Route() *Route
- func (context *Context) Set(key string, value any)
- func (context *Context) SetLogger(logger *slog.Logger)
- 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) 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 LifecycleHook
- type Middleware
- type Module
- type Option
- func WithDisallowUnknownJSONFields(enabled bool) Option
- func WithIdleTimeout(timeout time.Duration) Option
- func WithLogger(logger *slog.Logger) Option
- func WithMaxBodySize(bytes int64) Option
- func WithReadHeaderTimeout(timeout time.Duration) Option
- func WithReadTimeout(timeout time.Duration) Option
- func WithShutdownTimeout(timeout time.Duration) Option
- func WithWriteTimeout(timeout time.Duration) Option
- type Registrar
- 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) Use(middleware ...Middleware)
- type ResponseWriter
- func (writer *ResponseWriter) BytesWritten() int64
- func (writer *ResponseWriter) Committed() bool
- func (writer *ResponseWriter) Flush()
- func (writer *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
- func (writer *ResponseWriter) Push(target string, options *http.PushOptions) error
- func (writer *ResponseWriter) ReadFrom(reader io.Reader) (int64, error)
- 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
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) 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 (*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) 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.
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) 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 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 WithDisallowUnknownJSONFields ¶
WithDisallowUnknownJSONFields controls rejection of unknown JSON fields.
func WithIdleTimeout ¶
WithIdleTimeout sets the HTTP server keep-alive idle timeout.
func WithLogger ¶
WithLogger sets the application logger when logger is non-nil.
func WithMaxBodySize ¶
WithMaxBodySize sets the request body limit when bytes is positive.
func WithReadHeaderTimeout ¶
WithReadHeaderTimeout sets the HTTP server header-read timeout.
func WithReadTimeout ¶
WithReadTimeout sets the HTTP server request-read timeout.
func WithShutdownTimeout ¶
WithShutdownTimeout sets the graceful shutdown deadline when timeout is positive.
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) 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) 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) Flush ¶
func (writer *ResponseWriter) Flush()
Flush implements http.Flusher when supported by the underlying writer.
func (*ResponseWriter) Hijack ¶
func (writer *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
Hijack delegates connection ownership to the underlying http.Hijacker.
func (*ResponseWriter) Push ¶
func (writer *ResponseWriter) Push(target string, options *http.PushOptions) error
Push delegates an HTTP/2 server push to the underlying http.Pusher.
func (*ResponseWriter) ReadFrom ¶
func (writer *ResponseWriter) ReadFrom(reader io.Reader) (int64, error)
ReadFrom copies reader to the response while recording its size.
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 RouteOption ¶ added in v0.4.0
type RouteOption struct {
// contains filtered or unexported fields
}
RouteOption configures route metadata.
func RouteName ¶ added in v0.4.0
func RouteName(name string) RouteOption
RouteName assigns a stable name to a route.
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.
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
|