Single part at the root, COMPOSE_FILE in .env
This commit is contained in:
1 parent
321e4b6b35
commit
aac037676c
86 files changed
+68
-27
No files matched your search
Whitespace-only changes.
+13
@@ -0,0 +1,13 @@
|
||||
import uvicorn
|
||||
|
||||
from utils.env import env
|
||||
|
||||
|
||||
def main() -> None:
|
||||
uvicorn.run(
|
||||
"api.app:app", host=env.api.host, port=env.api.port, workers=env.api.workers
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
+53
@@ -0,0 +1,53 @@
|
||||
from collections.abc import AsyncGenerator
|
||||
from contextlib import asynccontextmanager
|
||||
{% if serve_spa %}from pathlib import Path
|
||||
{% endif %}
|
||||
{% if use_dishka %}from dishka.integrations.fastapi import setup_dishka
|
||||
{% endif %}from fastapi import FastAPI
|
||||
from fastapi.middleware.cors import CORSMiddleware
|
||||
{% if serve_spa %}from fastapi.responses import FileResponse
|
||||
{% endif %}
|
||||
from api import routers
|
||||
{% if use_dishka %}from dependencies.container import container
|
||||
{% endif %}{% if database != 'none' %}from utils.db import init_db
|
||||
{% endif %}{% if serve_spa %}from utils.env import env
|
||||
{% endif %}from utils.logging import setup_logging
|
||||
{% if serve_spa %}
|
||||
IMMUTABLE_HEADERS = {"Cache-Control": "public, max-age=31536000, immutable"}
|
||||
NO_CACHE_HEADERS = {"Cache-Control": "no-cache"}
|
||||
{% endif %}
|
||||
|
||||
@asynccontextmanager
|
||||
async def lifespan(app_: FastAPI) -> AsyncGenerator[None]:
|
||||
setup_logging()
|
||||
{%- if database != 'none' %}
|
||||
await init_db()
|
||||
{%- endif %}
|
||||
yield
|
||||
{%- if use_dishka %}
|
||||
await app_.state.dishka_container.close()
|
||||
{%- endif %}
|
||||
|
||||
|
||||
app = FastAPI(title="{{ project_name }} API", lifespan=lifespan)
|
||||
|
||||
app.add_middleware(
|
||||
CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]
|
||||
)
|
||||
|
||||
app.include_router(routers.router)
|
||||
{% if serve_spa %}
|
||||
spa_dir = Path(env.api.static_dir).resolve()
|
||||
if spa_dir.is_dir():
|
||||
|
||||
@app.get("/{spa_path:path}")
|
||||
async def serve_spa(spa_path: str) -> FileResponse:
|
||||
candidate = (spa_dir / spa_path).resolve()
|
||||
if spa_path and candidate.is_relative_to(spa_dir) and candidate.is_file():
|
||||
immutable = spa_path.startswith("_app/immutable/")
|
||||
headers = IMMUTABLE_HEADERS if immutable else NO_CACHE_HEADERS
|
||||
return FileResponse(candidate, headers=headers)
|
||||
return FileResponse(spa_dir / "index.html", headers=NO_CACHE_HEADERS)
|
||||
{% endif %}
|
||||
{% if use_dishka %}setup_dishka(container, app)
|
||||
{% endif %}
|
||||
Whitespace-only changes.
Whitespace-only changes.
+6
@@ -0,0 +1,6 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from . import api
|
||||
|
||||
router = APIRouter()
|
||||
router.include_router(api.router, prefix="/api")
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
from fastapi import APIRouter
|
||||
|
||||
from . import health
|
||||
|
||||
router = APIRouter()
|
||||
router.include_router(health.router, prefix="/health")
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
{% if use_dishka and database == 'postgres' -%}
|
||||
from dishka.integrations.fastapi import DishkaRoute, FromDishka
|
||||
from fastapi import APIRouter
|
||||
from sqlalchemy import text
|
||||
from sqlmodel.ext.asyncio.session import AsyncSession
|
||||
|
||||
router = APIRouter(tags=["health"], route_class=DishkaRoute)
|
||||
|
||||
|
||||
@router.get("")
|
||||
@router.get("/")
|
||||
async def health(session: FromDishka[AsyncSession]) -> dict[str, bool]:
|
||||
result = await session.scalars(text("SELECT 1"))
|
||||
return {"db": result.first() == 1}
|
||||
{% elif use_dishka and database == 'mongo' -%}
|
||||
from dishka.integrations.fastapi import DishkaRoute, FromDishka
|
||||
from fastapi import APIRouter
|
||||
from pymongo import AsyncMongoClient
|
||||
|
||||
router = APIRouter(tags=["health"], route_class=DishkaRoute)
|
||||
|
||||
|
||||
@router.get("")
|
||||
@router.get("/")
|
||||
async def health(client: FromDishka[AsyncMongoClient]) -> dict[str, bool]:
|
||||
await client.admin.command("ping")
|
||||
return {"db": True}
|
||||
{% else -%}
|
||||
from fastapi import APIRouter
|
||||
|
||||
router = APIRouter(tags=["health"])
|
||||
|
||||
|
||||
@router.get("")
|
||||
@router.get("/")
|
||||
async def health() -> dict[str, bool]:
|
||||
return {"ok": True}
|
||||
{% endif -%}
|
||||
Reference in new issue
Block a user