Starlette Adapter¶
Module: Web — Module Guide Package:
pyfly.web.adapters.starletteBackend: Starlette 0.40+, Uvicorn 0.30+
Quick Start¶
Installation¶
Minimal Configuration¶
Minimal Example¶
from pyfly.container import rest_controller
from pyfly.web import request_mapping, get_mapping
@rest_controller
@request_mapping("/api/hello")
class HelloController:
@get_mapping("")
async def hello(self) -> dict:
return {"message": "Hello, World!"}
Configuration Reference¶
| Key | Type | Default | Description |
|---|---|---|---|
pyfly.web.adapter |
str |
"auto" |
Adapter selection (auto or starlette) |
pyfly.server.port |
int |
8080 |
Application server port |
pyfly.server.host |
str |
"0.0.0.0" |
Server bind address |
pyfly.management.server.port |
int |
9090 |
Management port serving actuator + admin separately (env PYFLY_MANAGEMENT_SERVER_PORT) |
pyfly.web.debug |
bool |
false |
Debug mode |
pyfly.web.docs.enabled |
bool |
true |
Enable OpenAPI / Swagger UI / ReDoc |
pyfly.web.actuator.enabled |
bool |
false |
Enable actuator endpoints |
Adapter-Specific Features¶
StarletteWebAdapter¶
Implements WebServerPort. Creates a Starlette Application with routes, middleware, and exception handlers wired from the DI container.
Controller Auto-Discovery¶
ControllerRegistrar scans the DI container for @rest_controller beans and registers their @get_mapping / @post_mapping / etc. routes with the Starlette router.
Parameter Resolution¶
ParameterResolver maps controller method parameters to HTTP request data:
Body[T]— JSON request body (Pydantic-validated)PathVar[T]— URL path variableQueryParam[T]— Query string parameterHeader[T]— HTTP header valueCookie[T]— Cookie value
WebFilter Chain¶
WebFilterChainMiddleware runs an ordered chain of filters on every request. Always-active built-in filters:
| Filter | Purpose |
|---|---|
RequestContextFilter |
Initializes request-scoped context (runs first) |
CorrelationFilter |
Stamps W3C trace context and correlation headers |
TransactionIdFilter |
Propagates or generates X-Transaction-Id |
RequestLoggingFilter |
Logs request method, path, status, and duration |
SecurityHeadersFilter |
Adds OWASP security headers |
Opt-in filters (registered as beans):
| Filter | Purpose |
|---|---|
SecurityFilter |
JWT Bearer token authentication |
HttpSecurityFilter |
URL-pattern authorization rules |
CsrfFilter |
Double-submit cookie CSRF protection |
OpenAPI & Documentation¶
When pyfly.web.docs.enabled is true:
/openapi.json— OpenAPI 3.1 spec (auto-generated from controllers)/docs— Swagger UI/redoc— ReDoc
Global Exception Handler¶
Maps PyFly exceptions to RFC 7807 error responses automatically. No @ControllerAdvice needed.
Actuator Routes¶
When pyfly.web.actuator.enabled is true, the adapter mounts actuator endpoints at /actuator/* via make_starlette_actuator_routes().
Testing¶
Use Starlette's TestClient or httpx.AsyncClient for integration tests:
from starlette.testclient import TestClient
def test_hello(app):
client = TestClient(app)
response = client.get("/api/hello")
assert response.status_code == 200
See Also¶
- Web Module Guide — Full API reference: controllers, routing, middleware, CORS, OpenAPI
- WebFilters Guide — Filter chain details
- Actuator Guide — Health checks and monitoring endpoints
- Adapter Catalog