opencode-config/.opencode/templates/backend/{{cookiecutter.project_name}}/main.py
Sergey 35b6c6e542
feat(infra): cookiecutter templates backend fullstack cli (#235)
* feat(infra): backend cookiecutter template with fastapi tortoise

* feat(infra): cli cookiecutter template with typer entry point

* feat(infra): fullstack cookiecutter template with sveltekit frontend

* test(infra): cookiecutter template tests and render fixes

* style(infra): ruff cleanup for cookiecutter template tests

* style(infra): ruff format cookiecutter template tests

* fix(infra): handle use_db no use_auth no in cookiecutter

---------

Co-authored-by: opencode-agent <agent@opencode.local>
2026-08-03 17:06:24 +03:00

42 lines
No EOL
1.1 KiB
Python

"""FastAPI application entry point for {{ cookiecutter.project_name }}."""
from __future__ import annotations
from contextlib import asynccontextmanager
from pathlib import Path
from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles
from {{ cookiecutter.project_name }}.config.logger import setup_logging
{% if cookiecutter.use_db == "yes" %}
from {{ cookiecutter.project_name }}.db.connection import close_db, init_db
{% endif %}
from {{ cookiecutter.project_name }}.api.router import api_router
_BASE_DIR = Path(__file__).resolve().parent
@asynccontextmanager
async def lifespan(app: FastAPI):
{% if cookiecutter.use_db == "yes" %}setup_logging()
await init_db()
try:
yield
finally:
await close_db(){% else %}setup_logging()
yield{% endif %}
app = FastAPI(lifespan=lifespan)
static_dir = _BASE_DIR / "static"
static_dir.mkdir(exist_ok=True)
app.mount("/static", StaticFiles(directory=static_dir), name="static")
app.include_router(api_router, prefix="/api")
@app.get("/health")
async def health() -> dict[str, str]:
return {"status": "ok"}