-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
97 lines (74 loc) · 2.85 KB
/
Copy pathmain.py
File metadata and controls
97 lines (74 loc) · 2.85 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
"""
EntityGuard - FastAPI Application Entry Point.
Copyright (C) 2026 Christopher Abanilla
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import logging
from contextlib import asynccontextmanager
from pathlib import Path
import uvicorn
from fastapi import FastAPI, Request
from fastapi.responses import RedirectResponse
from fastapi.staticfiles import StaticFiles
from src.admin import admin_router, get_current_user
from src.views import entityguard_router
# Logger
logger = logging.getLogger("uvicorn.error")
@asynccontextmanager
async def lifespan(app: FastAPI):
"""
Lifespan context manager for startup and shutdown events.
The database schema and seed data are managed exclusively by Alembic.
Run `uv run alembic upgrade head` before starting the application.
"""
# Startup
logger.info("EntityGuard starting up...")
yield
# Shutdown (if needed)
logger.info("Shutting down...")
def create_app():
"""
Create and configure the FastAPI application instance.
Returns:
FastAPI: Configured FastAPI application with registered routers
and health check endpoint.
"""
app = FastAPI(
title="EntityGuard",
description="Security layer for processing patient data according to GDPR & HIPAA",
version="1.0.0",
lifespan=lifespan
)
# Mount static files
static_dir = Path(__file__).parent / "src" / "static"
static_dir.mkdir(exist_ok=True)
app.mount("/static", StaticFiles(directory=str(static_dir)), name="static")
# Include routers
app.include_router(entityguard_router)
app.include_router(admin_router)
@app.get("/health")
async def health():
"""
Health check endpoint.
Returns:
dict: Status indicator showing the service is running.
"""
return {"status": "Service is running"}
@app.get("/")
async def root_redirect(request: Request):
"""Redirect root to the admin dashboard, or login if not authenticated."""
if get_current_user(request):
return RedirectResponse(url="/admin/dashboard", status_code=302)
return RedirectResponse(url="/admin/login", status_code=302)
return app
if __name__ == "__main__":
uvicorn.run(create_app, host="0.0.0.0", port=9500)