Override-based compose, profiles in .env, README

This commit is contained in:
hh
2026-09-09 00:18:15 +02:00
parent 1f416dbe60
commit ac0ccf47e5
13 changed files with 131 additions and 88 deletions
@@ -69,6 +69,7 @@ class ApiSettings(BaseSettings):
host: str = "0.0.0.0" # noqa: S104
port: int = 8080
workers: int = 1
docs: bool = False
{%- if serve_spa %}
static_dir: str = "static"
{%- endif %}
@@ -110,7 +111,10 @@ class Settings(BaseSettings):
{%- endif %}
model_config = SettingsConfigDict(
case_sensitive=False, env_file=".env", env_nested_delimiter="__", extra="ignore"
case_sensitive=False,
env_file={% if layout == 'part' %}("../.env", ".env"){% else %}".env"{% endif %},
env_nested_delimiter="__",
extra="ignore",
)
@@ -5,7 +5,11 @@ 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
"api.app:app",
host=env.api.host,
port=env.api.port,
workers=env.api.workers,
forwarded_allow_ips="*",
)
@@ -10,8 +10,8 @@ from fastapi.middleware.cors import CORSMiddleware
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
{% endif %}from utils.env import env
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"}
@@ -29,7 +29,11 @@ async def lifespan(app_: FastAPI) -> AsyncGenerator[None]:
{%- endif %}
app = FastAPI(title="{{ project_name }} API", lifespan=lifespan)
app = FastAPI(
title="{{ project_name }} API",
lifespan=lifespan,
openapi_url="/openapi.json" if env.api.docs else None,
)
app.add_middleware(
CORSMiddleware, allow_origins=["*"], allow_methods=["*"], allow_headers=["*"]
@@ -1,5 +1,6 @@
import asyncio
import contextlib
import signal
import anyio
import uvloop
@@ -11,8 +12,15 @@ import uvloop
setup_logging()
SCAN_INTERVAL = 10
async def runner() -> None:
stop = asyncio.Event()
loop = asyncio.get_running_loop()
for sig in (signal.SIGINT, signal.SIGTERM):
loop.add_signal_handler(sig, stop.set)
{% if database != 'none' %} await init_db()
{% endif %} sessions_dir = anyio.Path("sessions")
@@ -22,7 +30,7 @@ async def runner() -> None:
clients: dict[int, PyroClient] = {}
try:
while True:
while not stop.is_set():
current = {path.name async for path in sessions_dir.glob("*.session")}
for session_file in current - started:
@@ -43,7 +51,8 @@ async def runner() -> None:
if me:
clients[me.id] = client
await asyncio.sleep(10)
with contextlib.suppress(TimeoutError):
await asyncio.wait_for(stop.wait(), SCAN_INTERVAL)
finally:
for client in clients.values():
with contextlib.suppress(Exception):
@@ -55,8 +64,5 @@ def main() -> None:
uvloop.install()
logger.info("Starting...")
with contextlib.suppress(KeyboardInterrupt):
asyncio.run(runner())
asyncio.run(runner())
logger.info("[red]Stopped.[/]")
@@ -1,5 +1,5 @@
import asyncio
import contextlib
import signal
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.interval import IntervalTrigger
@@ -15,6 +15,10 @@ async def example_job() -> None:
async def runner() -> None:
stop = asyncio.Event()
loop = asyncio.get_running_loop()
for sig in (signal.SIGINT, signal.SIGTERM):
loop.add_signal_handler(sig, stop.set)
{%- if database != 'none' %}
await init_db()
{%- endif %}
@@ -27,14 +31,13 @@ async def runner() -> None:
scheduler.start()
logger.info("Scheduler started")
with contextlib.suppress(asyncio.CancelledError):
await asyncio.Event().wait()
await stop.wait()
scheduler.shutdown()
def main() -> None:
setup_logging()
logger.info("Starting...")
with contextlib.suppress(KeyboardInterrupt):
asyncio.run(runner())
asyncio.run(runner())
logger.info("[red]Stopped.[/]")