I'm new to fastapi, which is really great so far, but I struggle to find a clean way to import my app config to in another module.
EDIT: I need to be able to change the config when running unit test
Here is my dir tree:
/app
| __init__.py
| /router
| | __init__.py
| | my_router.py
| /test
| | test_api.py
| config.py
| main.py
Here is my main.py file:
from functools import lru_cache
from fastapi import FastAPI
from .router import my_router
from . import config
app = FastAPI()
app.include_router(
my_router.router,
prefix="/r",
tags=["my-router"],
)
@lru_cache()
def get_setting():
return config.Settings(admin_email="admin@domain.com")
@app.get('/')
def hello():
return 'Hello world'
Here is the router.py:
from fastapi import APIRouter
from ..main import get_setting
router = APIRouter()
@router.get('/test')
def get_param_list(user_id: int):
config = get_setting()
return 'Import Ok'
And here is the config file
from pydantic import BaseSettings
class Settings(BaseSettings):
param_folder: str = "param"
result_folder: str = "output"
class Config:
env_prefix = "APP_"
Then runing uvicorn app.main:app --reload I got : ERROR: Error loading ASGI app. Could not import module "app.main".
I guess because of a kind of circular import. But then I don't how to pass my config to my router ?
Thanks for your help :)