Skip to content

Commit 03f152b

Browse files
committed
feat: allow CORS for allowed origins
1 parent 8a0220f commit 03f152b

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,14 @@ tutor-log-api
3535
pip install -r requirements.txt
3636
```
3737

38-
3. Run the application:
38+
3. Set required envionment variables in `.env` file:
39+
```
40+
DATABASE_URL=<db_connection_string>
41+
SKIP_DATABASE_SETUP=true
42+
ALLOWED_ORIGINS=http://localhost:3000
43+
```
44+
45+
4. Run the application:
3946
```
4047
fastapi dev main.py
4148
```

main.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import os
22
from contextlib import asynccontextmanager
33
from fastapi import FastAPI
4+
from fastapi.middleware.cors import CORSMiddleware
45

56
from routers import base, user, pupil, group, event, payment
67
from database import create_db_and_tables
@@ -16,6 +17,15 @@ async def lifespan(app: FastAPI):
1617
version="1.0.0",
1718
lifespan=lifespan
1819
)
20+
origins = os.getenv("ALLOWED_ORIGINS", "").split(",")
21+
print(f"Allowed origins: {origins}")
22+
app.add_middleware(
23+
CORSMiddleware,
24+
allow_origins=origins,
25+
allow_credentials=True,
26+
allow_methods=["*"],
27+
allow_headers=["*"],
28+
)
1929

2030
app.include_router(base)
2131
app.include_router(user)

0 commit comments

Comments
 (0)