Advanced Features
WSGI Application Mounting
Mount legacy WSGI applications:
1
2
3
4
5
6
7
8
9
10
11
12
13
14 | from fenrir import Fenrir, WsgiToAsgi
app = Fenrir()
# Legacy WSGI app
def wsgi_app(environ, start_response):
status = '200 OK'
headers = [('Content-Type', 'text/plain')]
start_response(status, headers)
return [b'Hello from WSGI']
# Mount WSGI app
wsgi_asgi = WsgiToAsgi(wsgi_app)
app.mount_wsgi("/legacy", wsgi_asgi)
|
Framework Compatibility Modes
Bottle Compatibility
| from fenrir import install_bottle_compat
install_bottle_compat()
# Now use Bottle-style features
from fenrir.bottle import Bottle
bottle_app = Bottle()
|
Falcon Compatibility
| from fenrir import install_falcon_compat
install_falcon_compat()
# Now use Falcon-style features
import fenrir.falcon as falcon
|
Sanic Compatibility
| from fenrir import install_sanic_compat
install_sanic_compat()
# Now use Sanic-style features
import fenrir.sanic as sanic
|
Response Models
| from pydantic import BaseModel
class Item(BaseModel):
id: int
name: str
price: float
@app.get("/items/<item_id:int>", response_model=Item)
async def get_item(item_id: int):
return {"id": item_id, "name": "Item", "price": 9.99}
|
Event Listeners
| @app.listener("before_server_start")
async def startup(app_instance):
print("Server starting")
@app.listener("after_server_stop")
async def shutdown(app_instance):
print("Server stopping")
|
Custom Response Models with Include/Exclude
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 | class User(BaseModel):
id: int
name: str
email: str
password: str
@app.get(
"/users/<user_id:int>",
response_model=User,
response_model_exclude={"password"}
)
async def get_user(user_id: int):
return User(
id=user_id,
name="John",
email="john@example.com",
password="secret"
)
|