-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·69 lines (55 loc) · 1.87 KB
/
main.py
File metadata and controls
executable file
·69 lines (55 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import logging
import os
from contextlib import asynccontextmanager
from datetime import UTC
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from fastapi import FastAPI
from slowapi import _rate_limit_exceeded_handler
from app.routers.admin.routes import create_admin
from app.routers.router import setup_router as setup_router_v2
from app.services.database.database import AsyncSessionLocal, init_db
from app.services.email_jobs import send_daily_subscriptions_email
from app.services.limiter import limiter
logger = logging.getLogger(__name__)
def _resolve_timezone():
tz_name = os.getenv("APP_TIMEZONE")
if not tz_name:
return UTC
try:
return ZoneInfo(tz_name)
except ZoneInfoNotFoundError:
logger.warning(
"Fuso horário '%s' inválido. Revertendo para UTC.", tz_name
)
return UTC
@asynccontextmanager
async def lifespan(app: FastAPI):
# add check db file and create if not found
await init_db()
app.db_session_factory = AsyncSessionLocal()
scheduler = AsyncIOScheduler(timezone=_resolve_timezone())
scheduler.add_job(
send_daily_subscriptions_email,
CronTrigger(hour=3, minute=0),
id="daily_subscription_email_job",
name="Disparo diário de e-mails para assinantes",
replace_existing=True,
)
scheduler.start()
app.state.scheduler = scheduler
await create_admin(app.db_session_factory)
try:
yield
finally:
scheduler.shutdown(wait=False)
app = FastAPI(
lifespan=lifespan,
title="pynews-server",
description="PyNews Server",
)
app.state.limiter = limiter
app.add_exception_handler(429, _rate_limit_exceeded_handler)
app.include_router(setup_router_v2(), prefix="/api")
logger.info("PyNews Server Starter")