Aller au contenu

Variables d'environnement

Configurer les variables d’environneemnt

Créer un fichier .env

.env
ENV=dev
FRONTEND_URL="http://localhost:3000"

Créer un script config.py

config.py
from functools import lru_cache
from pydantic_settings import BaseSettings, SettingsConfigDict
class Settings(BaseSettings):
"""Main settings"""
app_name: str = "demofirebase"
env: str
frontend_url: str
model_config = SettingsConfigDict(env_file=".env")
@lru_cache
def get_settings() -> Settings:
"""Retrieves the fastapi settings"""
return Settings()

Accéder aux variables d’environnment

main.py
from fastapi import FastAPI
from router import router
from config import get_settings
settings = get_settings()
print(settings.frontend_url)
app = FastAPI()
app.include_router(router)