Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
118 lines (101 loc) · 3.77 KB
/
Copy pathmain.py
File metadata and controls
118 lines (101 loc) · 3.77 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# main.py
from fastapi import FastAPI
from database import engine, Base
from routers import snomed_filters, publications, datasets, projects, admin, auth_router, teams_router, tools
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")
from starlette.exceptions import HTTPException as StarletteHTTPException
@app.exception_handler(StarletteHTTPException)
async def http_exception_handler(request: Request, exc: StarletteHTTPException):
response = JSONResponse(
status_code=exc.status_code,
content={"message": exc.detail},
)
origin = request.headers.get("origin")
if origin:
response.headers["Access-Control-Allow-Origin"] = origin
response.headers["Access-Control-Allow-Credentials"] = "true"
response.headers["Access-Control-Allow-Methods"] = "*"
response.headers["Access-Control-Allow-Headers"] = "*"
return response
@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
)
response = JSONResponse(
status_code=500,
content={"message": message, "stack_trace": stack_trace, "correlation_id": correlation_id},
background=background_tasks
)
origin = request.headers.get("origin")
if origin:
response.headers["Access-Control-Allow-Origin"] = origin
response.headers["Access-Control-Allow-Credentials"] = "true"
response.headers["Access-Control-Allow-Methods"] = "*"
response.headers["Access-Control-Allow-Headers"] = "*"
return response
app.add_middleware(
CORSMiddleware,
allow_origins=[
"http://localhost:5173",
"http://127.0.0.1:5173",
"http://localhost:3000",
"http://127.0.0.1:3000",
"https://crukdatahub-production.up.railway.app",
"https://crukdatahub-staging.up.railway.app",
"https://crukdatahub-dev.up.railway.app",
],
allow_origin_regex=r"http://(localhost|127\.0\.0\.1)(:\d+)?",
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.include_router(tools.router)
@app.get("/")
def health_check():
return {"status": "active", "system": "CRUK Datahub Backend"}