Documentation
¶
Overview ¶
Package routing provides file-based routing similar to SvelteKit. It scans .templ files in a routes directory and maps them to URLs.
Package routing provides component registry for route handlers.
Index ¶
- func BuildURL(pattern string, pathParams Params, queryParams *QueryParams) string
- func GetActions(pagePath string) map[string]ActionFunc
- func GetAllActions() []string
- func GetAllRouteOptions() map[string]RouteOptions
- func GetLayoutTier(path string) string
- func HasLayout(path string) bool
- func HasPage(path string) bool
- func RegisterAction(pagePath, actionName string, fn ActionFunc)
- func RegisterError(path string, fn ComponentFunc)
- func RegisterHook(fn HookFunc)
- func RegisterLayout(path string, fn LayoutFunc)
- func RegisterLayoutLoad(path string, fn LoadFunc)
- func RegisterLayoutWithOptions(path string, fn LayoutFunc, tier string)
- func RegisterLoad(path string, fn LoadFunc)
- func RegisterLoading(path string, fn ComponentFunc)
- func RegisterMiddleware(path string, fn MiddlewareFunc)
- func RegisterPage(path string, fn ComponentFunc)
- func RegisterPageWithOptions(path string, fn ComponentFunc, opts RouteOptions)
- func RegisterRemoteAction(name string, action RemoteActionFunc)
- func RegisterRootLayout(fn LayoutFunc, tier string)
- func RegisterSlot(pagePath, slotName string, fn SlotFunc)
- func ValidateParams(route *Route, params Params) error
- type ActionFunc
- type ActionRedirect
- type ActionResponse
- type ActionValidationError
- type ComponentFunc
- type Handler
- type HookFunc
- type LayoutChain
- type LayoutContext
- type LayoutData
- type LayoutFunc
- type LayoutNode
- type LayoutResolver
- type LayoutTree
- type LoadContext
- type LoadFunc
- type ManualRoute
- type ManualRouter
- func (mr *ManualRouter) DELETE(path string, handler Handler, middleware ...Middleware) *ManualRoute
- func (mr *ManualRouter) GET(path string, handler Handler, middleware ...Middleware) *ManualRoute
- func (mr *ManualRouter) GetRoutes() []*ManualRoute
- func (mr *ManualRouter) Group(prefix string, middleware ...Middleware) *RouteGroup
- func (mr *ManualRouter) HEAD(path string, handler Handler, middleware ...Middleware) *ManualRoute
- func (mr *ManualRouter) Match(method, path string) (*ManualRoute, Params)
- func (mr *ManualRouter) MatchAll(path string) []*ManualRoute
- func (mr *ManualRouter) OPTIONS(path string, handler Handler, middleware ...Middleware) *ManualRoute
- func (mr *ManualRouter) PATCH(path string, handler Handler, middleware ...Middleware) *ManualRoute
- func (mr *ManualRouter) POST(path string, handler Handler, middleware ...Middleware) *ManualRoute
- func (mr *ManualRouter) PUT(path string, handler Handler, middleware ...Middleware) *ManualRoute
- func (mr *ManualRouter) Page(path string, component templ.Component, middleware ...Middleware) *ManualRoute
- func (mr *ManualRouter) RegisterComponent(path string, component templ.Component, middleware ...Middleware) *ManualRoute
- func (mr *ManualRouter) RegisterRoute(method, path string, handler Handler, middleware ...Middleware) *ManualRoute
- func (mr *ManualRouter) RegisterToFiber(app *fiberpkg.App) error
- func (mr *ManualRouter) SetAutoRouter(router *Router)
- type Middleware
- type MiddlewareFunc
- type ParamExtractor
- type Params
- func (p Params) Bool(key string) (bool, error)
- func (p Params) BoolOk(key string) (bool, bool, error)
- func (p Params) Clone() Params
- func (p Params) Delete(key string)
- func (p Params) Float64(key string) (float64, error)
- func (p Params) Float64Ok(key string) (float64, bool, error)
- func (p Params) Get(key string) string
- func (p Params) GetDefault(key, defaultValue string) string
- func (p Params) Has(key string) bool
- func (p Params) Int(key string) (int, error)
- func (p Params) Int64(key string) (int64, error)
- func (p Params) Int64Ok(key string) (int64, bool, error)
- func (p Params) IntDefault(key string, defaultValue int) int
- func (p Params) IntOk(key string) (int, bool, error)
- func (p Params) Merge(other Params)
- func (p Params) Set(key, value string)
- func (p Params) Slice(key string) []string
- func (p Params) ToJSON() ([]byte, error)
- func (p Params) ToMap() map[string]string
- type PathBuilder
- type QueryParams
- func (qp *QueryParams) Add(key, value string)
- func (qp *QueryParams) Bool(key string) (bool, error)
- func (qp *QueryParams) BoolDefault(key string, defaultValue bool) bool
- func (qp *QueryParams) BoolOk(key string) (bool, bool, error)
- func (qp *QueryParams) Del(key string)
- func (qp *QueryParams) Encode() string
- func (qp *QueryParams) Get(key string) string
- func (qp *QueryParams) GetAll(key string) []string
- func (qp *QueryParams) Has(key string) bool
- func (qp *QueryParams) Int(key string) (int, error)
- func (qp *QueryParams) IntDefault(key string, defaultValue int) int
- func (qp *QueryParams) IntOk(key string) (int, bool, error)
- func (qp *QueryParams) Set(key, value string)
- func (qp *QueryParams) ToMap() map[string][]string
- type RateLimitOptions
- type Registry
- func (r *Registry) GetAction(pagePath, actionName string) ActionFunc
- func (r *Registry) GetActions(pagePath string) map[string]ActionFunc
- func (r *Registry) GetAllRouteOptions() map[string]RouteOptions
- func (r *Registry) GetError(path string) ComponentFunc
- func (r *Registry) GetHooks() []HookFunc
- func (r *Registry) GetLayout(path string) LayoutFunc
- func (r *Registry) GetLayoutLoad(path string) LoadFunc
- func (r *Registry) GetLayoutTier(path string) string
- func (r *Registry) GetLoad(path string) LoadFunc
- func (r *Registry) GetLoading(path string) ComponentFunc
- func (r *Registry) GetMiddleware(path string) MiddlewareFunc
- func (r *Registry) GetPage(path string) ComponentFunc
- func (r *Registry) GetRootLayout() LayoutFunc
- func (r *Registry) GetRouteOptions(path string) RouteOptions
- func (r *Registry) GetSlot(pagePath, slotName string) SlotFunc
- func (r *Registry) HasLayout(path string) bool
- func (r *Registry) HasPage(path string) bool
- func (r *Registry) RegisterAction(pagePath, actionName string, fn ActionFunc)
- func (r *Registry) RegisterError(path string, fn ComponentFunc)
- func (r *Registry) RegisterHook(fn HookFunc)
- func (r *Registry) RegisterLayout(path string, fn LayoutFunc)
- func (r *Registry) RegisterLayoutLoad(path string, fn LoadFunc)
- func (r *Registry) RegisterLayoutWithOptions(path string, fn LayoutFunc, tier string)
- func (r *Registry) RegisterLoad(path string, fn LoadFunc)
- func (r *Registry) RegisterLoading(path string, fn ComponentFunc)
- func (r *Registry) RegisterMiddleware(path string, fn MiddlewareFunc)
- func (r *Registry) RegisterPage(path string, fn ComponentFunc)
- func (r *Registry) RegisterPageWithOptions(path string, fn ComponentFunc, opts RouteOptions)
- func (r *Registry) RegisterRootLayout(fn LayoutFunc, tier string)
- func (r *Registry) RegisterSlot(pagePath, slotName string, fn SlotFunc)
- type RemoteActionFunc
- type RemoteContext
- type RemoteRegistry
- type RenderStrategy
- type Route
- type RouteGroup
- func (g *RouteGroup) DELETE(path string, handler Handler, middleware ...Middleware) *ManualRoute
- func (g *RouteGroup) GET(path string, handler Handler, middleware ...Middleware) *ManualRoute
- func (g *RouteGroup) Group(prefix string, middleware ...Middleware) *RouteGroup
- func (g *RouteGroup) PATCH(path string, handler Handler, middleware ...Middleware) *ManualRoute
- func (g *RouteGroup) POST(path string, handler Handler, middleware ...Middleware) *ManualRoute
- func (g *RouteGroup) PUT(path string, handler Handler, middleware ...Middleware) *ManualRoute
- func (g *RouteGroup) Page(path string, component templ.Component, middleware ...Middleware) *ManualRoute
- func (g *RouteGroup) RegisterRoute(method, path string, handler Handler, middleware ...Middleware) *ManualRoute
- func (g *RouteGroup) Use(middleware ...Middleware)
- type RouteMatch
- type RouteOptions
- type RouteType
- type Router
- func (r *Router) GetErrorRoute(path string) *Route
- func (r *Router) GetLayouts() []*Route
- func (r *Router) GetLoadings() []*Route
- func (r *Router) GetMiddlewares() []*Route
- func (r *Router) GetPages() []*Route
- func (r *Router) GetRoutes() []*Route
- func (r *Router) Match(urlPath string) (*Route, map[string]string)
- func (r *Router) MatchWithLayout(urlPath string) (*Route, []*Route, map[string]string)
- func (r *Router) ResolveLayoutChain(route *Route) []*Route
- func (r *Router) ResolveMiddlewareChain(route *Route) []*Route
- func (r *Router) Scan() error
- type SlotFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildURL ¶
func BuildURL(pattern string, pathParams Params, queryParams *QueryParams) string
BuildURL builds a URL from a route pattern and parameters.
func GetActions ¶ added in v0.1.35
func GetActions(pagePath string) map[string]ActionFunc
GetActions returns all actions for a page path from the global registry.
func GetAllActions ¶ added in v0.1.32
func GetAllActions() []string
GetAllActions returns all registered action names.
func GetAllRouteOptions ¶ added in v0.1.39
func GetAllRouteOptions() map[string]RouteOptions
GetAllRouteOptions returns all route options from the global registry.
func GetLayoutTier ¶ added in v0.1.36
GetLayoutTier returns the runtime tier for a layout from the global registry.
func RegisterAction ¶ added in v0.1.35
func RegisterAction(pagePath, actionName string, fn ActionFunc)
RegisterAction registers a page action in the global registry.
func RegisterError ¶ added in v0.1.24
func RegisterError(path string, fn ComponentFunc)
RegisterError registers an error component in the global registry.
func RegisterHook ¶ added in v0.1.35
func RegisterHook(fn HookFunc)
RegisterHook registers a global server-side hook in the global registry.
func RegisterLayout ¶
func RegisterLayout(path string, fn LayoutFunc)
RegisterLayout registers a layout component in the global registry.
func RegisterLayoutLoad ¶ added in v0.1.35
RegisterLayoutLoad registers a layout load function in the global registry.
func RegisterLayoutWithOptions ¶ added in v0.1.36
func RegisterLayoutWithOptions(path string, fn LayoutFunc, tier string)
RegisterLayoutWithOptions registers a layout component with options in the global registry.
func RegisterLoad ¶ added in v0.1.35
RegisterLoad registers a load function in the global registry.
func RegisterLoading ¶ added in v0.1.24
func RegisterLoading(path string, fn ComponentFunc)
RegisterLoading registers a loading component in the global registry.
func RegisterMiddleware ¶ added in v0.1.24
func RegisterMiddleware(path string, fn MiddlewareFunc)
RegisterMiddleware registers a middleware function in the global registry.
func RegisterPage ¶
func RegisterPage(path string, fn ComponentFunc)
RegisterPage registers a page component in the global registry (default SSR).
func RegisterPageWithOptions ¶
func RegisterPageWithOptions(path string, fn ComponentFunc, opts RouteOptions)
RegisterPageWithOptions registers a page in the global registry with options.
func RegisterRemoteAction ¶
func RegisterRemoteAction(name string, action RemoteActionFunc)
RegisterRemoteAction registers a remote server function.
func RegisterRootLayout ¶
func RegisterRootLayout(fn LayoutFunc, tier string)
RegisterRootLayout registers the root layout in the global registry with a runtime tier.
func RegisterSlot ¶ added in v0.1.4
RegisterSlot registers a PPR dynamic slot in the global registry.
func ValidateParams ¶
ValidateParams validates route parameters against a route.
Types ¶
type ActionFunc ¶ added in v0.1.35
type ActionFunc func(c LoadContext) (interface{}, error)
ActionFunc is a function that handles a form action.
func GetAction ¶ added in v0.1.35
func GetAction(pagePath, actionName string) ActionFunc
GetAction returns a specific action for a page path from the global registry.
type ActionRedirect ¶ added in v0.1.38
ActionRedirect describes a redirect response for progressive-enhanced forms.
type ActionResponse ¶ added in v0.1.38
type ActionResponse struct {
Data interface{} `json:"data,omitempty"`
Code string `json:"code,omitempty"`
Redirect *ActionRedirect `json:"redirect,omitempty"`
Validation *ActionValidationError `json:"validation,omitempty"`
Revalidate []string `json:"revalidate,omitempty"`
RevalidateTags []string `json:"revalidateTags,omitempty"`
RevalidateKeys []string `json:"revalidateKeys,omitempty"`
}
ActionResponse is the structured response contract for form actions.
type ActionValidationError ¶ added in v0.1.38
type ActionValidationError struct {
FieldErrors map[string]string `json:"fieldErrors,omitempty"`
FormError string `json:"formError,omitempty"`
}
ActionValidationError contains form and field-level validation details.
type ComponentFunc ¶
ComponentFunc is a function that returns a templ.Component.
func GetError ¶ added in v0.1.24
func GetError(path string) ComponentFunc
GetError returns the error component from the global registry.
func GetLoading ¶ added in v0.1.24
func GetLoading(path string) ComponentFunc
GetLoading returns the loading component from the global registry.
func GetPage ¶
func GetPage(path string) ComponentFunc
GetPage returns the page component from the global registry.
type HookFunc ¶ added in v0.1.35
HookFunc is a function that handles a server-side hook (middleware).
type LayoutChain ¶
type LayoutChain struct {
// Layouts are ordered from root to leaf
Layouts []*Route
// Page is the final page route
Page *Route
// Error is the error route for this chain
Error *Route
}
LayoutChain represents a chain of nested layouts.
func (*LayoutChain) String ¶
func (lc *LayoutChain) String() string
String returns a string representation of the layout chain.
type LayoutContext ¶
type LayoutContext struct {
// Path is the current URL path
Path string
// Params are the route parameters
Params map[string]string
// Data is the layout data chain
Data []*LayoutData
// Depth is the current layout depth
Depth int
}
LayoutContext provides context for layout rendering.
func NewLayoutContext ¶
func NewLayoutContext(path string, params map[string]string) *LayoutContext
NewLayoutContext creates a new layout context.
func (*LayoutContext) CurrentData ¶
func (lc *LayoutContext) CurrentData() *LayoutData
CurrentData returns the current layout data.
func (*LayoutContext) PopData ¶
func (lc *LayoutContext) PopData() *LayoutData
PopData pops layout data from the chain.
func (*LayoutContext) PushData ¶
func (lc *LayoutContext) PushData(data *LayoutData)
PushData pushes new layout data onto the chain.
type LayoutData ¶
type LayoutData struct {
// Data is the layout data
Data map[string]interface{}
// Children is nested content
Children interface{}
}
LayoutData represents data passed through layouts.
func (*LayoutData) Get ¶
func (ld *LayoutData) Get(key string) (interface{}, bool)
Get gets a value from layout data.
func (*LayoutData) Merge ¶
func (ld *LayoutData) Merge(other *LayoutData)
Merge merges another layout data into this one.
func (*LayoutData) Set ¶
func (ld *LayoutData) Set(key string, value interface{})
Set sets a value in layout data.
type LayoutFunc ¶
LayoutFunc is a function that returns a templ.Component for layouts.
func GetLayout ¶
func GetLayout(path string) LayoutFunc
GetLayout returns the layout component from the global registry.
func GetRootLayout ¶
func GetRootLayout() LayoutFunc
GetRootLayout returns the root layout from the global registry.
type LayoutNode ¶
type LayoutNode struct {
Route *Route
Children []*LayoutNode
Parent *LayoutNode
}
LayoutNode represents a node in the layout tree.
type LayoutResolver ¶
type LayoutResolver struct {
// contains filtered or unexported fields
}
LayoutResolver resolves layout hierarchies for routes.
func NewLayoutResolver ¶
func NewLayoutResolver() *LayoutResolver
NewLayoutResolver creates a new layout resolver.
func (*LayoutResolver) RegisterError ¶
func (lr *LayoutResolver) RegisterError(route *Route)
RegisterError registers an error route.
func (*LayoutResolver) RegisterLayout ¶
func (lr *LayoutResolver) RegisterLayout(route *Route)
RegisterLayout registers a layout route.
func (*LayoutResolver) ResolveChain ¶
func (lr *LayoutResolver) ResolveChain(page *Route) *LayoutChain
ResolveChain resolves the layout chain for a page route.
type LayoutTree ¶
type LayoutTree struct {
Root *LayoutNode
}
LayoutTree represents a tree of layouts.
func NewLayoutTree ¶
func NewLayoutTree(routes []*Route) *LayoutTree
NewLayoutTree creates a new layout tree from routes.
func (*LayoutTree) FindLayoutChain ¶
func (t *LayoutTree) FindLayoutChain(path string) []*Route
FindLayoutChain finds the layout chain for a path.
type LoadContext ¶ added in v0.1.35
type LoadContext interface {
Param(key string) string
Query(key string, defaultValue ...string) string
Header(key string) string
Cookie(key string) string
FormValue(key string, defaultValue ...string) string
Method() string
Path() string
}
LoadContext provides access to request data for server-side Load functions.
type LoadFunc ¶ added in v0.1.35
type LoadFunc func(c LoadContext) (map[string]interface{}, error)
LoadFunc is a function that returns data for a page or layout.
func GetLayoutLoad ¶ added in v0.1.35
GetLayoutLoad returns a layout load function from the global registry.
type ManualRoute ¶
type ManualRoute struct {
// Path is the URL path pattern
Path string
// Method is the HTTP method (GET, POST, etc.)
Method string
// Handler is the route handler
Handler Handler
// Component is the Templ component (for page routes)
Component templ.Component
// Middleware is the route-specific middleware
Middleware []Middleware
// Params are the parameter names
Params []string
// IsDynamic indicates if the route has dynamic segments
IsDynamic bool
// Priority is used for route matching order
Priority int
// Meta contains route metadata
Meta map[string]interface{}
// contains filtered or unexported fields
}
ManualRoute represents a manually registered route.
func (*ManualRoute) GetMeta ¶
func (r *ManualRoute) GetMeta(key string) (interface{}, bool)
GetMeta gets metadata from the route.
func (*ManualRoute) Name ¶
func (r *ManualRoute) Name(name string) *ManualRoute
Name sets a name for the route.
func (*ManualRoute) SetMeta ¶
func (r *ManualRoute) SetMeta(key string, value interface{}) *ManualRoute
SetMeta sets metadata on the route.
func (*ManualRoute) String ¶
func (r *ManualRoute) String() string
String returns a string representation of the route.
type ManualRouter ¶
type ManualRouter struct {
// contains filtered or unexported fields
}
ManualRouter manages manually registered routes.
func NewManualRouter ¶
func NewManualRouter() *ManualRouter
NewManualRouter creates a new manual router.
func (*ManualRouter) DELETE ¶
func (mr *ManualRouter) DELETE(path string, handler Handler, middleware ...Middleware) *ManualRoute
DELETE registers a DELETE route.
func (*ManualRouter) GET ¶
func (mr *ManualRouter) GET(path string, handler Handler, middleware ...Middleware) *ManualRoute
GET registers a GET route.
func (*ManualRouter) GetRoutes ¶
func (mr *ManualRouter) GetRoutes() []*ManualRoute
GetRoutes returns all manual routes.
func (*ManualRouter) Group ¶
func (mr *ManualRouter) Group(prefix string, middleware ...Middleware) *RouteGroup
Group creates a new route group.
func (*ManualRouter) HEAD ¶
func (mr *ManualRouter) HEAD(path string, handler Handler, middleware ...Middleware) *ManualRoute
HEAD registers a HEAD route.
func (*ManualRouter) Match ¶
func (mr *ManualRouter) Match(method, path string) (*ManualRoute, Params)
Match matches a URL path and method to a route.
func (*ManualRouter) MatchAll ¶
func (mr *ManualRouter) MatchAll(path string) []*ManualRoute
MatchAll returns all routes matching a path (for any method).
func (*ManualRouter) OPTIONS ¶
func (mr *ManualRouter) OPTIONS(path string, handler Handler, middleware ...Middleware) *ManualRoute
OPTIONS registers an OPTIONS route.
func (*ManualRouter) PATCH ¶
func (mr *ManualRouter) PATCH(path string, handler Handler, middleware ...Middleware) *ManualRoute
PATCH registers a PATCH route.
func (*ManualRouter) POST ¶
func (mr *ManualRouter) POST(path string, handler Handler, middleware ...Middleware) *ManualRoute
POST registers a POST route.
func (*ManualRouter) PUT ¶
func (mr *ManualRouter) PUT(path string, handler Handler, middleware ...Middleware) *ManualRoute
PUT registers a PUT route.
func (*ManualRouter) Page ¶
func (mr *ManualRouter) Page(path string, component templ.Component, middleware ...Middleware) *ManualRoute
Page registers a Templ component as a page.
func (*ManualRouter) RegisterComponent ¶
func (mr *ManualRouter) RegisterComponent(path string, component templ.Component, middleware ...Middleware) *ManualRoute
RegisterComponent registers a Templ component as a page route.
func (*ManualRouter) RegisterRoute ¶
func (mr *ManualRouter) RegisterRoute(method, path string, handler Handler, middleware ...Middleware) *ManualRoute
RegisterRoute registers a manual route.
func (*ManualRouter) RegisterToFiber ¶
func (mr *ManualRouter) RegisterToFiber(app *fiberpkg.App) error
RegisterToFiber registers all routes with a Fiber app.
func (*ManualRouter) SetAutoRouter ¶
func (mr *ManualRouter) SetAutoRouter(router *Router)
SetAutoRouter sets the auto router for hybrid routing.
type Middleware ¶
Middleware is a Fiber middleware function.
type MiddlewareFunc ¶ added in v0.1.24
type MiddlewareFunc interface{}
MiddlewareFunc represents a generic middleware handler. Typically this is downcast to fiber.Handler when registering routes.
func GetMiddleware ¶ added in v0.1.24
func GetMiddleware(path string) MiddlewareFunc
GetMiddleware returns the middleware function from the global registry.
type ParamExtractor ¶
type ParamExtractor struct {
// contains filtered or unexported fields
}
ParamExtractor extracts parameters from URLs.
func NewParamExtractor ¶
func NewParamExtractor(pattern string) *ParamExtractor
NewParamExtractor creates a new parameter extractor for a pattern.
func (*ParamExtractor) Extract ¶
func (pe *ParamExtractor) Extract(path string) (Params, bool)
Extract extracts parameters from a URL path.
func (*ParamExtractor) Match ¶
func (pe *ParamExtractor) Match(path string) bool
Match checks if a path matches the pattern.
func (*ParamExtractor) Params ¶
func (pe *ParamExtractor) Params() []string
Params returns the parameter names for this pattern.
type Params ¶
Params represents route parameters extracted from URL.
func ParamsFromJSON ¶
ParamsFromJSON creates Params from JSON.
func (Params) BoolOk ¶ added in v0.1.5
BoolOk returns a parameter as a bool with an existence check.
func (Params) Float64Ok ¶ added in v0.1.5
Float64Ok returns a parameter as a float64 with an existence check.
func (Params) GetDefault ¶
GetDefault returns a parameter value with a default.
func (Params) Int ¶
Int returns a parameter as an int. Returns error if the parameter doesn't exist or cannot be parsed.
func (Params) Int64Ok ¶ added in v0.1.5
Int64Ok returns a parameter as an int64 with an existence check.
func (Params) IntDefault ¶
IntDefault returns a parameter as an int with a default. Returns defaultValue if the parameter doesn't exist or cannot be parsed.
func (Params) IntOk ¶ added in v0.1.5
IntOk returns a parameter as an int with an existence check. Returns (value, true, nil) if found and valid. Returns (0, false, nil) if not found. Returns (0, true, error) if found but invalid.
type PathBuilder ¶
type PathBuilder struct {
// contains filtered or unexported fields
}
PathBuilder helps build paths with parameters.
func NewPathBuilder ¶
func NewPathBuilder(pattern string) *PathBuilder
NewPathBuilder creates a new path builder.
func (*PathBuilder) Param ¶
func (pb *PathBuilder) Param(key, value string) *PathBuilder
Param sets a path parameter.
func (*PathBuilder) Query ¶
func (pb *PathBuilder) Query(key, value string) *PathBuilder
Query sets a query parameter.
func (*PathBuilder) QueryAdd ¶
func (pb *PathBuilder) QueryAdd(key, value string) *PathBuilder
QueryAdd adds a query parameter.
type QueryParams ¶
type QueryParams struct {
// contains filtered or unexported fields
}
QueryParams represents URL query parameters.
func NewQueryParams ¶
func NewQueryParams(query string) *QueryParams
NewQueryParams creates QueryParams from a URL query string.
func NewQueryParamsFromValues ¶
func NewQueryParamsFromValues(values url.Values) *QueryParams
NewQueryParamsFromValues creates QueryParams from url.Values.
func (*QueryParams) Bool ¶
func (qp *QueryParams) Bool(key string) (bool, error)
Bool returns a query parameter as a bool.
func (*QueryParams) BoolDefault ¶
func (qp *QueryParams) BoolDefault(key string, defaultValue bool) bool
BoolDefault returns a query parameter as a bool with a default.
func (*QueryParams) BoolOk ¶ added in v0.1.5
func (qp *QueryParams) BoolOk(key string) (bool, bool, error)
BoolOk returns a query parameter as a bool with an existence check.
func (*QueryParams) Encode ¶
func (qp *QueryParams) Encode() string
Encode encodes the query parameters to a string.
func (*QueryParams) Get ¶
func (qp *QueryParams) Get(key string) string
Get returns the first value for a key.
func (*QueryParams) GetAll ¶
func (qp *QueryParams) GetAll(key string) []string
GetAll returns all values for a key.
func (*QueryParams) Int ¶
func (qp *QueryParams) Int(key string) (int, error)
Int returns a query parameter as an int.
func (*QueryParams) IntDefault ¶
func (qp *QueryParams) IntDefault(key string, defaultValue int) int
IntDefault returns a query parameter as an int with a default.
func (*QueryParams) IntOk ¶ added in v0.1.5
func (qp *QueryParams) IntOk(key string) (int, bool, error)
IntOk returns a query parameter as an int with an existence check.
func (*QueryParams) ToMap ¶
func (qp *QueryParams) ToMap() map[string][]string
ToMap converts to a regular map.
type RateLimitOptions ¶ added in v0.1.24
RateLimitOptions holds configuration for per-route rate limiters.
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry holds registered page and layout components.
func GetGlobalRegistry ¶
func GetGlobalRegistry() *Registry
GetGlobalRegistry returns the global registry.
func (*Registry) GetAction ¶ added in v0.1.35
func (r *Registry) GetAction(pagePath, actionName string) ActionFunc
GetAction returns a specific action for a page path.
func (*Registry) GetActions ¶ added in v0.1.35
func (r *Registry) GetActions(pagePath string) map[string]ActionFunc
GetActions returns all actions for a page path.
func (*Registry) GetAllRouteOptions ¶ added in v0.1.39
func (r *Registry) GetAllRouteOptions() map[string]RouteOptions
GetAllRouteOptions returns a copy of all registered route options keyed by path.
func (*Registry) GetError ¶ added in v0.1.24
func (r *Registry) GetError(path string) ComponentFunc
GetError returns the error component for a path.
func (*Registry) GetLayout ¶
func (r *Registry) GetLayout(path string) LayoutFunc
GetLayout returns the layout component for a path.
func (*Registry) GetLayoutLoad ¶ added in v0.1.35
GetLayoutLoad returns the load function for a layout path.
func (*Registry) GetLayoutTier ¶ added in v0.1.36
GetLayoutTier returns the runtime tier for a layout path.
func (*Registry) GetLoading ¶ added in v0.1.24
func (r *Registry) GetLoading(path string) ComponentFunc
GetLoading returns the loading component for a path.
func (*Registry) GetMiddleware ¶ added in v0.1.24
func (r *Registry) GetMiddleware(path string) MiddlewareFunc
GetMiddleware returns the middleware function for a path.
func (*Registry) GetPage ¶
func (r *Registry) GetPage(path string) ComponentFunc
GetPage returns the page component for a path.
func (*Registry) GetRootLayout ¶
func (r *Registry) GetRootLayout() LayoutFunc
GetRootLayout returns the root layout component.
func (*Registry) GetRouteOptions ¶
func (r *Registry) GetRouteOptions(path string) RouteOptions
GetRouteOptions returns the route options for a path.
func (*Registry) GetSlot ¶ added in v0.1.4
GetSlot returns the SlotFunc for a named PPR slot on a page path.
func (*Registry) RegisterAction ¶ added in v0.1.35
func (r *Registry) RegisterAction(pagePath, actionName string, fn ActionFunc)
RegisterAction registers an action for a page path.
func (*Registry) RegisterError ¶ added in v0.1.24
func (r *Registry) RegisterError(path string, fn ComponentFunc)
RegisterError registers an error component for a route path.
func (*Registry) RegisterHook ¶ added in v0.1.35
RegisterHook registers a global server-side hook (middleware).
func (*Registry) RegisterLayout ¶
func (r *Registry) RegisterLayout(path string, fn LayoutFunc)
RegisterLayout registers a layout component for a route path.
func (*Registry) RegisterLayoutLoad ¶ added in v0.1.35
RegisterLayoutLoad registers a load function for a layout path.
func (*Registry) RegisterLayoutWithOptions ¶ added in v0.1.36
func (r *Registry) RegisterLayoutWithOptions(path string, fn LayoutFunc, tier string)
RegisterLayoutWithOptions registers a layout component with a specific runtime tier.
func (*Registry) RegisterLoad ¶ added in v0.1.35
RegisterLoad registers a load function for a route path.
func (*Registry) RegisterLoading ¶ added in v0.1.24
func (r *Registry) RegisterLoading(path string, fn ComponentFunc)
RegisterLoading registers a loading component for a route path.
func (*Registry) RegisterMiddleware ¶ added in v0.1.24
func (r *Registry) RegisterMiddleware(path string, fn MiddlewareFunc)
RegisterMiddleware registers a middleware function for a route path.
func (*Registry) RegisterPage ¶
func (r *Registry) RegisterPage(path string, fn ComponentFunc)
RegisterPage registers a page component for a route path (default to SSR).
func (*Registry) RegisterPageWithOptions ¶
func (r *Registry) RegisterPageWithOptions(path string, fn ComponentFunc, opts RouteOptions)
RegisterPageWithOptions registers a page component with specific options.
func (*Registry) RegisterRootLayout ¶
func (r *Registry) RegisterRootLayout(fn LayoutFunc, tier string)
RegisterRootLayout registers the root layout component with a runtime tier.
func (*Registry) RegisterSlot ¶ added in v0.1.4
RegisterSlot registers a PPR dynamic slot component for a page path.
type RemoteActionFunc ¶
type RemoteActionFunc func(ctx context.Context, rc RemoteContext, input interface{}) (interface{}, error)
RemoteActionFunc is a type-safe server function that can be called remotely from the client.
func GetRemoteAction ¶
func GetRemoteAction(name string) (RemoteActionFunc, bool)
GetRemoteAction retrieves a registered remote server function.
type RemoteContext ¶ added in v0.1.24
type RemoteContext struct {
IP string
UserAgent string
RequestID string
SessionID string
// Headers is an allowlist of tracing headers only (e.g. X-Request-Id, Traceparent).
// It does not include Authorization, Cookie, or other sensitive request headers.
Headers map[string]string
}
RemoteContext provides HTTP request details to a remote action.
type RemoteRegistry ¶
type RemoteRegistry struct {
// contains filtered or unexported fields
}
RemoteRegistry is a registry for remote actions.
type RenderStrategy ¶
type RenderStrategy string
RenderStrategy defines how a page is rendered.
const ( // StrategySSR renders fresh on every request (default). StrategySSR RenderStrategy = "ssr" // StrategySSG renders once and caches forever (FIFO eviction via SSGCacheMaxEntries). StrategySSG RenderStrategy = "ssg" // StrategyISR renders once, then revalidates in the background after RevalidateAfter elapses // (stale-while-revalidate). Requires CacheTemplates: true. StrategyISR RenderStrategy = "isr" // StrategyPPR renders a static shell once and streams only named DynamicSlots // per-request. Requires CacheTemplates: true. StrategyPPR RenderStrategy = "ppr" )
type Route ¶
type Route struct {
// Path is the URL path pattern (e.g., /blog/:id)
Path string
// File is the absolute path to the .templ file
File string
// Type indicates the route type
Type RouteType
// Params are the extracted parameter names
Params []string
// IsDynamic indicates if the route has dynamic segments
IsDynamic bool
// IsCatchAll indicates if the route has a catch-all segment
IsCatchAll bool
// Priority is used for route matching order
Priority int
// Children are nested routes
Children []*Route
// Layout is the parent layout route
Layout *Route
// Middleware is the middleware chain for this route
Middleware []string
// contains filtered or unexported fields
}
Route represents a parsed route from the filesystem.
func (*Route) RouteRegex ¶
RouteRegex returns a regex pattern for the route. The regex is compiled once and cached for performance.
type RouteGroup ¶
type RouteGroup struct {
// Prefix is the URL prefix for all routes in the group
Prefix string
// Middleware is applied to all routes in the group
Middleware []Middleware
// Routes are the routes in this group
Routes []*ManualRoute
// Groups are nested route groups
Groups []*RouteGroup
// Parent is the parent group (nil for root)
Parent *RouteGroup
// Meta contains group metadata
Meta map[string]interface{}
// contains filtered or unexported fields
}
RouteGroup represents a group of routes with shared configuration.
func (*RouteGroup) DELETE ¶
func (g *RouteGroup) DELETE(path string, handler Handler, middleware ...Middleware) *ManualRoute
DELETE registers a DELETE route in the group.
func (*RouteGroup) GET ¶
func (g *RouteGroup) GET(path string, handler Handler, middleware ...Middleware) *ManualRoute
GET registers a GET route in the group.
func (*RouteGroup) Group ¶
func (g *RouteGroup) Group(prefix string, middleware ...Middleware) *RouteGroup
Group creates a nested route group.
func (*RouteGroup) PATCH ¶
func (g *RouteGroup) PATCH(path string, handler Handler, middleware ...Middleware) *ManualRoute
PATCH registers a PATCH route in the group.
func (*RouteGroup) POST ¶
func (g *RouteGroup) POST(path string, handler Handler, middleware ...Middleware) *ManualRoute
POST registers a POST route in the group.
func (*RouteGroup) PUT ¶
func (g *RouteGroup) PUT(path string, handler Handler, middleware ...Middleware) *ManualRoute
PUT registers a PUT route in the group.
func (*RouteGroup) Page ¶
func (g *RouteGroup) Page(path string, component templ.Component, middleware ...Middleware) *ManualRoute
Page registers a Templ component as a page in the group.
func (*RouteGroup) RegisterRoute ¶
func (g *RouteGroup) RegisterRoute(method, path string, handler Handler, middleware ...Middleware) *ManualRoute
RegisterRoute registers a route in the group.
func (*RouteGroup) Use ¶
func (g *RouteGroup) Use(middleware ...Middleware)
Use adds middleware to the group.
type RouteMatch ¶
type RouteMatch struct {
// Route is the matched route
Route *Route
// PathParams are the path parameters
PathParams Params
// QueryParams are the query parameters
QueryParams *QueryParams
// URL is the full URL
URL *url.URL
}
RouteMatch represents a matched route with parameters.
func NewRouteMatch ¶
func NewRouteMatch(route *Route, path string) (*RouteMatch, error)
NewRouteMatch creates a new route match.
func (*RouteMatch) Param ¶
func (rm *RouteMatch) Param(key string) string
Param returns a path parameter value.
func (*RouteMatch) Query ¶
func (rm *RouteMatch) Query(key string) string
Query returns a query parameter value.
type RouteOptions ¶
type RouteOptions struct {
Strategy RenderStrategy
// ISR: duration after which the cached page is considered stale.
// On a stale request the old page is served immediately and a background
// goroutine re-renders and updates the cache (stale-while-revalidate).
// Zero means "always revalidate" which behaves identically to SSR.
RevalidateAfter time.Duration
// PPR: names of dynamic slots that are excluded from the cached static shell
// and re-rendered per-request. Each name must match a slot registered with
// RegisterSlot for this page path.
DynamicSlots []string
// DeferredSlots are slots that are rendered out-of-order after the initial page load.
DeferredSlots []string
// RuntimeTier specifies the minimum client runtime tier required for this route.
RuntimeTier string
// Optional per-route rate limiter config.
RateLimit *RateLimitOptions
}
RouteOptions holds page-level options like rendering strategy.
func GetRouteOptions ¶
func GetRouteOptions(path string) RouteOptions
GetRouteOptions returns route options from the global registry.
type RouteType ¶
type RouteType int
RouteType represents the type of route.
const ( // RouteTypePage represents a page route. RouteTypePage RouteType = iota // RouteTypeLayout represents a layout component route RouteTypeLayout // RouteTypeError represents an error component route RouteTypeError // RouteTypeAPI represents an API component route RouteTypeAPI // RouteTypeMiddleware represents a middleware route RouteTypeMiddleware // RouteTypeLoading represents a loading component route RouteTypeLoading )
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router manages all routes.
func NewRouter ¶
func NewRouter(routesSource interface{}) *Router
NewRouter creates a new router with the given routes directory or filesystem. You can pass a string (directory path) or fs.FS.
func (*Router) GetErrorRoute ¶
GetErrorRoute returns the nearest error route for a given path.
func (*Router) GetLayouts ¶
GetLayouts returns all layout routes.
func (*Router) GetLoadings ¶ added in v0.1.24
GetLoadings returns all loading routes.
func (*Router) GetMiddlewares ¶ added in v0.1.24
GetMiddlewares returns all middleware routes.
func (*Router) MatchWithLayout ¶
MatchWithLayout matches a URL path and returns both the route and its layout chain.
func (*Router) ResolveLayoutChain ¶
ResolveLayoutChain resolves the complete layout chain for a matched route. It returns all layouts from root to the nearest parent, ordered root-first.
When the filesystem scan (Scan) found layout files those entries take priority. If the router was initialised without a routes directory, or the directory does not contain .templ source files (e.g. a production binary deployed without source), the global registry is consulted as a fallback so that layouts registered via generated init() code are still applied.
func (*Router) ResolveMiddlewareChain ¶ added in v0.1.24
ResolveMiddlewareChain resolves the complete middleware chain for a matched route. It returns all middlewares from root to the nearest parent, ordered root-first.