fastapi 구현

hccho·2024년 8월 15일

python fastAPI

목록 보기
2/7

1. fastapi, uvicorn 설치

pip install fastapi uvicorn

2. app 폴더 생성 후 main.py 파일 작성

{프로젝트명}/
├── venv/
├── app/
│ └── main.py
└── requirements.txt

main.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
async def read_root():
    return {"message": "Hello, World!"}

@app.get("/items/{item_id}")
async def read_item(item_id: int, q: str = None):
    return {"item_id": item_id, "q": q}

3. app 폴더로 이동 후 FastAPI 서버 실행

cd app
uvicorn main:app --reload --host=0.0.0.0 --port=80

0개의 댓글