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: RoutingClass

Base 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
apps: dict[str, RoutingClass]
base_dir: Path
config
dispatcher
lifespan
logger
openapi_info: dict[str, Any]
property request: BaseRequest | None

Current request from registry.

request_registry
resource_loader
property response: Response | None

Current response from request.

router
run()[source]

Run the server using Uvicorn.

Return type:

None

server_application
storage
sys_apps: dict[str, RoutingClass]

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: object

Handles server configuration loading and app instantiation.

property apps: SmartOptions | None

Apps configuration.

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.

Return type:

dict[str, tuple[str, str, dict[str, Any]]]

get_plugin_specs()[source]

Return {plugin_name: opts_dict} for all configured plugins.

Return type:

dict[str, dict[str, Any]]

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.

Return type:

dict[str, tuple[str, str, dict[str, Any]]]

property middleware: list[Any]

Middleware configuration list.

property openapi: dict[str, Any]

OpenAPI info as plain dict.

property plugins: SmartOptions | None

Plugins configuration.

property server: SmartOptions

Server options (host, port, reload, server_dir).

property server_dir: Path

Return resolved server directory path.

property sys_apps: SmartOptions | None

System apps configuration.