๐ฏ ์ด์ ํฌ์คํ ์์ ์ด์ด์ง๋ ๋ด์ฉ์ ๋๋ค.
[FastAPI] FastAPI ์์ํ๊ธฐ: ์ค์น๋ถํฐ ๋ก์ปฌ ์๋ฒ ์คํ๊น์ง ๋ฐ๋ผํ๊ธฐ
Python์ผ๋ก ๋น ๋ฅด๊ฒ ๋ฐฑ์๋ API๋ฅผ ๋ง๋ค๊ณ ์ถ๋ค๋ฉด FastAPI๋ ํ๋ฅญํ ์ ํ์ ๋๋ค. ๋ณธ ํฌ์คํ ์์๋ FastAPI ์ค์น๋ถํฐ ๊ฐ๋จํ API ์๋ฒ ์คํ๊น์ง ๋จ๊ณ๋ณ๋ก ์ค๋ช ํ๊ฒ ์ต๋๋ค.๊ฐ๋ฐ ํ๊ฒฝ: Ubuntu 22.04 LTS(WSL)ํ์ด์ฌ
hgk5722.tistory.com
FastAPI๋ก ๊ฐ๋จํ API๋ฅผ ๋ง๋ค์๋ค๋ฉด, ์ด์ ๋ ํ๋ก์ ํธ๋ฅผ ๊ตฌ์กฐํํด๋ณด๋ ๋จ๊ณ์ ๋๋ค. ๊ท๋ชจ๊ฐ ์ปค์ง์๋ก ์ ์ง๋ณด์๊ฐ ์ด๋ ค์์ง๊ธฐ ๋๋ฌธ์, ์ฒด๊ณ์ ์ธ ํด๋ ๊ตฌ์ฑ์ผ๋ก ์ฝ๋๋ฅผ ๋ถ๋ฆฌํด ๋ณด๊ฒ ์ต๋๋ค.
์ด๋ฒ ๊ธ์์๋ ๋ผ์ฐํฐ, ์คํค๋ง, ์ ํธ ํจ์๊น์ง ๋ถ๋ฆฌ๋ FastAPI ํด๋ ๊ตฌ์กฐ ์์ ๋ฅผ ์๊ฐํ๊ณ , ์ค์ ์คํ ๋ฐ ํ ์คํธ ๋ฐฉ๋ฒ๊น์ง ์๋ดํด๋๋ฆฌ๊ฒ ์ต๋๋ค.
๋ผ์ฐํฐ, ์คํค๋ง ๊ด๋ จ ์ค๋ช ์ ์ด์ ๊ธ์ ์์ฑํด ๋์์ต๋๋ค.
[FastAPI] FastAPI router, schemas
FastAPI๋ฅผ ๊ณต๋ถํ๋๋ฐ router, schemas, ๊ฐ์ฒด(app)์ ๊ด๊ณ์ ๋ํด์ ์์๋ณด๊ฒ ์ต๋๋ค. ์๋ก ์ฐ๊ฒฐ๋์ด ์๋ ๊ด๊ณ๋ค์ ๋ํด ์ดํดํ ๋ด์ฉ์ ์์ฑํ์ต๋๋ค.1. FastAPI RouterFastAPI๋ ์๋์ ๊ฐ์ด ๋จ์ํ๊ฒ API ์ค๊ณ
hgk5722.tistory.com
1๏ธโฃ. ํด๋ ๊ตฌ์กฐ ์ค๋ช
์๋์ ๊ฐ์ ํด๋ ๊ตฌ์กฐ๋ฅผ ๋ง๋ค๊ฒ ์ต๋๋ค.
fastapi-v1/
โโโ app_v1/
โโโ main.py
โโโ router/
โ โโโ __init__.py โ
๋ผ์ฐํฐ ๋ฑ๋ก์ฉ
โ โโโ hello.py
โโโ schemas/
โ โโโ hello.py
โโโ utilities/
โ โโโ helpers.py
โโโ config.py
๐ ๊ด๋ จ ์ฝ๋ ๊นํ๋ธ์์ ํ๋ฒ์ ํ์ธํ๊ธฐ
FastAPI-v1/app_v1 at main · hgk0404-v2/FastAPI-v1
๊ธฐ์ ๋ธ๋ก๊ทธ์ ์์ฑํ FastAPI ๊ด๋ จ ํฌ์คํ ์ฝ๋๋ฅผ ์์ฑํ ๋ ํฌ์งํ ๋ฆฌ์ ๋๋ค. Contribute to hgk0404-v2/FastAPI-v1 development by creating an account on GitHub.
github.com
2๏ธโฃ. ์ฝ๋
๐น 1. app_v1/main.py
from fastapi import FastAPI
from routers import hello
import uvicorn
app = FastAPI()
# ๋ผ์ฐํฐ ๋ฑ๋ก
app.include_router(hello.router)
if __name__ == "__main__":
uvicorn.run("main:app", host="0.0.0.0", port=8081, reload=True)
๐น 2. app_v1/ config.py
# ์ค์ ์์
APP_NAME = "FastAPI Example"
VERSION = "0.1.0"
๐น 3. routers/hello.py
from fastapi import APIRouter
from schemas.hello import HelloRequest, HelloResponse
from utilities.helpers import greet_user
router = APIRouter(prefix="/hello", tags=["hello"])
@router.post("/", response_model=HelloResponse)
def say_hello(req: HelloRequest):
message = greet_user(req.name)
return HelloResponse(message=message)
๐น 3-1. routers/__init__.py
๋ชจ๋ํ๋ฅผ ์ํ ํ์ผ์ผ๋ก ๊ทธ๋ฅ ๋น ํ์ผ๋ก ๋๋์๋ฉด ๋ฉ๋๋ค.
๐น 4. schemas/hello.py
from pydantic import BaseModel
class HelloRequest(BaseModel):
name: str
class HelloResponse(BaseModel):
message: str
๐น 5. utilities/helpers.py
def greet_user(name: str) -> str:
return f"Hello, {name}! ๐"
3๏ธโฃ. ์คํ
์ ๋ฒ ํฌ์คํ ๋ ์์ฑํ๋ ๊ฐ์ํ๊ฒฝ์ ๊ทธ๋๋ก ์ฌ์ฉํ๊ฒ ์ต๋๋ค.
# ์คํ (ํฌํธ๋ 8081๋ฒ)
python main.py

OpenAPI๋ก ์ ์ํด ๋ณด๊ฒ ์ต๋๋ค.
http://127.0.0.1:8081/docs

OpenAPI๋ก Post /hello์ ์ ์ํ์์ต๋๋ค.
4๏ธโฃ. ํ ์คํธ ๋ฐฉ๋ฒ
Try it out์ ์
๋ ฅํ์ฌ "name"์ ์ํ๋ ๋ด์ฉ์ ์
๋ ฅํ ๋ค execute๋ฅผ ์คํํฉ๋๋ค.

์ ๋ name์ value์ "๋ฉ๋ก "์ ๋ฃ์๊ณ

๊ฒฐ๊ณผ:
- Response body์ "๋ฉ๋ก "์ด ์ถ๋ ฅ๋๋ ๊ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
- routers/hello.py์์ from utilities.helpers import greet_uesr๋ฅผ ํด์ฃผ์๊ณ , helpers.py์์ greet_user๊ฐ ์ธ์๋ก "๋ฉ๋ก "์ ๋ฐ์๊ธฐ ๋๋ฌธ์ ์์ ๊ฐ์ด ์ถ๋ ฅ ๋์์ต๋๋ค.
์ด๋ ๊ฒ Swagger UI๋ฅผ ํตํด ๋ฐ๋ก ํ ์คํธ ๊ฐ๋ฅํ๋ฉฐ, request/response ๋ชจ๋ธ๋ ์๋์ผ๋ก ํ์ธํ ์ ์์ต๋๋ค.
โ๏ธ ํด๋ ๋ถ๋ฆฌ:
- ์ง์ ์ (main), ๋ผ์ฐํฐ, ์คํค๋ง, ์ ํธ ๋ถ๋ฆฌ
- Uvicorn์ผ๋ก ์คํํ๊ณ Swagger๋ก ํ ์คํธ
๋ค์ ์๊ฐ์ ์ด ๊ตฌ์กฐ ์์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๋, ์ธ์ฆ ๊ธฐ๋ฅ, ๋ฏธ๋ค์จ์ด ์ค์ ๋ค์ ์ถ๊ฐํ๋ ํ๋ก์ ํธ ๊ตฌ์กฐ๋ฅผ ๋ง๋ค์ด ๋ณด๊ฒ ์ต๋๋ค.
'[MLOps] > [FastAPI]' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [FastAPI] FastAPI ์์ํ๊ธฐ: ์ค์น๋ถํฐ ๋ก์ปฌ ์๋ฒ ์คํ๊น์ง ๋ฐ๋ผํ๊ธฐ (0) | 2025.07.21 |
|---|---|
| [FastAPI] FastAPI router, schemas (0) | 2025.07.18 |