FastAPI를 사용하기 위해선 ASGI서버인 uvicorn
이 필요하다.
AGSI서버가 뭔진 모르겠다만… 일단 그렇다고 하니 넘어가자.
가상환경에서 fastapi와 uvicorn을 pip로 설치해보자.
온갖게 딸려온다…
from fastapi import FastAPI
app = FastAPI() #fastAPI객체 생성
@app.get("/")
def read_root():
return {"message" : "hello world!"}
@app.get("/items/{item_id}")
def read_item(item_id : int, q: str = None):
return {"itme_id": item_id, "q": q}
이런 간단한 테스트용 api를 만들고 나서 uvicorn
을 사용해 서버를 실행한다.
uvicorn app:app --reload
api가 잘 응답하고 있다.
만약 파일명이 app.py가 아니라 main.py라면…
uvicorn main:app --reload
라고 서버를 시작할 수 있다. 공식홈페이지에 따르면 위 명령어는 다음과 같은 의미를 갖고 있다.
main
: main.py
파일 (파이썬 "모듈").app
: the object created inside of main.py
with the line app = FastAPI()
.