Skip to content

Commit 538846d

Browse files
authored
feat: add uvicorn app_factory, single worker (#44)
Adopt the app_factory + --factory pattern for serving the API. Ships a single worker for now; multi-worker is deferred because it is unsafe with SQLite storage and the in-memory AuthStateCache (concurrent-write "database is locked" errors; each worker would get its own auth cache).
1 parent f64dd45 commit 538846d

5 files changed

Lines changed: 13 additions & 3 deletions

File tree

packages/api/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ WORKDIR /app
3232
USER app
3333
EXPOSE 8000
3434

35-
CMD ["sh", "-c", "uvicorn api.main:app --host ${HOST} --port ${PORT} --log-level info"]
35+
CMD ["sh", "-c", "uvicorn api.main:app_factory --factory --host ${HOST} --port ${PORT} --log-level info"]

packages/api/dev.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ ENV HOST=0.0.0.0 PORT=8000 DEBUG=true
1818

1919
EXPOSE 8000
2020

21-
CMD ["uvicorn", "api.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload", "--log-level", "info"]
21+
CMD ["uvicorn", "api.main:app_factory", "--factory", "--host", "0.0.0.0", "--port", "8000", "--reload", "--log-level", "info"]

packages/api/run_server.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@
1919
print(f"Health check: http://{host}:{port}/api/health")
2020

2121
uvicorn.run(
22-
"api.main:app",
22+
"api.main:app_factory",
2323
host=host,
2424
port=port,
25+
factory=True,
2526
reload=debug,
2627
log_level="info" if not debug else "debug",
2728
)

packages/api/src/api/main.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,7 @@ async def lifespan(app: Litestar) -> AsyncGenerator[None, None]:
9393

9494

9595
app = create_app()
96+
97+
98+
def app_factory() -> Litestar:
99+
return create_app()

packages/api/tests/unit/test_main.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from datetime import datetime
22

33
from api.controllers.monitoring import health_check
4+
from api.main import app_factory
5+
from litestar import Litestar
46
from litestar.config.cors import CORSConfig
57
from litestar.testing import create_test_client
68

@@ -11,6 +13,9 @@ def test_it_creates_app_with_default_config(self, app):
1113
assert app is not None
1214
assert len(app.routes) > 0
1315

16+
def test_it_app_factory_returns_litestar_app(self):
17+
assert isinstance(app_factory(), Litestar)
18+
1419
def test_it_handles_cors_correctly(self):
1520
cors_config = CORSConfig(
1621
allow_origins=["*"],

0 commit comments

Comments
 (0)