-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
89 lines (76 loc) · 2.63 KB
/
Copy pathmain.py
File metadata and controls
89 lines (76 loc) · 2.63 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# main.py
from fastapi import FastAPI
from database import engine, Base
from routers import snomed_filters, publications, datasets, projects, admin, auth_router, teams_router
from fastapi.middleware.cors import CORSMiddleware
from fastapi import Request, BackgroundTasks
from fastapi.responses import JSONResponse
import httpx
import uuid
import traceback
import os
MIDDLELAYER_URL = os.getenv("MIDDLELAYER_URL", "http://localhost:8002")
async def log_error_to_middlelayer(service_name: str, correlation_id: str, message: str, stack_trace: str):
try:
async with httpx.AsyncClient() as client:
await client.post(
f"{MIDDLELAYER_URL}/logs/error",
json={
"service_name": service_name,
"correlation_id": correlation_id,
"message": message,
"stack_trace": stack_trace
},
timeout=5.0
)
except Exception:
pass # Fail silently if the logger is down
from migrate_db import migrate
# Run database schema migrations if needed
migrate()
# Create tables
Base.metadata.create_all(bind=engine)
app = FastAPI(title="CRUK Datahub")
@app.exception_handler(Exception)
async def global_exception_handler(request: Request, exc: Exception):
correlation_id = str(uuid.uuid4())
message = str(exc)
stack_trace = traceback.format_exc()
# Fire and forget the logging task
background_tasks = BackgroundTasks()
background_tasks.add_task(
log_error_to_middlelayer,
service_name="basic_backend",
correlation_id=correlation_id,
message=message,
stack_trace=stack_trace
)
return JSONResponse(
status_code=500,
content={"message": "An unexpected error occurred.", "correlation_id": correlation_id},
background=background_tasks
)
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:5173",
"http://127.0.0.1:5173",
"https://crukdatahub-production.up.railway.app",
"https://crukdatahub-staging.up.railway.app",
"https://crukdatahub-dev.up.railway.app",
],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# Add the new auth router for the /token endpoint
app.include_router(auth_router.router)
app.include_router(datasets.router)
app.include_router(projects.router)
app.include_router(admin.router)
app.include_router(snomed_filters.router)
app.include_router(publications.router)
app.include_router(teams_router.router)
@app.get("/")
def health_check():
return {"status": "active", "system": "CRUK Datahub Backend"}