-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.py
More file actions
executable file
·83 lines (73 loc) · 2.3 KB
/
Copy pathmain.py
File metadata and controls
executable file
·83 lines (73 loc) · 2.3 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
import logging
from fastapi import FastAPI, Request, status
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse
import auth.urls
import candidates.urls
import database
import elections.urls
import event.urls
import nominees.urls
import officers.urls
import permission.urls
from constants import IS_PROD
logging.basicConfig(level=logging.DEBUG)
database.setup_database()
# Enable OpenAPI docs only for local development
if not IS_PROD:
print("Running local environment")
origins = [
"http://localhost:4200", # default Angular
"http://localhost:8080", # for existing applications/sites
]
app = FastAPI(
lifespan=database.lifespan,
title="CSSS Site Backend",
root_path="/api",
)
# if on production, disable viewing the docs
else:
print("Running production environment")
origins = [
"https://sfucsss.org",
"https://test.sfucsss.org",
"https://admin.sfucsss.org",
"https://madness.sfucsss.org",
]
app = FastAPI(
lifespan=database.lifespan,
title="CSSS Site Backend",
root_path="/api",
docs_url=None, # disables Swagger UI
redoc_url=None, # disables ReDoc
openapi_url=None, # disables OpenAPI schema
)
app.add_middleware(
CORSMiddleware, allow_origins=origins, allow_credentials=True, allow_methods=["*"], allow_headers=["*"]
)
app.include_router(auth.urls.router)
app.include_router(elections.urls.router)
app.include_router(candidates.urls.router)
app.include_router(nominees.urls.router)
app.include_router(officers.urls.router)
app.include_router(permission.urls.router)
app.include_router(event.urls.router)
@app.get("/")
async def read_root():
return {"message": "Hello! You might be lost, this is actually the sfucsss.org's backend api."}
@app.exception_handler(RequestValidationError)
async def validation_exception_handler(
_: Request,
exception: RequestValidationError,
):
return JSONResponse(
status_code=status.HTTP_422_UNPROCESSABLE_CONTENT,
content=jsonable_encoder(
{
"detail": exception.errors(),
"body": exception.body,
}
),
)