FastAPI External 라우팅 - 67

2023. 1. 17. 22:30·Develop/FastAPI
728x90
SMALL
Authentication Routing /  Todo Routing /  Prefix Routing /  External Routing /  Dependencies Routing

 

1. 라우팅 역할

  • 애플리케이션의 구조를 위한 유연한 도구
  • 확장 가능 한 아키텍처를 지원
  • 파일 구조화를 도움

 

2. 구성할 프로젝트 구조

TodoApp
main.py

database.py

models.py
TodoApp/routers TodoApp/company
auth.py todos.py companyapis.py dependencies.py

External 라우팅

TodoApp 하위에 company 디렉터리 생성

company 디렉토리 하위에 companyapis.py 생성

현재 디렉토리 구조

 

companyapis.py 파일 수정

from fastapi import APIRouter

router = APIRouter()

@router.get("/")
async def get_company_name():
    return {"company_name": "Example Company, LLC"}

@router.get("/employees")
async def number_of_employees():
    return 162

 

main.py 파일 수정

from fastapi import FastAPI
import models
from database import engine
from routers import auth, todos
#companyapi.py 삽입
from company import companyapis

app = FastAPI()

models.Base.metadata.create_all(bind=engine)

app.include_router(auth.router)
app.include_router(todos.router)
#라우터 추가
app.include_router(
    companyapis.router,
    prefix="/companyapis",
    tags=["companyapis"],
    responses={418: {"description": "Internal Use Only"}}
                   )

prefix: companyapis.py안의 API의 모든 path는 /companyapis로 시작하게 된다.
tags: companyapis.py의 라우터는 companyapis라는 태그를 가진다.

728x90
SMALL
저작자표시 비영리 변경금지 (새창열림)

'Develop > FastAPI' 카테고리의 다른 글

FastAPI Routing 실습 - 69  (0) 2023.01.18
FastAPI Dependencies 라우팅 - 68  (0) 2023.01.17
FastAPI Prefix 라우팅 - 66  (0) 2023.01.17
FastAPI Todo 라우팅(main.py 수정) 확장성 확인 - 65  (0) 2023.01.17
FastAPI 라우팅 역할 및 인증 라우팅- 64  (0) 2023.01.17
'Develop/FastAPI' 카테고리의 다른 글
  • FastAPI Routing 실습 - 69
  • FastAPI Dependencies 라우팅 - 68
  • FastAPI Prefix 라우팅 - 66
  • FastAPI Todo 라우팅(main.py 수정) 확장성 확인 - 65
동석해요
동석해요
공부하고 싶은게 많은, 사소한 IT 지식들 공유
    250x250
  • 동석해요
    개발로 자기계발
    동석해요
  • 전체
    오늘
    어제
    • 분류 전체보기 (226)
      • Develop (126)
        • 기초지식 (12)
        • FastAPI (102)
        • Django (11)
      • Database & Data (62)
        • 기초지식 (16)
        • MySQL (29)
        • PostgreSQL (8)
        • 데이터 분석 (9)
      • 인공지능 (11)
        • PyTorch (9)
      • Cloud (8)
        • AWS (4)
        • GCP (2)
      • 버그처리 (14)
      • 회고 & 일상 (5)
  • 인기 글

  • 최근 글

  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.3
동석해요
FastAPI External 라우팅 - 67
상단으로

티스토리툴바