본문 바로가기

FastAPI7

Jinja2 템플릿 활용 1) style.css 파일 적용from fastapi.staticfiles import StaticFilesfrom fastapi import FastAPIapp = FastAPI()app.mount("/static", StaticFiles(directory="static"), name="static")FastAPI가 초기화된 위치를 기준으로 /static 폴더의 경로를 설정한다.html 코드 내에서 아래와 같이 호출이 가능하다. 2) Jinja2Template 예제from pathlib import Pathfrom fastapi.templating import Jinja2Templatesfrom fastapi.responses import HTMLResponsefrom fastapi import AP.. 2024. 5. 8.
SpringDataJPA 일부 구현해보기 fetchJoin을 통해 eager loading을 사용하기 보다는 select 문에 객체들을 선언하고 명시적으로 조인을 사용하는 방식을 더 선호해서 사용하지 않지만, 깃허브에서 좋은 내용을 봐서 작성해봅니다.from typing import Generic, TypeVar, Any, Sequencefrom sqlalchemy.ext.asyncio import AsyncSessionfrom sqlalchemy import select, insertfrom .base import BaseEntityEntityType = TypeVar("EntityType", bound=BaseEntity)class BaseRepository(Generic[EntityType]): def __init__( .. 2024. 5. 5.
Python structlog Standard Library LoggingIdeally, structlog should be able to be used as a drop-in replacement for standard library’s logging by wrapping it. In other words, you should be able to replace your call to logging.getLogger() b...www.structlog.org이전 글에서 작성했던 파이썬 내장 라이브러리 logging으로 구현했던 것을 structlog로 구현할 수 있다. Python logging파이썬에서도 날짜 별로, 특정 주기마다 로그 파일을 자동으로 생성할 수 있다.(trace Id도 설정하면 좋으련만...)구글링을 하면서 정리한.. 2024. 5. 3.
Middleware 커스텀해서 사용하기 from fastapi import FastAPIfrom middlewares import CustomMiddlewareapp = FastAPI()app.add_middleware(CustomMiddleware) # 미들웨어 추가middlewares.pyfrom starlette.types import ASGIApp, Receive, Scope, Sendclass CustomMiddleware: def __init__(self, app: ASGIApp) -> None: self.app = app async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: # do something await.. 2024. 5. 3.