-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
67 lines (51 loc) · 1.61 KB
/
api.py
File metadata and controls
67 lines (51 loc) · 1.61 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
import os
from datetime import datetime
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from pydantic import BaseModel
from modules.db_service import init_db
from modules.newsletter_storage import NewsletterStorage
class NewsletterOut(BaseModel):
id: int
title: str
description: str | None
source_url: str | None
source_name: str | None
created_at: datetime
summary_json: dict
class NewsletterListOut(BaseModel):
total: int
items: list[NewsletterOut]
app = FastAPI(title="Newsletter Bot API")
frontend_origin = os.getenv("DASHBOARD_ORIGIN", "http://localhost:3000")
app.add_middleware(
CORSMiddleware,
allow_origins=[frontend_origin],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
SessionLocal = init_db()
storage = NewsletterStorage(SessionLocal)
@app.get("/api/health")
def health_check():
return {"status": "ok"}
@app.get("/api/newsletters", response_model=NewsletterListOut)
def list_newsletters(limit: int = 50, offset: int = 0):
limit = min(max(limit, 1), 200)
offset = max(offset, 0)
records = storage.list_summaries(limit=limit, offset=offset)
items = [
NewsletterOut(
id=record.id,
title=record.title,
description=record.description,
source_url=record.source_url,
source_name=record.source_name,
created_at=record.created_at,
summary_json=record.summary_json,
)
for record in records
]
total = len(items)
return NewsletterListOut(total=total, items=items)