File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ __pycache__
2+ env
Original file line number Diff line number Diff line change 1+ # project/app/config.py
2+
3+
4+ import logging
5+ import os
6+ from functools import lru_cache
7+
8+ from pydantic import BaseSettings
9+
10+
11+ log = logging .getLogger (__name__ )
12+
13+
14+ class Settings (BaseSettings ):
15+ environment : str = os .getenv ("ENVIRONMENT" , "dev" )
16+ testing : bool = os .getenv ("TESTING" , 0 )
17+
18+
19+ @lru_cache ()
20+ def get_settings () -> BaseSettings :
21+ log .info ("Loading config settings from the environment..." )
22+ return Settings ()
Original file line number Diff line number Diff line change 1+ # project/app/main.py
2+
3+
4+ from fastapi import FastAPI , Depends
5+
6+ from app .config import get_settings , Settings
7+
8+
9+ app = FastAPI ()
10+
11+
12+ @app .get ("/ping" )
13+ async def pong (settings : Settings = Depends (get_settings )):
14+ return {
15+ "ping" : "pong!" ,
16+ "environment" : settings .environment ,
17+ "testing" : settings .testing
18+ }
Original file line number Diff line number Diff line change 1+ fastapi == 0.55.1
2+ uvicorn == 0.11.5
You can’t perform that action at this time.
0 commit comments