Routing
Define routes with Alata Router, controller methods, and FastAPI dependencies.
Alata's HTTP layer is FastAPI. Alata's Router extends FastAPI's APIRouter with direct controller registration and resource routes, while retaining its decorators, path parameters, and dependency system — and Alata adds the conventions around them: where route files live, how they are registered onto the one application kernel, and how routes reach services bound in the service container.
Use Alata Router or FastAPI APIRouter in routes/. Both support registration through your app's HTTP provider during the boot lifecycle.
Where Routes Live
A fresh application ships one route file, routes/api.py, containing a single top-level router:
from app.http.controllers.welcome_controller import WelcomeController
from alata.http import Router
api_router = Router(prefix="/api/v1")
api_router.get("/", WelcomeController().index)APIRouter, Depends, Request, Response and JSONResponse are the underlying toolkit's own objects, re-exported through alata.http so application code imports one namespace. Importing them from fastapi directly is equally supported — they are the same objects, and every FastAPI extension keeps working.
Controllers are plain classes with async methods:
from alata.http import Request
class WelcomeController:
async def index(self, request: Request):
return {"message": "Welcome to Alata Framework!"}Registering a bound method with api_router.get("/", WelcomeController().index) is equivalent to using the decorator form — it keeps handler logic in a controller class while the route file stays a thin, readable route table. Decorator-style function handlers work exactly the same way if you prefer them:
@api_router.get("/ping")
async def ping():
return {"pong": True}How Routes Are Registered
Routes are mounted onto the FastAPI kernel by your application's HTTP provider. The scaffolded app extends the framework's FastapiProvider and includes the router in boot():
from alata.http import FastapiProvider as HttpProvider
class FastapiProvider(HttpProvider):
def boot(self):
from app.exceptions.handler import Handler
from routes.api import api_router
Handler().bootstrap(self.app.fastapi)
self.app.fastapi.include_router(api_router)The chain from process start to a mounted route:
bootstrap/application.pybuilds theApplicationkernel with your provider list (the app'sFastapiProviderruns last).- The framework's
FastapiProvider.register()creates theFastAPIinstance and attaches it viaself.app.use_fastapi(fastapi). - Your
FastapiProvider.boot()imports the route modules and callsinclude_router. bootstrap/app.pyexposesapp = kernel.fastapifor the ASGI server (uvicorn main:appor./alata serve).
Because HTTP, console, and queue workers all load the same kernel, anything you bind in a provider is available to your route handlers no matter which entry point started the process.
Path Parameters
Declare path parameters as typed function arguments — conversion and validation are automatic, and a mismatched type returns a 422 response:
@api_router.get("/orders/{order_id}")
async def show_order(order_id: int):
return {"order_id": order_id}Query parameters are any typed arguments not present in the path:
@api_router.get("/orders")
async def list_orders(status: str | None = None, page: int = 1):
...Request Bodies
Type-hint a handler parameter with a FormRequest subclass and the JSON body is parsed, validated, whitespace-trimmed, and hardened against unknown fields before your code runs:
from alata.http.request import FormRequest, Field
class StoreOrderRequest(FormRequest):
product_id: int
quantity: int = Field(..., ge=1)@api_router.post("/orders", status_code=201)
async def store_order(request: StoreOrderRequest):
data = request.validated()
...Invalid bodies never reach your handler — the exception handler renders a structured 422 response. See Requests for the full FormRequest / FormResponse API.
Injecting Services into Routes
You have two complementary injection mechanisms.
Request-scoped values: Depends
Use FastAPI dependencies for values derived from the current request. The scaffold ships two in app/http/dependencies.py:
from alata.http import Depends
from app.http.dependencies import get_user_id, require_authenticated
@api_router.get("/me")
async def me(user_id: str = Depends(require_authenticated)):
return {"user_id": user_id}get_user_id reads the acting user from request.state (set by the identity middleware); require_authenticated raises a 401 when the caller is anonymous.
The scaffold reads identity headers for demonstration; it does not verify credentials. Replace this with validated session or token authentication before using these helpers to protect routes.
Application services: the container
Services registered by providers are resolved from the container. The container import is a proxy that always targets the current container, so it is safe to import at module level:
from alata.container import container
class ReportController:
async def index(self):
cache = container.make("cache")
...Or let type hints do the work with @inject, for any class registered via container.bind_type:
from alata.container import inject
from alata.log_manager import LogManager
@inject
async def index(log: LogManager):
log.info("Building report")
...The two mechanisms compose: use Depends for per-request data, the container for long-lived services.
Grouping and Prefixes
Group related routes into their own routers and compose them. Prefixes nest, and tags group the routes in the generated API docs:
from alata.http import APIRouter
orders_router = APIRouter(prefix="/orders", tags=["orders"])
@orders_router.get("/")
async def index():
...from routes.orders import orders_router
from alata.http import Router
api_router = Router(prefix="/api/v1")
api_router.include_router(orders_router) # served at /api/v1/orders/You can also apply a prefix at mount time — include_router(admin_router, prefix="/admin") — or attach shared guards to a whole group with router-level dependencies:
from alata.http import APIRouter, Depends
from app.http.dependencies import require_authenticated
admin_router = APIRouter(prefix="/admin", dependencies=[Depends(require_authenticated)])New route files are just modules — create routes/orders.py, then include its router either from routes/api.py or directly in your FastapiProvider.boot() with self.app.fastapi.include_router(orders_router).