I have a FastAPI app with pydantic settings instance reading and validation env variables:
### config.py
from pydantic import BaseSettings
class Settings(BaseSettings):
VERSION: str
OPENAPI_PREFIX: str
settings = Settings()
### main.py
from fastapi import FastApi
from .config import settings
app = FastAPI(
title='projectX',
description="my description",
version=settings.VERSION,
root_path=settings.OPENAPI_PREFIX,
openapi_url="/openapi.json",
prefix="/api"
)
if I want to use dependency injection, (for testing purpose to override the dependency settings) as showed in this section of the docs , how do I go for:
settings.VERSION
within FastAPI constructor?any other references to
settings
outside the endpoints views functions ? Should I put them as dependencies for each endpoint (very repetitive code I believe) ? or go for a middleware ? see here a very similar situation.