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) 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) 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
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) Handle ¶
func (app *App) Handle(method, path string, handler Handler, options ...RouteOption)
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) 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)
func (*App) SetErrorHandler ¶
func (app *App) SetErrorHandler(handler ErrorHandler)
func (*App) Use ¶
func (app *App) Use(middleware ...Middleware)
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
func (*BindingError) Unwrap ¶ added in v0.4.0
func (err *BindingError) Unwrap() error
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 ¶
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) Response ¶
func (context *Context) Response() http.ResponseWriter
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)
func (*Group) Handle ¶
func (group *Group) Handle(method, path string, handler Handler, options ...RouteOption)
func (*Group) Options ¶
func (group *Group) Options(path string, handler Handler, options ...RouteOption)
func (*Group) Patch ¶
func (group *Group) Patch(path string, handler Handler, options ...RouteOption)
func (*Group) Use ¶
func (group *Group) Use(middleware ...Middleware)
type HTTPError ¶
HTTPError is an application error with a stable HTTP representation.
func BadRequest ¶
func InternalServerError ¶
func MethodNotAllowed ¶ added in v0.3.0
func NewHTTPError ¶
func RequestEntityTooLarge ¶
func Unauthorized ¶
func UnsupportedMediaType ¶
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 WithIdleTimeout ¶
func WithLogger ¶
func WithMaxBodySize ¶
func WithReadHeaderTimeout ¶
func WithReadTimeout ¶
func WithShutdownTimeout ¶
func WithWriteTimeout ¶
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
func (*ResponseWriter) Committed ¶
func (writer *ResponseWriter) Committed() bool
func (*ResponseWriter) Flush ¶
func (writer *ResponseWriter) Flush()
func (*ResponseWriter) Hijack ¶
func (writer *ResponseWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
func (*ResponseWriter) Push ¶
func (writer *ResponseWriter) Push(target string, options *http.PushOptions) error
func (*ResponseWriter) ReadFrom ¶
func (writer *ResponseWriter) ReadFrom(reader io.Reader) (int64, error)
func (*ResponseWriter) Status ¶
func (writer *ResponseWriter) Status() int
func (*ResponseWriter) Unwrap ¶
func (writer *ResponseWriter) Unwrap() http.ResponseWriter
func (*ResponseWriter) WriteHeader ¶
func (writer *ResponseWriter) WriteHeader(status int)
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.
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
|
|
|
hello
command
|
|
|
json-api
command
|
|
|
modules
command
|
|
|
upload
command
|
|
|
Package fault defines transport-neutral application errors.
|
Package fault defines transport-neutral application errors. |
|
internal
|
|
|
Package middleware contains the framework's optional default HTTP middleware.
|
Package middleware contains the framework's optional default HTTP middleware. |
|
Package testkit provides lifecycle-aware HTTP helpers for Vial applications.
|
Package testkit provides lifecycle-aware HTTP helpers for Vial applications. |
|
vialgrpc
module
|
|
|
vialws
module
|