-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
62 lines (46 loc) · 1.6 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
54
55
56
57
58
59
60
61
62
import time
from pathlib import Path
import uvicorn
from fastapi import FastAPI
from sqlalchemy.orm import Session
from fastapi import Request
from fastapi.responses import JSONResponse
from loguru import logger
from fastapi.staticfiles import StaticFiles
from database.config import SessionLocal, engine, Base
from database.curd import set_represent_flowers, set_admin
from router import photo, picture, admin
from utils.config import secrets
Base.metadata.create_all(bind=engine)
app = FastAPI()
app.include_router(photo.router)
app.include_router(picture.router)
app.include_router(admin.router)
app.mount("/static", StaticFiles(directory="static"), name="static")
# 분류용 꽃 세팅
@app.on_event("startup")
def init():
db: Session = SessionLocal()
set_represent_flowers(db)
set_admin(db, secrets["admin_password"])
# 미들웨어를 통한 통합 로깅 및 예외처리
@app.middleware("http")
async def request_middleware(request: Request, call_next):
logger.info("Request start")
# logger.info("headers = {}", request.headers)
try:
start_time = time.time()
response = await call_next(request)
process_time = time.time() - start_time
logger.info("Request Processing Time: {}s", round(process_time, 2))
return response
except Exception as e:
logger.warning(f"Request failed: {e}")
return JSONResponse(content={"error": str(e)}, status_code=400)
finally:
logger.info("Request end")
@app.get("/")
def welcome_page():
return "Hello! this is a Flooming REST API Server."
if __name__ == "__main__":
uvicorn.run(app)