-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
76 lines (63 loc) · 2.42 KB
/
main.py
File metadata and controls
76 lines (63 loc) · 2.42 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
import logging
from contextlib import asynccontextmanager
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
from src.config import settings
from src.db.client import prisma
from src.ehrbase.client import ehrbase_client
from src.ehrbase.templates import ensure_templates_registered
from src.encounters.router import router as encounters_router
from src.observations.router import router as observations_router
from src.patients.router import router as patients_router
logger = logging.getLogger(__name__)
@asynccontextmanager
async def lifespan(app: FastAPI):
# Startup - attempt database connection but don't fail if unavailable
logger.info(f"CORS allowed origins: {settings.cors_origins}")
try:
await prisma.connect()
logger.info("Database connected successfully")
except Exception as e:
logger.error(f"Failed to connect to database: {e}")
# Ensure openEHR templates are registered in EHRBase
try:
template_results = await ensure_templates_registered()
if template_results:
for template_id, success in template_results.items():
status = "registered" if success else "FAILED"
logger.info(f"Template {template_id}: {status}")
except Exception as e:
logger.warning(f"Could not ensure templates are registered: {e}")
yield
# Shutdown
if prisma.is_connected():
await prisma.disconnect()
await ehrbase_client.close()
app = FastAPI(
title="CIS API",
description="Clinical Information System on openEHR",
version="0.1.0",
lifespan=lifespan,
)
app.add_middleware(
CORSMiddleware,
allow_origins=settings.cors_origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(patients_router, prefix="/api/patients", tags=["patients"])
app.include_router(encounters_router, prefix="/api/encounters", tags=["encounters"])
app.include_router(observations_router, prefix="/api/observations", tags=["observations"])
@app.get("/health")
async def health_check():
db_connected = prisma.is_connected()
status = "healthy" if db_connected else "degraded"
response_data = {
"status": status,
"database": "connected" if db_connected else "disconnected",
}
if not db_connected:
return JSONResponse(status_code=503, content=response_data)
return response_data