필자도 Fastapi를 처음 접했을 때, main.py에 test용 api를 만들고 풀 명령어를 이용하여 test를 했었다. (아래와 같이)
from fastapi import FastAPI
from typing import Union
app = FastAPI()
@app.get("/")
def root():
return {"hello root"}
@app.get("/world")
def world():
return {"hello world"}
@app.get("/table/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
명령어 : uvicorn main:app --reload --host=0.0.0.0 --port=8000
> 여기서 간략한 설명
uvicorn: 우리가 설치한 것으로 fastapi와 함께 사용한다. (python 에서 pip 처럼 fastapi도 uvicorn 이라고 붙여줌)
app : main.py 속의 app = FastAPI() 를 명칭
--reload : 코드 수정시 자동으로 저장되고 재시작 됨 (로컬에서 편리하나, 실제 서버에서는 cpu 사용률 때문에 고려해야함)
--host: 모든 접근이 가능하게 하려면 0.0.0.0 을 입력함
--port: 접속 원하는 포트를 지정해준다 (주로 8000 번을 사용 많이 함)
하지만, 본격적인 main.py를 만들기 위해서 mvc에서 사용되었던 router 초기값 , 미들웨어, 핸들러들을 main.py에 초기화를 시켜준다.
이때, app 앞에 언더바가 붙는 경우가 있는데, (_app) 이 변수는 '이 모듈 내부에서만 사용할 거다'라는 암묵적인 약속과 같음!
''' 라우터 초기화 '''
def router_init(_app: FastAPI) -> None:
try:
_app.include_router(api.router, prefix="/api")
except Exception as e:
logger.error(e)
다음 middleware 설정
- logger.info 를 이용하여 -> log 기록을 남김
- 어떤 request.method에서 error가 발생하는지 기록하는 부분 -> logger.error
- 미들웨어 내 error 발생시, 500으로 처리
""" 미들웨어 초기화 """
def middleware_init(_app: FastAPI) -> None:
@_app.middleware("http")
async def prc_middleware(request: Request, call_next):
try:
start_time = time.time()
response = await call_next(request)
end_time = time.time()
logger.info(f" {request.method} | {end_time - start_time:.3f}")
return response
except Exception as e:
logger.error(f"{request.method} {request.url} >> {e}")
return JSONResponse(
status_code=500,
content={"error_code": 500, "message": "Server Error"}
)
다음으로 app 변수 생성및 초기화
def create_app() -> FastAPI:
_app = FastAPI(
title="test",
description="test for mixed food",
version="1.0.0",
)
listener_init(_app=_app) # 앞 부분에서 핸들러 부분 (exception)
router_init(_app=_app)
middleware_init(_app=_app)
return _app
마지막으로 app 실행 !
app = create_app()
if __name__ == "__main__":
uvicorn.run("main:app", host='0.0.0.0', port=8000, reload=True)