-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
47 lines (39 loc) · 1.38 KB
/
main.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
import uvicorn
from fastapi.middleware.cors import CORSMiddleware
from fastapi import FastAPI
from routes.api import router as api_router
from sqlalchemy.orm import sessionmaker
from starlette.requests import Request
from db import engine
app = FastAPI()
# DB接続用のセッションクラス インスタンスが作成されると接続する
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
origins = [
"http://localhost:3000",
"http://localhost:8000",
"https://www.neotaskmanegement.com",
"https://neotaskmanegement.com",
"https://neo-task-management.vercel.app",
"https://task-management-front-end-xf6w.vercel.app",
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
app.include_router(api_router)
# Start the server when the code is executed
if __name__ == "__main__":
uvicorn.run("main:app", host="127.0.0.1", port=8001, log_level="info", reload=True)
print("running")
@app.middleware("http")
async def db_session_middleware(request: Request, call_next):
# each router will have request with state.db property
request.state.db = SessionLocal()
# proceed to the next middleware or the route handler
response = await call_next(request)
# route handler may return a response
request.state.db.close()
return response