How to inject Fastenv to FastAPI methods #28
-
Hello,
Sorry for dumb question:
Thank you in advance |
Beta Was this translation helpful? Give feedback.
Answered by
br3ndonland
Mar 16, 2024
Replies: 1 comment 2 replies
-
Thanks for your interest @clabnet. It's a good question and I was thinking I should write some docs to more clearly explain this topic. I agree with your suggestion of using a lifespan event. I would just modify it slightly. To access your from contextlib import asynccontextmanager
import fastenv
from fastapi import FastAPI
@asynccontextmanager
async def lifespan(app: FastAPI):
"""Load .env file"""
settings = await fastenv.load_dotenv(".env", find_source=True)
app.settings = settings
yield
app = FastAPI(lifespan=lifespan) Let me know how it goes. |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
clabnet
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for your interest @clabnet. It's a good question and I was thinking I should write some docs to more clearly explain this topic.
I agree with your suggestion of using a lifespan event. I would just modify it slightly. To access your
DotEnv
model after the application starts up, maybe you could set it as an attribute on yourFastAPI
instance.Let me know how it goes.