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 HasLayout(path string) bool
- func HasPage(path string) bool
- func RegisterLayout(path string, fn LayoutFunc)
- func RegisterPage(path string, fn ComponentFunc)
- func RegisterPageWithOptions(path string, fn ComponentFunc, opts RouteOptions)
- func RegisterRemoteAction(name string, action RemoteActionFunc)
- func RegisterRootLayout(fn LayoutFunc)
- func ValidateParams(route *Route, params Params) error
- type ComponentFunc
- type Handler
- type LayoutChain
- type LayoutContext
- type LayoutData
- type LayoutFunc
- type LayoutNode
- type LayoutResolver
- type LayoutTree
- 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 *fiber.App) error
- func (mr *ManualRouter) SetAutoRouter(router *Router)
- type Middleware
- type ParamExtractor
- type Params
- func (p Params) Bool(key string) (bool, error)
- func (p Params) Clone() Params
- func (p Params) Delete(key string)
- func (p Params) Float64(key string) (float64, 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) IntDefault(key string, defaultValue int) int
- 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) 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) Set(key, value string)
- func (qp *QueryParams) ToMap() map[string][]string
- type Registry
- func (r *Registry) GetLayout(path string) LayoutFunc
- func (r *Registry) GetPage(path string) ComponentFunc
- func (r *Registry) GetRootLayout() LayoutFunc
- func (r *Registry) GetRouteOptions(path string) RouteOptions
- func (r *Registry) HasLayout(path string) bool
- func (r *Registry) HasPage(path string) bool
- func (r *Registry) RegisterLayout(path string, fn LayoutFunc)
- func (r *Registry) RegisterPage(path string, fn ComponentFunc)
- func (r *Registry) RegisterPageWithOptions(path string, fn ComponentFunc, opts RouteOptions)
- func (r *Registry) RegisterRootLayout(fn LayoutFunc)
- type RemoteActionFunc
- 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) 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) Scan() error
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 RegisterLayout ¶
func RegisterLayout(path string, fn LayoutFunc)
RegisterLayout registers a layout component 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)
RegisterRootLayout registers the root layout in the global registry.
func ValidateParams ¶
ValidateParams validates route parameters against a route.
Types ¶
type ComponentFunc ¶
ComponentFunc is a function that returns a templ.Component.
func GetPage ¶
func GetPage(path string) ComponentFunc
GetPage returns the page component from the global registry.
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 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{}
}
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 *fiber.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 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 ¶
FromJSON creates Params from JSON.
func (Params) GetDefault ¶
GetDefault returns a parameter value with a default.
func (Params) IntDefault ¶
IntDefault returns a parameter as an int with a default.
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) 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) ToMap ¶
func (qp *QueryParams) ToMap() map[string][]string
ToMap converts to a regular map.
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) GetLayout ¶
func (r *Registry) GetLayout(path string) LayoutFunc
GetLayout returns the layout component 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) RegisterLayout ¶
func (r *Registry) RegisterLayout(path string, fn LayoutFunc)
RegisterLayout registers a layout component 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)
RegisterRootLayout registers the root layout component.
type RemoteActionFunc ¶
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 RemoteRegistry ¶
type RemoteRegistry struct {
// contains filtered or unexported fields
}
type RenderStrategy ¶
type RenderStrategy string
RenderStrategy defines how a page is rendered.
const ( StrategySSR RenderStrategy = "ssr" StrategySSG RenderStrategy = "ssg" )
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
}
Route represents a parsed route from the filesystem.
func (*Route) RouteRegex ¶
RouteRegex returns a regex pattern for the route.
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
}
RouteOptions holds page-level options like rendering strategy.
func GetRouteOptions ¶
func GetRouteOptions(path string) RouteOptions
GetRouteOptions returns route options from the global registry.
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) 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.