Skip to content

Admin Console in Your Application

neozen serve runs the admin console on its own (see the self-hosted quickstart). An application can also serve it itself, at any path: create_admin_app(config) returns an ASGI application to mount.

Starlette

from starlette.applications import Starlette
from starlette.routing import Mount

from neozen.admin import AdminConfig, create_admin_app

console = create_admin_app(AdminConfig(db_url=DATABASE_URL, secret_key=SECRET_KEY))
app = Starlette(routes=[..., Mount("/admin", app=console)])

Mount tells the console where it is (the ASGI root_path), and its links, forms, assets and redirects follow. The console checks its own CSRF token: leave its path out of a CSRF middleware of the application's.

Litestar

Litestar's mounts don't set root_path: give the path in mount_path.

from typing import Any

from litestar import Litestar, asgi

console = create_admin_app(
    AdminConfig(db_url=DATABASE_URL, secret_key=SECRET_KEY, mount_path="/admin")
)


@asgi("/admin", is_mount=True, copy_scope=True)
async def admin(scope: Any, receive: Any, send: Any) -> None:
    await console(scope, receive, send)


app = Litestar(route_handlers=[..., admin])

Behind a proxy

A proxy serving the console under a path passes it on as the root path: uvicorn --root-path /admin, for instance. Nothing else is needed.

What the console does on its own

Mounted, the console doesn't get the application's startup events: it prepares its schema and built-in roles on the first request instead. It keeps its own session cookie (neozen_admin_session) and its own sign-in, separate from the application's.