Server
Core ASGI server classes.
AsgiServer
ASGI Server - main entry point for genro-asgi applications.
AsgiServer is the central coordinator that: - Loads configuration from YAML files (config.yaml) - Mounts applications (AsgiApplication subclasses) - Builds the middleware chain (auth, cors, errors) - Handles ASGI lifespan protocol (startup/shutdown) - Routes requests via genro-routes Router
- Usage:
from genro_asgi import AsgiServer
server = AsgiServer(server_dir=”.”) server.run() # Starts uvicorn
- Configuration (config.yaml):
- server:
host: “127.0.0.1” port: 8000 reload: true
- middleware:
cors: on auth: on
- apps:
- shop:
module: “shop_app:Application”
- openapi:
title: “My API” version: “1.0.0”
- App naming convention:
File: {name}_app.py (e.g., shop_app.py)
Class: Application
If convention is followed, module can be omitted in config
- Architecture:
- AsgiServer(RoutingClass)
├── config: ServerConfig ├── router: Router (genro-routes) ├── dispatcher: Middleware chain → Dispatcher ├── lifespan: ServerLifespan ├── server_application: ServerApplication (system endpoints) ├── sys_apps: dict[str, RoutingClass] (system apps) ├── apps: dict[str, AsgiApplication] (user apps) ├── storage: LocalStorage └── resource_loader: ResourceLoader
- Request flow:
- ASGI Server (uvicorn) → AsgiServer.__call__
→ Middleware chain (errors → cors → auth) → Dispatcher → router.node(path, auth_tags) → handler(**query) → response.set_result()
- class genro_asgi.server.server.AsgiServer(server_dir=None, host=None, port=None, reload=None, argv=None)[source]
Bases:
RoutingClassBase ASGI server with routing via genro_routes.
- config
ServerConfig for configuration.
- router
genro_routes Router for dispatch.
- dispatcher
Dispatcher handling request routing.
- lifespan
ServerLifespan for startup/shutdown.
- server_application
ServerApplication with system endpoints.
- sys_apps
Dict for system apps (mounted at /_sys/).
- apps
Dict for user apps.
- logger
Server logger instance.
- request_registry
RequestRegistry for tracking requests.
- request
Current request (from ContextVar).
- response
Current response builder.
- __init__(server_dir=None, host=None, port=None, reload=None, argv=None)[source]
Initialize AsgiServer.
- app_loader
- config
- dispatcher
- lifespan
- logger
- property request: BaseRequest | None
Current request from registry.
- request_registry
- resource_loader
- router
- server_application
- storage
ServerConfig
Server configuration - handles all config loading and app instantiation.
- class genro_asgi.server.server_config.ServerConfig(server_dir=None, host=None, port=None, reload=None, argv=None)[source]
Bases:
objectHandles server configuration loading and app instantiation.
- get_app_specs_raw()[source]
Return {name: (module_name, class_name, kwargs)} for all configured apps.
Returns raw config data without importing. Server uses AppLoader to import.
- get_sys_app_specs_raw()[source]
Return {name: (module_name, class_name, kwargs)} for system apps.
Same format as get_app_specs_raw but for sys_apps config section.
- property server: SmartOptions
Server options (host, port, reload, server_dir).