FastAPI 개발일지/개발하기

FastAPI 개발일지6 - FastAPI 시작하기

hccode0419 2024. 9. 17. 19:56

1. vscode 실행하기

1) 먼저 github desktop에서 사용할 repository에 연결한다.

 

이후 상단바의 Repository > open in Visual Studio Code를 클릭

 

2) Ctrl + Shift + p -> Python: Select Interpreter 클릭

가상환경을 변경하는 작업이다.

우리가 만든 fastapi를 선택한다!

 

2. 디렉토리 구조

└─project-fastapi
    │  database.py
    │  main.py
    │  models.py
    │  README.md
    │
    ├─history
    │      history_crud.py
    │      history_router.py
    │      history_schema.py
    │
    ├─item
    │      item_crud.py
    │      item_router.py
    │      item_schema.py
    │
    └─user
           user_crud.py
           user_router.py
           user_schema.py

 

다음과 같이 디렉토리를 구성한다.

 

database : 데이터베이스를 관리

main : 실행시키기 위한 메인 파일

models : database 모델을 관리

 

history / history_crud : 이용내역의 create, read, update, delete를 과닐

history / history_router : 이용내역의 라우터 관리 

history / history_schema : 이용내역의 기본 스케마를 관리

 

item / item_crud : 아이템의 create, read, update, delete를 관리

item / item_router : 아이템의 라우터 관리

item / item_schema : 아이템의 기본 스케마를 관리

 

user / user_crud : 사용자의 create, read, update, delete를 관리

user / user_router : 사용자의 라우터 관리

user / user_schema : 사용자의 기본 스케마를 관리

 

 

3. FastAPI 시작하기

먼저 FastAPI 기본부터 시작하겠다.

다음과 같이 코드를 작성한 한다.

 

- main.py

from fastapi import FastAPI

app = FastAPI()

@app.get("/")
def root():
    return {"message":"hello UNTOC"}

 

이후 터미널에서 해당 명령어를 작성하면 FastAPI가 실행된다.

uvicorn main:app --reload

 

 

 

주소에 접속하면 다음과 같이 메시지가 뜬다!

 

그리고 

http://127.0.0.1:8000/docs 에 접속하면 FastAPI에서 자동으로 docs를 만들어준다

오늘은 FastAPI 맛만 보고 다음 포스터부터 진짜!! 쇼핑몰을 만들어보자!!