-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.py
53 lines (39 loc) · 1.46 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
48
49
50
51
52
53
from fastapi import FastAPI
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles
from starlette.requests import Request
from db import database
from users.router import router as users_router
from answer.router import router as answer_router
from problems.router import router as problem_router
DEBUG_MODE = False
app = FastAPI(
docs_url="/docs" if DEBUG_MODE else None,
redoc_url="/redoc" if DEBUG_MODE else None,
)
# 起動時にDatabaseに接続する。
@app.on_event("startup")
async def startup():
await database.connect()
# 終了時にDatabaseを切断する。
@app.on_event("shutdown")
async def shutdown():
await database.disconnect()
# users routerを登録する。
app.include_router(users_router)
# answer routerを登録する。
app.include_router(answer_router)
# problem routerを登録する。
app.include_router(problem_router)
templates = Jinja2Templates(directory="templates")
app.mount("/static", StaticFiles(directory="static"), name="static")
@app.get('/', response_class=HTMLResponse)
def index(request: Request):
return templates.TemplateResponse("index.html", {"request": request})
# middleware state.connectionにdatabaseオブジェクトをセットする。
@app.middleware("http")
async def db_session_middleware(request: Request, call_next):
request.state.connection = database
response = await call_next(request)
return response