Documentation
¶
Index ¶
- Variables
- func GetTyped[T any](c *Context, key string) (T, bool)
- func RegisterRule(name string, fn CustomRuleFunc)
- func RegisterStructValidation(t reflect.Type, fn StructLevelFunc)
- func SetValidationMessageTemplate(tag string, fn ValidationMessageFunc)
- type App
- func (a *App) AcquireContext(w http.ResponseWriter, r *http.Request) *Context
- func (a *App) AddHook(phase HookPhase, fn HookFunc) *App
- func (a *App) AddHookWithPriority(phase HookPhase, priority int, fn HookFunc) *App
- func (a *App) Any(pattern string, handler any, opts ...RouteOption) *App
- func (a *App) Delete(pattern string, handler any, opts ...RouteOption) *App
- func (a *App) Get(pattern string, handler any, opts ...RouteOption) *App
- func (a *App) Group(prefix string, fn func(g *RouteGroup)) *App
- func (a *App) Head(pattern string, handler any, opts ...RouteOption) *App
- func (a *App) Listen(addr string) error
- func (a *App) ListenMutualTLS(addr, certFile, keyFile, clientCAFile string) error
- func (a *App) ListenTLS(addr, certFile, keyFile string) error
- func (a *App) Mount(prefix string, handler http.Handler) *App
- func (a *App) OnShutdown(fn ShutdownHook) *App
- func (a *App) Options(pattern string, handler any, opts ...RouteOption) *App
- func (a *App) Patch(pattern string, handler any, opts ...RouteOption) *App
- func (a *App) Post(pattern string, handler any, opts ...RouteOption) *App
- func (a *App) Put(pattern string, handler any, opts ...RouteOption) *App
- func (a *App) Register(plugin Plugin, opts ...PluginOption) *App
- func (a *App) ReleaseContext(c *Context)
- func (a *App) Routes() []RouteInfo
- func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (a *App) SetMethodNotAllowedHandler(h HandlerFunc) *App
- func (a *App) SetNotFoundHandler(h HandlerFunc) *App
- func (a *App) Shutdown(ctx context.Context) error
- func (a *App) Use(middlewares ...Middleware) *App
- type AppError
- func ErrBadGateway(msg string) *AppError
- func ErrBadRequest(msg string) *AppError
- func ErrConflict(msg string) *AppError
- func ErrForbidden(msg string) *AppError
- func ErrGatewayTimeout(msg string) *AppError
- func ErrInternal(err error) *AppError
- func ErrMethodNotAllowed(msg string) *AppError
- func ErrNotFound(msg string) *AppError
- func ErrServiceUnavailable(msg string) *AppError
- func ErrTooManyRequests(msg string) *AppError
- func ErrUnauthorized(msg string) *AppError
- func ErrUnprocessable(msg string) *AppError
- func NewError(status int, code, message string) *AppError
- func (e *AppError) Code() string
- func (e *AppError) Detail() string
- func (e *AppError) Error() string
- func (e *AppError) Internal() error
- func (e *AppError) Message() string
- func (e *AppError) StatusCode() int
- func (e *AppError) Unwrap() error
- func (e *AppError) WithDetail(detail string) *AppError
- func (e *AppError) WithInternal(err error) *AppError
- type BindError
- type Codec
- type Config
- type Context
- func (c *Context) AddHeader(name, value string)
- func (c *Context) Attachment(filepath, filename string) error
- func (c *Context) Bind(dest any) error
- func (c *Context) BindForm(dest any) error
- func (c *Context) BindJSON(dest any) error
- func (c *Context) BindQuery(dest any) error
- func (c *Context) Blob(status int, contentType string, data []byte) error
- func (c *Context) Body() ([]byte, error)
- func (c *Context) Context() context.Context
- func (c *Context) Cookie(name string) (*http.Cookie, error)
- func (c *Context) Error(status int, message string) error
- func (c *Context) ErrorWithDetail(status int, message, detail string) error
- func (c *Context) File(filepath string) error
- func (c *Context) FormFile(name string) (*multipart.FileHeader, error)
- func (c *Context) Get(key string) (any, bool)
- func (c *Context) HTML(status int, html string) error
- func (c *Context) Header(name string) string
- func (c *Context) HeaderValues(name string) []string
- func (c *Context) HookError() error
- func (c *Context) Host() string
- func (c *Context) IsTLS() bool
- func (c *Context) JSON(status int, v any) error
- func (c *Context) JSONPretty(status int, v any) error
- func (c *Context) Logger() *slog.Logger
- func (c *Context) Method() string
- func (c *Context) MustGet(key string) any
- func (c *Context) NoContent(status int) error
- func (c *Context) Param(name string) string
- func (c *Context) ParamInt(name string) (int, error)
- func (c *Context) ParamInt64(name string) (int64, error)
- func (c *Context) ParamUUID(name string) (string, error)
- func (c *Context) Path() string
- func (c *Context) Protocol() string
- func (c *Context) Query(name string) string
- func (c *Context) QueryBool(name string, fallback bool) bool
- func (c *Context) QueryDefault(name, fallback string) string
- func (c *Context) QueryFloat64(name string, fallback float64) float64
- func (c *Context) QueryInt(name string, fallback int) int
- func (c *Context) QueryInt64(name string, fallback int64) int64
- func (c *Context) QueryParams() url.Values
- func (c *Context) QuerySlice(name string) []string
- func (c *Context) RealIP() string
- func (c *Context) Redirect(status int, url string) error
- func (c *Context) Request() *http.Request
- func (c *Context) RequestID() string
- func (c *Context) Response() http.ResponseWriter
- func (c *Context) Scheme() string
- func (c *Context) SendStatus(code int) error
- func (c *Context) Set(key string, value any)
- func (c *Context) SetContext(ctx context.Context)
- func (c *Context) SetContextValue(key, value any)
- func (c *Context) SetCookie(cookie *http.Cookie)
- func (c *Context) SetHeader(name, value string)
- func (c *Context) SetResponse(w http.ResponseWriter)
- func (c *Context) Status(code int) *Context
- func (c *Context) Stream(status int, contentType string, reader io.Reader) error
- func (c *Context) Text(status int, text string) error
- func (c *Context) Written() bool
- func (c *Context) XML(status int, v any) error
- type CustomBinder
- type CustomRuleFunc
- type ErrorHandler
- type HandlerFunc
- type HookFunc
- type HookPhase
- type Middleware
- type MiddlewareFunc
- type OptimizedJSONCodec
- type Option
- func WithBanner(enabled bool) Option
- func WithCodec(codec Codec) Option
- func WithDebug(enabled bool) Option
- func WithDisableHTTP2(disabled bool) Option
- func WithErrorHandler(fn ErrorHandler) Option
- func WithIdleTimeout(d time.Duration) Option
- func WithLogger(logger *slog.Logger) Option
- func WithMaxBodySize(n int64) Option
- func WithMaxHeaderBytes(n int) Option
- func WithReadHeaderTimeout(d time.Duration) Option
- func WithReadTimeout(d time.Duration) Option
- func WithRedirectTrailingSlash(enabled bool) Option
- func WithRequestContextBridge(enabled bool) Option
- func WithShutdownTimeout(d time.Duration) Option
- func WithTLSConfig(cfg *tls.Config) Option
- func WithTrustedProxies(cidrs ...string) Option
- func WithWriteTimeout(d time.Duration) Option
- type ParamParser
- type Plugin
- type PluginContext
- func (pc *PluginContext) AddHook(phase HookPhase, fn HookFunc)
- func (pc *PluginContext) App() *App
- func (pc *PluginContext) Decorate(key string, value any)
- func (pc *PluginContext) Delete(pattern string, handler any, opts ...RouteOption) *PluginContext
- func (pc *PluginContext) Get(pattern string, handler any, opts ...RouteOption) *PluginContext
- func (pc *PluginContext) Group(prefix string, fn func(g *RouteGroup)) *PluginContext
- func (pc *PluginContext) Logger() *slog.Logger
- func (pc *PluginContext) Post(pattern string, handler any, opts ...RouteOption) *PluginContext
- func (pc *PluginContext) Put(pattern string, handler any, opts ...RouteOption) *PluginContext
- func (pc *PluginContext) Register(plugin Plugin) error
- func (pc *PluginContext) Resolve(key string) (any, bool)
- func (pc *PluginContext) Use(middlewares ...Middleware) *PluginContext
- type PluginFunc
- type PluginOption
- type PluginWithDeps
- type RouteGroup
- func (g *RouteGroup) Any(pattern string, handler any, opts ...RouteOption) *RouteGroup
- func (g *RouteGroup) Delete(pattern string, handler any, opts ...RouteOption) *RouteGroup
- func (g *RouteGroup) Get(pattern string, handler any, opts ...RouteOption) *RouteGroup
- func (g *RouteGroup) Group(prefix string, fn func(g *RouteGroup)) *RouteGroup
- func (g *RouteGroup) Head(pattern string, handler any, opts ...RouteOption) *RouteGroup
- func (g *RouteGroup) Options(pattern string, handler any, opts ...RouteOption) *RouteGroup
- func (g *RouteGroup) Patch(pattern string, handler any, opts ...RouteOption) *RouteGroup
- func (g *RouteGroup) Post(pattern string, handler any, opts ...RouteOption) *RouteGroup
- func (g *RouteGroup) Put(pattern string, handler any, opts ...RouteOption) *RouteGroup
- func (g *RouteGroup) Use(middlewares ...Middleware) *RouteGroup
- type RouteInfo
- type RouteOption
- func WithDeprecated() RouteOption
- func WithDescription(desc string) RouteOption
- func WithName(name string) RouteOption
- func WithOperationID(id string) RouteOption
- func WithRouteMaxBodySize(bytes int64) RouteOption
- func WithRouteMiddleware(mw ...Middleware) RouteOption
- func WithSummary(s string) RouteOption
- func WithTags(tags ...string) RouteOption
- type SelfValidator
- type ShutdownHook
- type StdJSONCodec
- type StructLevelFunc
- type StructLevelValidator
- type TestClient
- func (tc *TestClient) Delete(path string) *TestResponse
- func (tc *TestClient) Do(req *http.Request) *TestResponse
- func (tc *TestClient) Get(path string) *TestResponse
- func (tc *TestClient) Patch(path string, body any) *TestResponse
- func (tc *TestClient) Post(path string, body any) *TestResponse
- func (tc *TestClient) Put(path string, body any) *TestResponse
- func (tc *TestClient) WithBearer(token string) *TestClient
- func (tc *TestClient) WithCookie(cookie *http.Cookie) *TestClient
- func (tc *TestClient) WithHeader(key, value string) *TestClient
- func (tc *TestClient) WithQuery(key, value string) *TestClient
- type TestResponse
- type ValidationError
- type ValidationErrors
- type ValidationMessageFunc
Constants ¶
This section is empty.
Variables ¶
var ( // ErrResponseAlreadyWritten is returned when attempting to write to a response // that has already been written to. ErrResponseAlreadyWritten = errors.New("aarv: response already written") )
Sentinel errors for common conditions.
Functions ¶
func RegisterRule ¶
func RegisterRule(name string, fn CustomRuleFunc)
RegisterRule registers a custom validation rule. The rule can then be used in validate tags like: validate:"myrule=param"
func RegisterStructValidation ¶
func RegisterStructValidation(t reflect.Type, fn StructLevelFunc)
RegisterStructValidation registers a struct-level validation function for a specific type.
func SetValidationMessageTemplate ¶ added in v0.4.0
func SetValidationMessageTemplate(tag string, fn ValidationMessageFunc)
SetValidationMessageTemplate overrides the default message for a validation rule. Passing a nil function removes the custom template for the rule.
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is the central framework instance.
func (*App) AcquireContext ¶
AcquireContext gets a Context from the pool. Must be followed by ReleaseContext.
func (*App) AddHookWithPriority ¶
AddHookWithPriority registers a hook with priority (lower = runs first).
func (*App) Any ¶
func (a *App) Any(pattern string, handler any, opts ...RouteOption) *App
Any registers the handler for the common HTTP methods on the same pattern.
func (*App) Delete ¶
func (a *App) Delete(pattern string, handler any, opts ...RouteOption) *App
Delete registers a DELETE route for the given pattern.
func (*App) Get ¶
func (a *App) Get(pattern string, handler any, opts ...RouteOption) *App
Get registers a GET route for the given pattern.
func (*App) Group ¶
func (a *App) Group(prefix string, fn func(g *RouteGroup)) *App
Group creates a route group with a prefix.
func (*App) Head ¶
func (a *App) Head(pattern string, handler any, opts ...RouteOption) *App
Head registers a HEAD route for the given pattern.
func (*App) ListenMutualTLS ¶
ListenMutualTLS starts the server with mutual TLS authentication.
func (*App) OnShutdown ¶
func (a *App) OnShutdown(fn ShutdownHook) *App
OnShutdown registers a shutdown hook.
func (*App) Options ¶
func (a *App) Options(pattern string, handler any, opts ...RouteOption) *App
Options registers an OPTIONS route for the given pattern.
func (*App) Patch ¶
func (a *App) Patch(pattern string, handler any, opts ...RouteOption) *App
Patch registers a PATCH route for the given pattern.
func (*App) Post ¶
func (a *App) Post(pattern string, handler any, opts ...RouteOption) *App
Post registers a POST route for the given pattern.
func (*App) Put ¶
func (a *App) Put(pattern string, handler any, opts ...RouteOption) *App
Put registers a PUT route for the given pattern.
func (*App) Register ¶
func (a *App) Register(plugin Plugin, opts ...PluginOption) *App
Register registers a plugin with the framework.
func (*App) ReleaseContext ¶
ReleaseContext returns a Context to the pool.
func (*App) ServeHTTP ¶
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler for the application.
func (*App) SetMethodNotAllowedHandler ¶
func (a *App) SetMethodNotAllowedHandler(h HandlerFunc) *App
SetMethodNotAllowedHandler replaces the default 405 handler used for method mismatches.
func (*App) SetNotFoundHandler ¶
func (a *App) SetNotFoundHandler(h HandlerFunc) *App
SetNotFoundHandler replaces the default 404 handler used for unmatched routes.
func (*App) Use ¶
func (a *App) Use(middlewares ...Middleware) *App
Use appends middleware to the global middleware chain.
type AppError ¶
type AppError struct {
// contains filtered or unexported fields
}
AppError is a structured HTTP error with status code, machine-readable code, and message.
func ErrBadGateway ¶
ErrBadGateway creates a 502 bad gateway AppError.
func ErrBadRequest ¶
ErrBadRequest creates a 400 bad request AppError.
func ErrConflict ¶
ErrConflict creates a 409 conflict AppError.
func ErrForbidden ¶
ErrForbidden creates a 403 forbidden AppError.
func ErrGatewayTimeout ¶
ErrGatewayTimeout creates a 504 gateway timeout AppError.
func ErrInternal ¶
ErrInternal creates a 500 internal server error AppError.
func ErrMethodNotAllowed ¶
ErrMethodNotAllowed creates a 405 method not allowed AppError.
func ErrNotFound ¶
ErrNotFound creates a 404 not found AppError.
func ErrServiceUnavailable ¶
ErrServiceUnavailable creates a 503 service unavailable AppError.
func ErrTooManyRequests ¶
ErrTooManyRequests creates a 429 rate limit AppError.
func ErrUnauthorized ¶
ErrUnauthorized creates a 401 unauthorized AppError.
func ErrUnprocessable ¶
ErrUnprocessable creates a 422 validation failure AppError.
func (*AppError) StatusCode ¶
StatusCode returns the HTTP status code associated with the error.
func (*AppError) WithDetail ¶
WithDetail sets a detail string on the error and returns the same value.
func (*AppError) WithInternal ¶
WithInternal attaches an internal error that is not serialized to clients.
type BindError ¶
type BindError struct {
// Err is the underlying binding failure.
Err error
// Source identifies the binding stage that failed, such as "binding" or "body".
Source string
}
BindError represents an error during request binding.
type Codec ¶
type Codec interface {
// Decode reads from r and unmarshals into v (stream-based, for request bodies).
Decode(r io.Reader, v any) error
// Encode marshals v and writes to w (stream-based, for response bodies).
Encode(w io.Writer, v any) error
// UnmarshalBytes unmarshals pre-read bytes into v (for cached/small payloads).
UnmarshalBytes(data []byte, v any) error
// MarshalBytes marshals v into bytes (for pre-serialization).
MarshalBytes(v any) ([]byte, error)
// ContentType returns the MIME type for this codec.
ContentType() string
}
Codec defines the interface for encoding/decoding request and response bodies.
type Config ¶
type Config struct {
// ReadTimeout is the maximum duration for reading the entire request.
ReadTimeout time.Duration
// ReadHeaderTimeout is the maximum duration for reading request headers.
ReadHeaderTimeout time.Duration
// WriteTimeout is the maximum duration before timing out response writes.
WriteTimeout time.Duration
// IdleTimeout is the maximum keep-alive idle time between requests.
IdleTimeout time.Duration
// ShutdownTimeout is the maximum time allowed for graceful shutdown.
ShutdownTimeout time.Duration
// MaxHeaderBytes limits the size of incoming request headers.
MaxHeaderBytes int
// MaxBodySize is the default request body limit applied by the framework.
MaxBodySize int64
// TLSConfig provides the TLS configuration used by HTTPS listeners.
TLSConfig *tls.Config
// TrustedProxies contains CIDRs or IPs whose forwarding headers are trusted.
TrustedProxies []string
// DisableHTTP2 forces HTTPS listeners to serve HTTP/1.1 only.
DisableHTTP2 bool
// Banner controls whether the startup banner is printed.
Banner bool
// Debug enables framework-level debug behavior where supported.
Debug bool
// RedirectTrailingSlash enables redirects between slash and non-slash route variants.
RedirectTrailingSlash bool
// RequestContextBridge clones requests to keep Aarv Context available through
// raw r.WithContext(...) middleware chains. Disable only for fully opt-in
// performance-sensitive stacks that do not need that compatibility.
RequestContextBridge bool
}
Config holds server configuration values.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context wraps the http.Request and http.ResponseWriter with convenience helpers. It is pooled via sync.Pool — do NOT hold references to it beyond the handler lifetime.
func FromRequest ¶
FromRequest extracts the aarv Context from an http.Request. This is exported so sub-packages (plugins) can access the aarv Context.
func (*Context) Attachment ¶
Attachment sends a file as an attachment (download).
func (*Context) BindQuery ¶
BindQuery decodes query parameters into a struct using the query struct tag.
func (*Context) Body ¶
Body reads and caches the request body bytes. Subsequent calls return cached data.
func (*Context) ErrorWithDetail ¶
ErrorWithDetail returns an AppError with a detail string.
func (*Context) FormFile ¶
func (c *Context) FormFile(name string) (*multipart.FileHeader, error)
FormFile returns the first file for the given form key.
func (*Context) HeaderValues ¶
HeaderValues returns all values for a request header.
func (*Context) HookError ¶ added in v0.4.0
HookError returns the currently propagated lifecycle error for OnError hooks. It is only set while the framework is invoking OnError.
func (*Context) JSON ¶
JSON serializes v as JSON and writes it with the given status code. If status is 0, uses the status set by Status() or defaults to 200.
func (*Context) JSONPretty ¶
JSONPretty serializes v as indented JSON.
func (*Context) Logger ¶
Logger returns a request-scoped logger with the request ID attached (cached).
func (*Context) ParamInt64 ¶
ParamInt64 returns a path parameter parsed as int64.
func (*Context) QueryDefault ¶
QueryDefault returns a query parameter or a fallback value.
func (*Context) QueryFloat64 ¶
QueryFloat64 returns a query parameter parsed as float64, with a fallback.
func (*Context) QueryInt64 ¶
QueryInt64 returns a query parameter parsed as int64, with a fallback.
func (*Context) QueryParams ¶
QueryParams returns all query parameters.
func (*Context) QuerySlice ¶
QuerySlice returns all values for a query parameter.
func (*Context) RealIP ¶
RealIP extracts the client IP, respecting X-Real-IP and X-Forwarded-For headers. It only trusts proxy headers if the direct client IP is in the TrustedProxies list.
func (*Context) RequestID ¶
RequestID returns the request ID from the store (set by RequestID middleware).
func (*Context) Response ¶
func (c *Context) Response() http.ResponseWriter
Response returns the underlying http.ResponseWriter.
func (*Context) SendStatus ¶
SendStatus writes only the status code with no body (alias for NoContent).
func (*Context) SetContext ¶
SetContext replaces the request's context.
func (*Context) SetContextValue ¶ added in v0.4.0
SetContextValue stores a value in the request context while preserving Aarv's own context marker in a single wrapped context object.
func (*Context) SetResponse ¶ added in v0.4.0
func (c *Context) SetResponse(w http.ResponseWriter)
SetResponse replaces the underlying response writer.
func (*Context) Status ¶
Status sets the response status code for chaining. Use with JSON, Text, etc. to set status before writing. Example: c.Status(201).JSON(data)
func (*Context) Stream ¶
Stream copies from reader directly to the response writer. It bypasses the buffered response writer for streaming responses.
type CustomBinder ¶
CustomBinder is an interface for types that can bind themselves from the request context. If a type implements this interface, BindFromContext is called instead of the default binding.
type CustomRuleFunc ¶
CustomRuleFunc is a function signature for custom validation rules. The function receives the field value and the rule parameter, and returns true if valid.
type ErrorHandler ¶
ErrorHandler is a function that handles errors returned by handlers.
type HandlerFunc ¶
HandlerFunc is the framework's handler signature — every handler converts to this.
func Adapt ¶
func Adapt(fn func(http.ResponseWriter, *http.Request)) HandlerFunc
Adapt wraps a stdlib http.HandlerFunc as a framework HandlerFunc.
func Bind ¶
func Bind[Req any, Res any](fn func(*Context, Req) (Res, error)) HandlerFunc
Bind creates a typed handler with automatic request parsing and response serialization. At registration time, it pre-computes the binder and validator for Req. At request time, it decodes the request into Req, validates it, calls the handler, and serializes the Res as JSON.
type HookPhase ¶
type HookPhase int
HookPhase represents a phase in the request lifecycle.
const ( // OnRequest runs immediately after the framework acquires the request context. OnRequest HookPhase = iota // PreRouting is reserved for hooks that should run before route matching. PreRouting // PreParsing is reserved for hooks that should run before body parsing. PreParsing // PreValidation is reserved for hooks that should run after parsing and before validation. PreValidation // PreHandler is reserved for hooks that should run after validation and before the handler. PreHandler // OnResponse runs after the handler completes and before the final response lifecycle ends. OnResponse // OnSend runs just before buffered response bytes are flushed to the client. OnSend // OnError runs when the framework handles an error returned from the chain. OnError // OnStartup runs before the server begins accepting requests. OnStartup // OnShutdown runs when graceful shutdown starts. OnShutdown )
type Middleware ¶
Middleware is the standard Go middleware signature.
func RegisterNativeMiddleware ¶ added in v0.4.0
func RegisterNativeMiddleware(m Middleware, fn MiddlewareFunc) Middleware
RegisterNativeMiddleware associates an Aarv-native middleware implementation with a standard net/http middleware. It enables the runtime to use a faster native exact-route path when every middleware in the chain provides one.
func WrapMiddleware ¶
func WrapMiddleware(fn MiddlewareFunc) Middleware
WrapMiddleware converts a framework MiddlewareFunc to a stdlib Middleware.
type MiddlewareFunc ¶
type MiddlewareFunc func(next HandlerFunc) HandlerFunc
MiddlewareFunc is a framework-specific middleware with access to Context.
type OptimizedJSONCodec ¶
type OptimizedJSONCodec = StdJSONCodec
OptimizedJSONCodec is kept as a deprecated alias for backward compatibility. Deprecated: use StdJSONCodec or NewStdJSONCodec.
func NewOptimizedJSONCodec ¶
func NewOptimizedJSONCodec() *OptimizedJSONCodec
NewOptimizedJSONCodec is kept as a deprecated constructor alias. Deprecated: use NewStdJSONCodec.
type Option ¶
type Option func(*App)
Option configures the App.
func WithBanner ¶
WithBanner enables or disables the startup banner.
func WithDisableHTTP2 ¶
WithDisableHTTP2 disables HTTP/2.
func WithErrorHandler ¶
func WithErrorHandler(fn ErrorHandler) Option
WithErrorHandler sets a custom error handler.
func WithIdleTimeout ¶
WithIdleTimeout sets the server idle timeout.
func WithLogger ¶
WithLogger sets the structured logger.
func WithMaxBodySize ¶
WithMaxBodySize sets the global max body size.
func WithMaxHeaderBytes ¶
WithMaxHeaderBytes sets the max header bytes.
func WithReadHeaderTimeout ¶
WithReadHeaderTimeout sets the server read header timeout.
func WithReadTimeout ¶
WithReadTimeout sets the server read timeout.
func WithRedirectTrailingSlash ¶
WithRedirectTrailingSlash enables trailing slash redirect.
func WithRequestContextBridge ¶ added in v0.4.0
WithRequestContextBridge controls whether Aarv clones requests to keep the framework Context attached through raw r.WithContext(...) middleware chains. Disable it only for middleware stacks that never rely on aarv.FromRequest(...) after cloning requests, in exchange for a slightly cheaper hot path.
func WithShutdownTimeout ¶
WithShutdownTimeout sets the graceful shutdown timeout.
func WithTLSConfig ¶
WithTLSConfig sets a custom TLS configuration.
func WithTrustedProxies ¶
WithTrustedProxies sets the trusted proxy CIDRs for IP extraction.
func WithWriteTimeout ¶
WithWriteTimeout sets the server write timeout.
type ParamParser ¶
ParamParser is an interface for types that can parse themselves from a string parameter. This allows custom types to be used as path parameters, query params, etc.
type Plugin ¶
type Plugin interface {
Name() string
Version() string
Register(app *PluginContext) error
}
Plugin is the interface for framework plugins. All plugins must implement Name(), Version(), and Register().
type PluginContext ¶
type PluginContext struct {
// contains filtered or unexported fields
}
PluginContext is a scoped view of the App given to plugins during registration.
func (*PluginContext) AddHook ¶
func (pc *PluginContext) AddHook(phase HookPhase, fn HookFunc)
AddHook adds a lifecycle hook from this plugin.
func (*PluginContext) App ¶
func (pc *PluginContext) App() *App
App returns the underlying App (for advanced use).
func (*PluginContext) Decorate ¶
func (pc *PluginContext) Decorate(key string, value any)
Decorate registers a shared service by key.
func (*PluginContext) Delete ¶
func (pc *PluginContext) Delete(pattern string, handler any, opts ...RouteOption) *PluginContext
Delete registers a DELETE route scoped to this plugin.
func (*PluginContext) Get ¶
func (pc *PluginContext) Get(pattern string, handler any, opts ...RouteOption) *PluginContext
Get registers a GET route scoped to this plugin.
func (*PluginContext) Group ¶
func (pc *PluginContext) Group(prefix string, fn func(g *RouteGroup)) *PluginContext
Group creates a nested route group within the plugin.
func (*PluginContext) Logger ¶
func (pc *PluginContext) Logger() *slog.Logger
Logger returns the plugin-scoped logger.
func (*PluginContext) Post ¶
func (pc *PluginContext) Post(pattern string, handler any, opts ...RouteOption) *PluginContext
Post registers a POST route scoped to this plugin.
func (*PluginContext) Put ¶
func (pc *PluginContext) Put(pattern string, handler any, opts ...RouteOption) *PluginContext
Put registers a PUT route scoped to this plugin.
func (*PluginContext) Register ¶
func (pc *PluginContext) Register(plugin Plugin) error
Register registers a nested plugin.
func (*PluginContext) Resolve ¶
func (pc *PluginContext) Resolve(key string) (any, bool)
Resolve retrieves a decorated service by key.
func (*PluginContext) Use ¶
func (pc *PluginContext) Use(middlewares ...Middleware) *PluginContext
Use adds middleware scoped to this plugin's routes.
type PluginFunc ¶
type PluginFunc func(app *PluginContext) error
PluginFunc adapts a function to the Plugin interface.
func (PluginFunc) Name ¶
func (f PluginFunc) Name() string
Name returns the default plugin name for a PluginFunc adapter.
func (PluginFunc) Register ¶
func (f PluginFunc) Register(app *PluginContext) error
Register invokes the adapted plugin function.
func (PluginFunc) Version ¶
func (f PluginFunc) Version() string
Version returns the default plugin version for a PluginFunc adapter.
type PluginOption ¶
type PluginOption func(*string)
PluginOption configures plugin registration.
func WithPrefix ¶
func WithPrefix(prefix string) PluginOption
WithPrefix sets the plugin's route prefix.
type PluginWithDeps ¶
PluginWithDeps is an optional interface for plugins that declare dependencies.
type RouteGroup ¶
type RouteGroup struct {
// contains filtered or unexported fields
}
RouteGroup represents a group of routes with a common prefix and middleware.
func (*RouteGroup) Any ¶
func (g *RouteGroup) Any(pattern string, handler any, opts ...RouteOption) *RouteGroup
Any registers the handler for the common HTTP methods within the group.
func (*RouteGroup) Delete ¶
func (g *RouteGroup) Delete(pattern string, handler any, opts ...RouteOption) *RouteGroup
Delete registers a DELETE route within the group.
func (*RouteGroup) Get ¶
func (g *RouteGroup) Get(pattern string, handler any, opts ...RouteOption) *RouteGroup
Get registers a GET route within the group.
func (*RouteGroup) Group ¶
func (g *RouteGroup) Group(prefix string, fn func(g *RouteGroup)) *RouteGroup
Group creates a nested route group under the current group's prefix.
func (*RouteGroup) Head ¶
func (g *RouteGroup) Head(pattern string, handler any, opts ...RouteOption) *RouteGroup
Head registers a HEAD route within the group.
func (*RouteGroup) Options ¶
func (g *RouteGroup) Options(pattern string, handler any, opts ...RouteOption) *RouteGroup
Options registers an OPTIONS route within the group.
func (*RouteGroup) Patch ¶
func (g *RouteGroup) Patch(pattern string, handler any, opts ...RouteOption) *RouteGroup
Patch registers a PATCH route within the group.
func (*RouteGroup) Post ¶
func (g *RouteGroup) Post(pattern string, handler any, opts ...RouteOption) *RouteGroup
Post registers a POST route within the group.
func (*RouteGroup) Put ¶
func (g *RouteGroup) Put(pattern string, handler any, opts ...RouteOption) *RouteGroup
Put registers a PUT route within the group.
func (*RouteGroup) Use ¶
func (g *RouteGroup) Use(middlewares ...Middleware) *RouteGroup
Use appends middleware scoped to routes registered on this group.
type RouteInfo ¶
type RouteInfo struct {
// Method is the HTTP method registered for the route.
Method string `json:"method"`
// Pattern is the path pattern registered for the route.
Pattern string `json:"pattern"`
// Name is the optional application-defined route name.
Name string `json:"name,omitempty"`
// Tags contains optional route classification tags.
Tags []string `json:"tags,omitempty"`
// Description is the optional long-form route description.
Description string `json:"description,omitempty"`
// Deprecated reports whether the route is marked deprecated.
Deprecated bool `json:"deprecated,omitempty"`
}
RouteInfo describes a registered route for introspection.
type RouteOption ¶
type RouteOption func(*routeConfig)
RouteOption configures per-route metadata and behavior.
func WithDeprecated ¶
func WithDeprecated() RouteOption
WithDeprecated marks the route as deprecated in route metadata.
func WithDescription ¶
func WithDescription(desc string) RouteOption
WithDescription sets the long-form description for the route.
func WithName ¶
func WithName(name string) RouteOption
WithName sets a stable human-readable name for the route.
func WithOperationID ¶
func WithOperationID(id string) RouteOption
WithOperationID sets a machine-readable identifier for the route.
func WithRouteMaxBodySize ¶
func WithRouteMaxBodySize(bytes int64) RouteOption
WithRouteMaxBodySize overrides the global body limit for this route.
func WithRouteMiddleware ¶
func WithRouteMiddleware(mw ...Middleware) RouteOption
WithRouteMiddleware attaches middleware that runs only for this route.
func WithSummary ¶
func WithSummary(s string) RouteOption
WithSummary sets a short summary for the route.
func WithTags ¶
func WithTags(tags ...string) RouteOption
WithTags associates one or more tags with the route.
type SelfValidator ¶
type SelfValidator interface {
Validate() []ValidationError
}
SelfValidator can be implemented by types to provide custom validation.
type ShutdownHook ¶
type ShutdownHook func(ctx interface{ Done() <-chan struct{} }) error
ShutdownHook runs during graceful shutdown with the shutdown context.
type StdJSONCodec ¶
type StdJSONCodec struct{}
StdJSONCodec implements the canonical stdlib-backed JSON codec. It uses encoding/json and a pooled buffer for MarshalBytes.
func NewStdJSONCodec ¶ added in v0.4.0
func NewStdJSONCodec() *StdJSONCodec
NewStdJSONCodec returns the canonical stdlib-backed JSON codec.
func (StdJSONCodec) ContentType ¶
func (StdJSONCodec) ContentType() string
ContentType returns the MIME type produced by StdJSONCodec.
func (StdJSONCodec) Decode ¶
func (StdJSONCodec) Decode(r io.Reader, v any) error
Decode reads JSON from r into v using encoding/json.
func (StdJSONCodec) Encode ¶
func (StdJSONCodec) Encode(w io.Writer, v any) error
Encode writes v to w as JSON using encoding/json.
func (StdJSONCodec) MarshalBytes ¶
func (StdJSONCodec) MarshalBytes(v any) ([]byte, error)
MarshalBytes encodes v to JSON bytes using a pooled buffer.
func (StdJSONCodec) UnmarshalBytes ¶
func (StdJSONCodec) UnmarshalBytes(data []byte, v any) error
UnmarshalBytes decodes JSON bytes into v using encoding/json.
type StructLevelFunc ¶
type StructLevelFunc func(v any) []ValidationError
StructLevelFunc is a function that performs struct-level validation.
type StructLevelValidator ¶
type StructLevelValidator interface {
ValidateStruct() []ValidationError
}
StructLevelValidator can be implemented by types to provide struct-level validation that runs after field validation.
type TestClient ¶
type TestClient struct {
// contains filtered or unexported fields
}
TestClient provides a fluent API for testing aarv handlers without starting a server.
func NewTestClient ¶
func NewTestClient(app *App) *TestClient
NewTestClient creates a TestClient for the given App.
func (*TestClient) Delete ¶
func (tc *TestClient) Delete(path string) *TestResponse
Delete sends a DELETE request.
func (*TestClient) Do ¶
func (tc *TestClient) Do(req *http.Request) *TestResponse
Do sends a custom http.Request.
func (*TestClient) Get ¶
func (tc *TestClient) Get(path string) *TestResponse
Get sends a GET request.
func (*TestClient) Patch ¶
func (tc *TestClient) Patch(path string, body any) *TestResponse
Patch sends a PATCH request with a JSON body.
func (*TestClient) Post ¶
func (tc *TestClient) Post(path string, body any) *TestResponse
Post sends a POST request with a JSON body.
func (*TestClient) Put ¶
func (tc *TestClient) Put(path string, body any) *TestResponse
Put sends a PUT request with a JSON body.
func (*TestClient) WithBearer ¶
func (tc *TestClient) WithBearer(token string) *TestClient
WithBearer adds an Authorization: Bearer header.
func (*TestClient) WithCookie ¶
func (tc *TestClient) WithCookie(cookie *http.Cookie) *TestClient
WithCookie adds a cookie to the test request.
func (*TestClient) WithHeader ¶
func (tc *TestClient) WithHeader(key, value string) *TestClient
WithHeader adds a header to the test request.
func (*TestClient) WithQuery ¶
func (tc *TestClient) WithQuery(key, value string) *TestClient
WithQuery adds a query parameter.
type TestResponse ¶
type TestResponse struct {
// Status is the recorded HTTP status code.
Status int
// Headers contains the recorded response headers.
Headers http.Header
// Body contains the recorded response body bytes.
Body []byte
}
TestResponse wraps an httptest.ResponseRecorder result.
func (*TestResponse) AssertStatus ¶
func (tr *TestResponse) AssertStatus(t *testing.T, expected int)
AssertStatus checks that the response status matches expected.
func (*TestResponse) JSON ¶
func (tr *TestResponse) JSON(dest any) error
JSON unmarshals the response body into dest.
func (*TestResponse) Text ¶
func (tr *TestResponse) Text() string
Text returns the body as a string.
type ValidationError ¶
type ValidationError struct {
// Field is the field or parameter name that failed validation.
Field string `json:"field"`
// Tag is the validation rule that failed.
Tag string `json:"tag"`
// Param is the optional validation rule parameter.
Param string `json:"param,omitempty"`
// Value is the rejected value when it is safe to include.
Value any `json:"value,omitempty"`
// Message is the human-readable validation message.
Message string `json:"message"`
}
ValidationError represents a single field validation failure.
type ValidationErrors ¶
type ValidationErrors struct {
// Errors contains the individual validation failures.
Errors []ValidationError `json:"details"`
}
ValidationErrors is a collection of validation failures.
func (*ValidationErrors) Error ¶
func (e *ValidationErrors) Error() string
Error returns a summary string for the validation failure set.
type ValidationMessageFunc ¶ added in v0.4.0
ValidationMessageFunc customizes the human-readable message for a failed rule.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
codec
|
|
|
segmentio
module
|
|
|
examples
|
|
|
auth
command
Example: Authentication patterns — JWT, API keys, and session-based auth.
|
Example: Authentication patterns — JWT, API keys, and session-based auth. |
|
binding
command
|
|
|
codec
command
Example: Pluggable Codec — demonstrates the pluggable Codec interface.
|
Example: Pluggable Codec — demonstrates the pluggable Codec interface. |
|
custom-middleware
command
|
|
|
custom-plugin
command
Example: Writing a custom plugin — demonstrates the Plugin interface, PluginContext methods, lifecycle hooks, decorated services, and scoped route registration.
|
Example: Writing a custom plugin — demonstrates the Plugin interface, PluginContext methods, lifecycle hooks, decorated services, and scoped route registration. |
|
database
command
Example: Database integration patterns — demonstrates common patterns for integrating databases with aarv, using an in-memory store that mimics real database patterns (connection pooling, transactions, etc.).
|
Example: Database integration patterns — demonstrates common patterns for integrating databases with aarv, using an in-memory store that mimics real database patterns (connection pooling, transactions, etc.). |
|
encrypt
command
Example: AES-GCM Encrypted API
|
Example: AES-GCM Encrypted API |
|
error-handling
command
|
|
|
fileserver
command
Example: File upload, download, and static file serving.
|
Example: File upload, download, and static file serving. |
|
hello
command
|
|
|
hooks
command
Example: Lifecycle hooks — demonstrates all hook phases, priorities, shutdown hooks, and OnError handling.
|
Example: Lifecycle hooks — demonstrates all hook phases, priorities, shutdown hooks, and OnError handling. |
|
json-logger
command
Example: JSON Structured Logging with Full Request/Response Dump
|
Example: JSON Structured Logging with Full Request/Response Dump |
|
middleware-bridge
command
|
|
|
middleware-chain
command
Example: Middleware chain showcasing all built-in plugins.
|
Example: Middleware chain showcasing all built-in plugins. |
|
rest-crud
command
Example: REST CRUD API with typed handlers, validation, path params, query binding, error handling, route groups, and middleware.
|
Example: REST CRUD API with typed handlers, validation, path params, query binding, error handling, route groups, and middleware. |
|
route-groups
command
|
|
|
streaming
command
Example: Server-Sent Events (SSE) and streaming responses.
|
Example: Server-Sent Events (SSE) and streaming responses. |
|
internal
|
|
|
plugins
|
|
|
bodylimit
Package bodylimit provides request body size limiting middleware for the aarv framework.
|
Package bodylimit provides request body size limiting middleware for the aarv framework. |
|
compress
Package compress provides gzip and deflate compression middleware for the aarv framework.
|
Package compress provides gzip and deflate compression middleware for the aarv framework. |
|
cors
Package cors provides Cross-Origin Resource Sharing (CORS) middleware for the aarv framework.
|
Package cors provides Cross-Origin Resource Sharing (CORS) middleware for the aarv framework. |
|
encrypt
Package encrypt provides AES-GCM encryption middleware for the aarv framework.
|
Package encrypt provides AES-GCM encryption middleware for the aarv framework. |
|
etag
Package etag provides ETag middleware for the aarv framework.
|
Package etag provides ETag middleware for the aarv framework. |
|
health
Package health provides health check endpoint middleware for the aarv framework.
|
Package health provides health check endpoint middleware for the aarv framework. |
|
logger
Package logger provides request logging middleware for the aarv framework.
|
Package logger provides request logging middleware for the aarv framework. |
|
recover
Package recover provides panic recovery middleware for the aarv framework.
|
Package recover provides panic recovery middleware for the aarv framework. |
|
requestid
Package requestid provides request ID middleware for the aarv framework.
|
Package requestid provides request ID middleware for the aarv framework. |
|
secure
Package secure provides security headers middleware for the aarv framework.
|
Package secure provides security headers middleware for the aarv framework. |
|
static
Package static provides static file serving middleware for the aarv framework.
|
Package static provides static file serving middleware for the aarv framework. |
|
timeout
Package timeout provides request timeout middleware for the aarv framework.
|
Package timeout provides request timeout middleware for the aarv framework. |
|
verboselog
Package verboselog provides full request/response logging middleware for the aarv framework.
|
Package verboselog provides full request/response logging middleware for the aarv framework. |