Routing¶
Route definition, path parameter extraction, and URL reversing.
Route¶
Represents a registered route with path parameter support.
Routes can be named for URL reversing:
# Auto-named from function name
@app.gemini("/user/{id}")
def user_profile(request, id): # name="user_profile"
pass
# Explicit name
@app.gemini("/u/{id}", name="user_detail")
def handler(request, id):
pass
Route
¶
Route(
path: str,
handler: Callable[..., Any],
*,
name: str | None = None,
input_prompt: str | None = None,
sensitive_input: bool = False,
)
Represents a registered route.
Routes match URL paths and extract parameters based on the path template.
Example
route = Route("/user/{username}", handler_func) if route.matches("/user/alice"): params = route.extract_params("/user/alice") # params = {"username": "alice"}
Create a new route.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Path template with optional parameters (e.g., "/user/{id}"). |
required |
handler
|
Callable[..., Any]
|
The handler function to call. |
required |
name
|
str | None
|
Route name for URL reversing. Defaults to handler function name. |
None
|
input_prompt
|
str | None
|
If set, request input with this prompt before calling handler. |
None
|
sensitive_input
|
bool
|
If True, use status 11 (sensitive input) instead of 10. |
False
|
Source code in src/xitzin/routing.py
call_handler
async
¶
Call the handler with the request and extracted parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request
|
Request
|
The current request. |
required |
params
|
dict[str, Any]
|
Extracted path parameters. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The handler's return value. |
Source code in src/xitzin/routing.py
extract_params
¶
Extract and type-convert path parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
URL path to extract parameters from. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary of parameter names to values. |
Source code in src/xitzin/routing.py
matches
¶
Check if this route matches the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
URL path to match. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the path matches this route's pattern. |
Source code in src/xitzin/routing.py
reverse
¶
Build URL from this route's path template.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**params
|
Any
|
Path parameters to substitute. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
URL path string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If required parameters are missing. |
Example
route = Route("/user/{username}", handler) route.reverse(username="alice") # Returns "/user/alice"
Source code in src/xitzin/routing.py
Router¶
Collection of routes with matching logic and URL reversing.
Router
¶
Collection of routes with matching logic.
Routes are matched in registration order; first match wins. Mounted routes are checked before regular routes.
Source code in src/xitzin/routing.py
add_mounted_route
¶
Add a mounted route to the router.
Mounted routes are checked before regular routes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
route
|
MountedRoute
|
The mounted route to add. |
required |
add_route
¶
Add a route to the router.
Raises:
| Type | Description |
|---|---|
ValueError
|
If a route with the same name already exists. |
Source code in src/xitzin/routing.py
add_titan_route
¶
Add a Titan upload route to the router.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
route
|
TitanRoute
|
The Titan route to add. |
required |
has_titan_routes
¶
match
¶
Find a matching route and extract parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
URL path to match. |
required |
Returns:
| Type | Description |
|---|---|
tuple[Route, dict[str, Any]] | None
|
Tuple of (route, params) if found, None otherwise. |
Source code in src/xitzin/routing.py
match_mount
¶
Find a matching mounted route and extract path info.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
URL path to match. |
required |
Returns:
| Type | Description |
|---|---|
tuple[MountedRoute, str] | None
|
Tuple of (mounted_route, path_info) if found, None otherwise. |
Source code in src/xitzin/routing.py
match_titan
¶
Find a matching Titan route and extract parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
URL path to match. |
required |
Returns:
| Type | Description |
|---|---|
tuple[TitanRoute, dict[str, Any]] | None
|
Tuple of (titan_route, params) if found, None otherwise. |
Source code in src/xitzin/routing.py
reverse
¶
Build URL for a named route.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Route name. |
required |
**params
|
Any
|
Path parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
URL path string. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If route name not found or parameters missing. |