Skip to content

Commit f99648b

Browse files
committed
wip: add CORS
1 parent 2d27b11 commit f99648b

1 file changed

Lines changed: 23 additions & 14 deletions

File tree

src/main.py

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,57 @@
11
import logging
2-
import os
32

43
from fastapi import FastAPI, Request, status
54
from fastapi.encoders import jsonable_encoder
65
from fastapi.exceptions import RequestValidationError
6+
from fastapi.middleware.cors import CORSMiddleware
77
from fastapi.responses import JSONResponse
88

99
import auth.urls
1010
import database
1111
import elections.urls
1212
import officers.urls
1313
import permission.urls
14+
from constants import IS_PROD
1415

1516
logging.basicConfig(level=logging.DEBUG)
1617
database.setup_database()
1718

18-
_login_link = (
19-
"https://cas.sfu.ca/cas/login?service=" + (
20-
"http%3A%2F%2Flocalhost%3A8080"
21-
if os.environ.get("LOCAL") == "true"
22-
else "https%3A%2F%2Fnew.sfucsss.org"
23-
) + "%2Fapi%2Fauth%2Flogin%3Fredirect_path%3D%2Fapi%2Fapi%2Fdocs%2F%26redirect_fragment%3D"
24-
)
25-
2619
# Enable OpenAPI docs only for local development
27-
if os.environ.get("LOCAL") == "true":
20+
if not IS_PROD:
21+
print("Running local environment")
22+
origins = [
23+
"http://localhost:*", # default Angular
24+
"http://localhost:8080", # for existing applications/sites
25+
]
2826
app = FastAPI(
2927
lifespan=database.lifespan,
3028
title="CSSS Site Backend",
31-
description=f'To login, please click <a href="{_login_link}">here</a><br><br>To logout from CAS click <a href="https://cas.sfu.ca/cas/logout">here</a>',
3229
root_path="/api",
3330
)
34-
# if on production, disable vieweing the docs
31+
# if on production, disable viewing the docs
3532
else:
33+
print("Running production environment")
34+
origins = [
35+
"https://sfucsss.org",
36+
"https://test.sfucsss.org",
37+
"https://admin.sfucsss.org"
38+
]
3639
app = FastAPI(
3740
lifespan=database.lifespan,
3841
title="CSSS Site Backend",
39-
description=f'To login, please click <a href="{_login_link}">here</a><br><br>To logout from CAS click <a href="https://cas.sfu.ca/cas/logout">here</a>',
4042
root_path="/api",
4143
docs_url=None, # disables Swagger UI
4244
redoc_url=None, # disables ReDoc
4345
openapi_url=None # disables OpenAPI schema
4446
)
4547

48+
app.add_middleware(
49+
CORSMiddleware,
50+
allow_origins=origins,
51+
allow_credentials=True,
52+
allow_methods=["*"],
53+
allow_headers=["*"]
54+
)
4655

4756
app.include_router(auth.urls.router)
4857
app.include_router(elections.urls.router)
@@ -55,7 +64,7 @@ async def read_root():
5564

5665
@app.exception_handler(RequestValidationError)
5766
async def validation_exception_handler(
58-
request: Request,
67+
_: Request,
5968
exception: RequestValidationError,
6069
):
6170
return JSONResponse(

0 commit comments

Comments
 (0)