-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
80 lines (59 loc) · 2.28 KB
/
main.py
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
70
71
72
73
74
75
76
77
78
79
80
import logging
from fastapi import FastAPI, Request
from fastapi.staticfiles import StaticFiles
from fastapi.responses import HTMLResponse, JSONResponse
from models import Subscriber, Unsubscriber
from subscription_service import subscribe, unsubscribe, get_subscriber_preferences
from utils import limiter, setup_rate_limiting
from apscheduler.schedulers.asyncio import AsyncIOScheduler
from apscheduler.triggers.cron import CronTrigger
from analytics import analytics
logging.basicConfig(
level=logging.INFO,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
app = FastAPI()
setup_rate_limiting(app)
app.mount("/static", StaticFiles(directory="."), name="static")
scheduler = AsyncIOScheduler()
@app.on_event("startup")
async def startup_event():
scheduler.start()
scheduler.add_job(send_scheduled_newsletter,
trigger=CronTrigger(day_of_week="mon", hour=9, minute=0))
scheduler.add_job(
analytics.update_analytics,
trigger=CronTrigger(hour='*')) # Update analytics every hour
@app.on_event("shutdown")
async def shutdown_event():
scheduler.shutdown()
@app.get("/", response_class=HTMLResponse)
async def root():
logger.info("Serving root endpoint")
with open("index.html", "r") as f:
return f.read()
@app.post("/subscribe")
@limiter.limit("3/minute")
async def subscribe_route(request: Request, subscriber: Subscriber):
result = await subscribe(subscriber)
await analytics.update_analytics()
return result
@app.post("/unsubscribe")
@limiter.limit("3/minute")
async def unsubscribe_route(request: Request, unsubscriber: Unsubscriber):
result = await unsubscribe(unsubscriber)
await analytics.update_analytics()
return result
@app.get("/analytics")
async def get_analytics():
await analytics.update_analytics()
return JSONResponse(analytics.get_analytics_report())
@app.get("/preferences/{email}")
async def get_preferences(email: str):
preferences = await get_subscriber_preferences(email)
if preferences is None:
return JSONResponse({"error": "Subscriber not found"}, status_code=404)
return JSONResponse({"preferences": preferences})
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)