FastAPI 는 무엇인가

Jay Chung·2022년 9월 24일
0

FastAPI

FastAPI는 파이썬 3.7 이상의 버전에서 API를 빌드하기위한 웹 프레임워크이다.

FastAPI의 특징

  1. 빠르다
    node.js나 Go에 대등할 정도로 매우 높은 성능
  2. 빠른 코드 작성
  3. 적은 버그
  4. 표준 기반
    OpenAPI 기반

FastAPI 설치

$ pip install fastapi
$ pip install "uvicorn[standard]"

FastAPI 실행

from typing import Optional

from fastapi import FastAPI

app = FastAPI()


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


@app.get("/items/{item_id}")
def read_item(item_id: int, q: Optional[str] = None):
    return {"item_id": item_id, "q": q}
$ python main.py
$ uvicorn main:app --reload

http://localhost:8000 접속하여 확인


{"Hello": "World"} 가 출력되는 것을 확인할 수 있다.

http://localhost:8000/items/3?q=what? 로 접속해본 경우 위와 같이 나온다.

FastAPI Doc

http://localhost:8000/docs 로 접속 시 다음과 같은 페이지를 확인할 수 있다.

Swagger를 사용한 대화형 API 문서로 여러 테스트 들이 가능하다.

FastAPI ReDoc

http://localhost:8000/redoc

조금 더 깔끔한 디자인을 가지고 있으나, 테스트를 해보는 기능은 제공되지 않는다.

0개의 댓글