본문 바로가기

FastAPI4

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.
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.
Python logging 파이썬에서도 날짜 별로, 특정 주기마다 로그 파일을 자동으로 생성할 수 있다.(trace Id도 설정하면 좋으련만...)구글링을 하면서 정리한 내용을 아래에 작성해보겠다..json, .yaml, .conf, .py 파일에서 로그 파일 설정을 할 수 있다.여기선 .conf를 사용하겠다.app/config/logger.conf[loggers]keys = root[handlers]keys = stream_handler, file_handler[formatters]keys = stream_formatter, file_formatter[logger_root]level = NOTSEThandlers = stream_handler, file_handlerqualname = propagate = 0# handler[h.. 2024. 2. 19.