-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
108 lines (86 loc) · 3.05 KB
/
app.py
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
import os
from typing import Annotated
import json
from dotenv import load_dotenv
from fastapi.responses import JSONResponse
if os.getenv("ENVIRONMENT") == "development":
load_dotenv()
from fastapi.middleware.cors import CORSMiddleware
from fastapi import Cookie, FastAPI, HTTPException, Response, Request, status
from contextlib import asynccontextmanager
from src.tasks.router import router as task_router
from src.tasks import models as tasks_model
from src.projects.router import router as project_router
from src.projects import models as projects_model
from src.users.router import router as users_router
from src.users import models as users_model
from src.settings.database import create_database
from src.settings.database import SessionLocal, engine
allow_origins = os.getenv("ORIGINS")
allow_origins = allow_origins.split(",")
allow_methods = ("*",)
allow_headers = ("*",)
@asynccontextmanager
async def lifespan(app: FastAPI):
create_database()
yield
app = FastAPI(lifespan=lifespan)
app.add_middleware(
CORSMiddleware,
allow_origins=allow_origins,
allow_methods=allow_methods,
allow_headers=allow_headers,
allow_credentials=True,
expose_headers=("*",),
)
@app.get("/healthcheck")
def health():
return "OK"
@app.get("/protected")
def protected(session: Annotated[str | None, Cookie()] = None):
print("protected route")
if session is None:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized"
)
user_data = json.loads(session)
user_data = dict(email=user_data["email"])
return JSONResponse(status_code=status.HTTP_200_OK, content=user_data)
@app.middleware("http")
async def db_session_middleware(request: Request, call_next):
try:
request.state.db = SessionLocal()
response = await call_next(request)
except Exception:
response = Response(
"Internal server error", status_code=status.HTTP_500_INTERNAL_SERVER_ERROR
)
finally:
request.state.db.close()
return response
@app.middleware("http")
async def http_session_middleware(request: Request, call_next):
if request.url.path in ["/healthcheck",]:
return await call_next(request)
try:
origin = request.headers["Origin"]
if origin not in allow_origins:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED, detail="Unauthorized"
)
response = await call_next(request)
response.headers["Access-Control-Allow-Credentials"] = "true"
response.headers["Access-Control-Allow-Origin"] = origin
response.headers["Vary"] = "Origin"
except HTTPException as e:
response = JSONResponse(
{"detail": e.detail}, status_code=e.status_code
)
except Exception as e:
response = Response(
f"Internal server error: {str(e)}", status_code=status.HTTP_500_INTERNAL_SERVER_ERROR
)
return response
app.include_router(task_router)
app.include_router(project_router)
app.include_router(users_router)