Documentation
¶
Index ¶
- Constants
- Variables
- func ConfigFromEnv(prefix string, base config.Config) config.Config
- func ConfigFromFile(path string, base config.Config) (config.Config, error)
- func DefaultConfig() config.Config
- func InjectRequestMetadata(r *http.Request, metadata RequestMetadata)
- func InjectRequestMetadataIfMissing(r *http.Request, metadata RequestMetadata)
- func LoadConfig(path, envPrefix string) (config.Config, error)
- func LoadConfigProfile(profile config.Profile) (config.Config, error)
- func NewRequestID() string
- func RequestIDFromHeader(r *http.Request) string
- func SetPrincipal(ctx *Context, principal *Principal)
- func TraceIDs(traceparent string) (string, string, bool)
- func ValidateConfig(cfg config.Config) error
- func WithRequestMetadata(ctx context.Context, metadata RequestMetadata) context.Context
- type App
- func (a *App) AddOpenAPIRoutes(builder *openapi.Builder, options ...OpenAPIOption) error
- func (a *App) AuthHooks() AuthHooks
- func (a *App) DELETE(path string, handler Handler, middleware ...Middleware)
- func (a *App) GET(path string, handler Handler, middleware ...Middleware)
- func (a *App) Group(prefix string, middleware ...Middleware) *Group
- func (a *App) HEAD(path string, handler Handler, middleware ...Middleware)
- func (a *App) Handle(method, path string, handler Handler, middleware ...Middleware)
- func (a *App) ListenAndServe() error
- func (a *App) ListenAndServeTLS(certFile, keyFile string) error
- func (a *App) PATCH(path string, handler Handler, middleware ...Middleware)
- func (a *App) POST(path string, handler Handler, middleware ...Middleware)
- func (a *App) PUT(path string, handler Handler, middleware ...Middleware)
- func (a *App) Path(name string, params map[string]string) (string, bool)
- func (a *App) PathWithQuery(name string, params map[string]string, query map[string]string) (string, bool)
- func (a *App) Registry() *Registry
- func (a *App) Route(method, path string, handler Handler, options ...RouteOption)
- func (a *App) RouteInfo(name string) (RouteInfo, bool)
- func (a *App) Routes() []RouteInfo
- func (a *App) RoutesAll() []RouteInfo
- func (a *App) Run(ctx context.Context) error
- func (a *App) RunWithSignals() error
- func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (a *App) SetAuthHooks(hooks AuthHooks)
- func (a *App) ShutdownTimeout() time.Duration
- func (a *App) Static(prefix, dir string, options ...StaticOption)
- func (a *App) StaticFS(prefix string, fsys fs.FS, options ...StaticOption)
- func (a *App) Use(middleware ...Middleware)
- func (a *App) UsePre(middleware ...PreMiddleware)
- func (a *App) Version(version string, middleware ...Middleware) *Group
- type AuthHooks
- type Authenticator
- type AuthenticatorFactory
- type Authorizer
- type CacheFactory
- type Config
- type ConfigProfile
- type Context
- func (c *Context) App() *App
- func (c *Context) AuthHooks() AuthHooks
- func (c *Context) BindForm(dst any) error
- func (c *Context) BindJSON(dst any) error
- func (c *Context) BindMultipart(dst any, maxMemory int64) error
- func (c *Context) FormFile(name string, maxMemory int64) (*multipart.FileHeader, error)
- func (c *Context) Get(key string) (any, bool)
- func (c *Context) HTML(status int, name string, data any) error
- func (c *Context) JSON(status int, payload any) error
- func (c *Context) Logger() Logger
- func (c *Context) Param(name string) string
- func (c *Context) ParamBool(name string) (bool, error)
- func (c *Context) ParamInt(name string) (int, error)
- func (c *Context) ParamInt64(name string) (int64, error)
- func (c *Context) Query(name string) string
- func (c *Context) QueryBool(name string) (bool, error)
- func (c *Context) QueryInt(name string) (int, error)
- func (c *Context) QueryInt64(name string) (int64, error)
- func (c *Context) Render(status int, fn render.RenderFunc) error
- func (c *Context) RequestID() string
- func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string) error
- func (c *Context) Set(key string, value any)
- func (c *Context) Text(status int, message string) error
- type ErrorEnvelope
- type ErrorHandler
- type ErrorPageData
- type Group
- func (g *Group) DELETE(path string, handler Handler, middleware ...Middleware)
- func (g *Group) GET(path string, handler Handler, middleware ...Middleware)
- func (g *Group) Group(prefix string, middleware ...Middleware) *Group
- func (g *Group) HEAD(path string, handler Handler, middleware ...Middleware)
- func (g *Group) Handle(method, path string, handler Handler, middleware ...Middleware)
- func (g *Group) PATCH(path string, handler Handler, middleware ...Middleware)
- func (g *Group) POST(path string, handler Handler, middleware ...Middleware)
- func (g *Group) PUT(path string, handler Handler, middleware ...Middleware)
- func (g *Group) Route(method, path string, handler Handler, options ...RouteOption)
- type Handler
- type Logger
- type Middleware
- type MiddlewareFactory
- type OpenAPIOption
- type OpenAPIOptions
- type Option
- func WithAuthHooks(hooks AuthHooks) Option
- func WithConfig(cfg config.Config) Option
- func WithErrorHandler(handler ErrorHandler) Option
- func WithErrorTemplates(templates map[int]string) Option
- func WithLogger(logger *slog.Logger) Option
- func WithRegistry(registry *Registry) Option
- func WithRenderer(engine *render.Engine) Option
- func WithTemplateFS(fsys fs.FS, dir string) Option
- func WithTemplateFSDevDir(dir string) Option
- func WithTemplateFuncs(funcs render.FuncMap) Option
- func WithTemplatePartials(patterns ...string) Option
- func WithTemplateReload(enabled bool) Option
- func WithTemplateSubdirs(enabled bool) Option
- type Params
- type Plugin
- type PreMiddleware
- type Principal
- type Registry
- func (r *Registry) Authenticator(name string, config map[string]any) (Authenticator, error)
- func (r *Registry) Cache(name string, config map[string]any) (cache.Store, error)
- func (r *Registry) Middleware(name string, config map[string]any) (Middleware, error)
- func (r *Registry) RegisterAuthenticator(name string, factory AuthenticatorFactory) error
- func (r *Registry) RegisterCache(name string, factory CacheFactory) error
- func (r *Registry) RegisterMiddleware(name string, factory MiddlewareFactory) error
- func (r *Registry) RegisterValidator(name string, fn validate.ValidatorFunc) error
- func (r *Registry) Use(plugin Plugin) error
- func (r *Registry) Validator(name string) (validate.ValidatorFunc, error)
- type RequestMetadata
- type RouteInfo
- type RouteOption
- type StaticOption
Constants ¶
const ( TraceparentHeader = "traceparent" TracestateHeader = "tracestate" )
const DefaultMultipartMemory int64 = 32 << 20
const RequestIDHeader = "X-Request-ID"
Variables ¶
var ( // ErrRegistryNil indicates the registry is nil. ErrRegistryNil = errors.New("registry is nil") // ErrRegistryNameRequired indicates a missing registry name. ErrRegistryNameRequired = errors.New("registry name is required") // ErrRegistryExists indicates a registry entry already exists. ErrRegistryExists = errors.New("registry entry already exists") // ErrRegistryNotFound indicates a registry entry was not found. ErrRegistryNotFound = errors.New("registry entry not found") )
Functions ¶
func ConfigFromEnv ¶
ConfigFromEnv applies environment overrides using the given prefix.
func ConfigFromFile ¶
ConfigFromFile loads config from a JSON file onto the base config.
func DefaultConfig ¶
DefaultConfig returns default config values.
func InjectRequestMetadata ¶
func InjectRequestMetadata(r *http.Request, metadata RequestMetadata)
InjectRequestMetadata sets request metadata headers.
func InjectRequestMetadataIfMissing ¶
func InjectRequestMetadataIfMissing(r *http.Request, metadata RequestMetadata)
InjectRequestMetadataIfMissing sets request metadata headers only when missing.
func LoadConfig ¶
LoadConfig loads config from file and applies env overrides.
func LoadConfigProfile ¶
LoadConfigProfile loads config from base/env/secrets profiles with validation.
func RequestIDFromHeader ¶
RequestIDFromHeader returns the request id from headers.
func SetPrincipal ¶
SetPrincipal stores the principal in context storage.
func ValidateConfig ¶
ValidateConfig validates configuration values.
func WithRequestMetadata ¶
func WithRequestMetadata(ctx context.Context, metadata RequestMetadata) context.Context
WithRequestMetadata stores metadata in a context.
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App is the main framework entrypoint.
func (*App) AddOpenAPIRoutes ¶
func (a *App) AddOpenAPIRoutes(builder *openapi.Builder, options ...OpenAPIOption) error
AddOpenAPIRoutes derives OpenAPI operations from registered routes.
func (*App) DELETE ¶
func (a *App) DELETE(path string, handler Handler, middleware ...Middleware)
DELETE registers a DELETE route.
func (*App) GET ¶
func (a *App) GET(path string, handler Handler, middleware ...Middleware)
GET registers a GET route.
func (*App) Group ¶
func (a *App) Group(prefix string, middleware ...Middleware) *Group
Group creates a new route group.
func (*App) Handle ¶
func (a *App) Handle(method, path string, handler Handler, middleware ...Middleware)
Handle registers a route for an arbitrary method.
func (*App) ListenAndServe ¶
ListenAndServe starts the HTTP server using config values.
func (*App) ListenAndServeTLS ¶
ListenAndServeTLS starts the HTTPS server using config values.
func (*App) PATCH ¶
func (a *App) PATCH(path string, handler Handler, middleware ...Middleware)
PATCH registers a PATCH route.
func (*App) POST ¶
func (a *App) POST(path string, handler Handler, middleware ...Middleware)
POST registers a POST route.
func (*App) PUT ¶
func (a *App) PUT(path string, handler Handler, middleware ...Middleware)
PUT registers a PUT route.
func (*App) PathWithQuery ¶
func (a *App) PathWithQuery(name string, params map[string]string, query map[string]string) (string, bool)
PathWithQuery builds a URL path from a named route and attaches query params.
func (*App) Route ¶
func (a *App) Route(method, path string, handler Handler, options ...RouteOption)
Route registers a route with options.
func (*App) RunWithSignals ¶
RunWithSignals starts the server and handles SIGINT/SIGTERM for shutdown.
func (*App) ServeHTTP ¶
func (a *App) ServeHTTP(w http.ResponseWriter, r *http.Request)
ServeHTTP implements http.Handler.
func (*App) SetAuthHooks ¶
SetAuthHooks updates the auth hooks.
func (*App) ShutdownTimeout ¶
ShutdownTimeout returns the configured graceful shutdown timeout.
func (*App) Static ¶
func (a *App) Static(prefix, dir string, options ...StaticOption)
Static registers a static file route.
func (*App) StaticFS ¶
func (a *App) StaticFS(prefix string, fsys fs.FS, options ...StaticOption)
StaticFS registers a static file route from an fs.FS.
func (*App) UsePre ¶
func (a *App) UsePre(middleware ...PreMiddleware)
UsePre registers pre-routing middleware.
type AuthHooks ¶
type AuthHooks struct {
BeforeAuthenticate func(ctx *Context)
AfterAuthenticate func(ctx *Context, principal *Principal, err error)
}
AuthHooks provides hook points around authentication.
type Authenticator ¶
Authenticator validates a request and returns a principal.
type AuthenticatorFactory ¶
type AuthenticatorFactory func(config map[string]any) (Authenticator, error)
AuthenticatorFactory builds an authenticator using a config map.
type Authorizer ¶
Authorizer checks if a principal can access a resource.
type CacheFactory ¶
CacheFactory builds a cache store using a config map.
type ConfigProfile ¶
ConfigProfile describes layered config sources.
type Context ¶
type Context struct {
ResponseWriter http.ResponseWriter
Request *http.Request
Params router.Params
// contains filtered or unexported fields
}
Context holds request-specific data.
func NewContext ¶
NewContext constructs a Context.
func (*Context) BindMultipart ¶
BindMultipart binds multipart form values into dst.
func (*Context) ParamInt64 ¶
ParamInt64 returns a route param as an int64.
func (*Context) QueryInt64 ¶
QueryInt64 returns a query param as an int64.
func (*Context) Render ¶
func (c *Context) Render(status int, fn render.RenderFunc) error
Render uses a custom render function.
func (*Context) SaveUploadedFile ¶
func (c *Context) SaveUploadedFile(file *multipart.FileHeader, dst string) error
SaveUploadedFile writes a multipart file to disk.
type ErrorEnvelope ¶
type ErrorEnvelope struct {
Code string
Message string
Fields []validate.FieldError
RequestID string
}
ErrorEnvelope describes a standardized error payload.
type ErrorHandler ¶
ErrorHandler processes errors returned by handlers.
type ErrorPageData ¶
type ErrorPageData struct {
Status int
Code string
Message string
Fields []validate.FieldError
RequestID string
Error ErrorEnvelope
}
ErrorPageData is passed to error templates.
type Group ¶
type Group struct {
// contains filtered or unexported fields
}
Group defines a route group with a common prefix and middleware.
func (*Group) DELETE ¶
func (g *Group) DELETE(path string, handler Handler, middleware ...Middleware)
DELETE registers a DELETE route in the group.
func (*Group) GET ¶
func (g *Group) GET(path string, handler Handler, middleware ...Middleware)
GET registers a GET route in the group.
func (*Group) Group ¶
func (g *Group) Group(prefix string, middleware ...Middleware) *Group
Group creates a nested group.
func (*Group) Handle ¶
func (g *Group) Handle(method, path string, handler Handler, middleware ...Middleware)
Handle registers a route in the group for an arbitrary method.
func (*Group) PATCH ¶
func (g *Group) PATCH(path string, handler Handler, middleware ...Middleware)
PATCH registers a PATCH route in the group.
func (*Group) POST ¶
func (g *Group) POST(path string, handler Handler, middleware ...Middleware)
POST registers a POST route in the group.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger wraps slog.Logger with request context.
func LoggerFromContext ¶
LoggerFromContext builds a logger that includes request metadata.
func LoggerFromRequest ¶
LoggerFromRequest builds a logger using request metadata.
type Middleware ¶
Middleware wraps a handler with additional behavior.
type MiddlewareFactory ¶
type MiddlewareFactory func(config map[string]any) (Middleware, error)
MiddlewareFactory builds middleware using a config map.
type OpenAPIOption ¶
type OpenAPIOption func(*OpenAPIOptions)
OpenAPIOption customizes OpenAPI route derivation.
func WithOpenAPIIncludeUnnamed ¶
func WithOpenAPIIncludeUnnamed(enabled bool) OpenAPIOption
WithOpenAPIIncludeUnnamed toggles inclusion of unnamed routes.
func WithOpenAPISkipMethods ¶
func WithOpenAPISkipMethods(methods ...string) OpenAPIOption
WithOpenAPISkipMethods skips specific HTTP methods.
func WithOpenAPISkipPaths ¶
func WithOpenAPISkipPaths(paths ...string) OpenAPIOption
WithOpenAPISkipPaths skips paths that match the provided patterns.
func WithOpenAPITagFromHost ¶
func WithOpenAPITagFromHost(enabled bool) OpenAPIOption
WithOpenAPITagFromHost applies host values as OpenAPI tags.
type OpenAPIOptions ¶
type OpenAPIOptions struct {
IncludeUnnamed bool
SkipPaths []string
SkipMethods []string
TagFromHost bool
}
OpenAPIOptions configures automatic OpenAPI route derivation.
func DefaultOpenAPIOptions ¶
func DefaultOpenAPIOptions() OpenAPIOptions
DefaultOpenAPIOptions returns the default OpenAPI options.
type Option ¶
type Option func(*App)
Option customizes the app instance.
func WithAuthHooks ¶
WithAuthHooks configures authentication hooks.
func WithConfig ¶
WithConfig overrides the default config.
func WithErrorHandler ¶
func WithErrorHandler(handler ErrorHandler) Option
WithErrorHandler overrides the default error handler.
func WithErrorTemplates ¶
WithErrorTemplates configures template names for HTML error pages. Use status code keys, or 0 for a default template.
func WithRegistry ¶
WithRegistry sets a custom registry for extensibility.
func WithRenderer ¶
WithRenderer sets a custom template engine.
func WithTemplateFS ¶
WithTemplateFS configures embedded templates from an fs.FS.
func WithTemplateFSDevDir ¶
WithTemplateFSDevDir sets a disk path for template reloads when using embedded templates.
func WithTemplateFuncs ¶
WithTemplateFuncs registers template functions for the built-in renderer.
func WithTemplatePartials ¶
WithTemplatePartials configures glob patterns for partial templates.
func WithTemplateReload ¶
WithTemplateReload enables template reloading for development.
func WithTemplateSubdirs ¶
WithTemplateSubdirs enables nested template directories.
type Principal ¶
Principal represents an authenticated actor.
func PrincipalFromContext ¶
PrincipalFromContext extracts the principal from context storage.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry stores registered middleware, auth, cache, and validators.
func (*Registry) Authenticator ¶
Authenticator builds a named authenticator.
func (*Registry) Middleware ¶
Middleware builds a named middleware.
func (*Registry) RegisterAuthenticator ¶
func (r *Registry) RegisterAuthenticator(name string, factory AuthenticatorFactory) error
RegisterAuthenticator registers a named authenticator factory.
func (*Registry) RegisterCache ¶
func (r *Registry) RegisterCache(name string, factory CacheFactory) error
RegisterCache registers a named cache store factory.
func (*Registry) RegisterMiddleware ¶
func (r *Registry) RegisterMiddleware(name string, factory MiddlewareFactory) error
RegisterMiddleware registers a named middleware factory.
func (*Registry) RegisterValidator ¶
func (r *Registry) RegisterValidator(name string, fn validate.ValidatorFunc) error
RegisterValidator registers a named validator and exposes it via validate.Register.
type RequestMetadata ¶
RequestMetadata holds request-scoped identifiers.
func RequestMetadataFromContext ¶
func RequestMetadataFromContext(ctx context.Context) RequestMetadata
RequestMetadataFromContext returns metadata stored in a context.
func RequestMetadataFromRequest ¶
func RequestMetadataFromRequest(r *http.Request) RequestMetadata
RequestMetadataFromRequest returns metadata from the request and context.
type RouteOption ¶
type RouteOption func(*routeConfig)
RouteOption customizes route registration.
func WithHost ¶
func WithHost(host string) RouteOption
WithHost scopes a route to a host or wildcard (e.g. "api.example.com", "*.example.com").
func WithMiddleware ¶
func WithMiddleware(middleware ...Middleware) RouteOption
WithMiddleware attaches middleware to a route.
func WithTimeout ¶
func WithTimeout(timeout time.Duration) RouteOption
WithTimeout sets a per-route timeout.
type StaticOption ¶
type StaticOption func(*staticConfig)
StaticOption configures static file handling.
func StaticCacheControl ¶
func StaticCacheControl(value string) StaticOption
StaticCacheControl sets the Cache-Control header value.
func StaticIndex ¶
func StaticIndex(name string) StaticOption
StaticIndex sets the index file name to serve for directories.