I have a FastAPI application that is going to production soon, however i am facing some problems with hydra integration.
First i could not run the @hydra.main()
decorator on fastAPI endpoints. This was overcome with using the Hydra Compose API. Here is my implementation for it:
def load_config(config_path, config_name):
"""Loads specified hydra config.
This method is used because, fastapi and hydra did not
wanna work together.
"""
core.global_hydra.GlobalHydra.instance().clear()
initialize(version_base=None, config_path=config_path)
cfg = compose(config_name=config_name)
return cfg
This was fine until now, while I only used one config, but now I need to separate test and production configurations.
My configuration dircetory structure looks like this:
├── config
│ ├── config.yaml
│ ├── env
│ │ ├── prod.yaml
│ │ └── test.yaml
│ ├── prod
│ │ └── prod1.yaml
│ └── test
│ └── test1.yaml
The directory structure and file contents are based on this post: Python Hydra configuration for different environments. The default environment is set to test.
My application runs with this command:
uvicorn my.api:app
and this successfully loads the test config.
However when I try to run it with:
uvicorn env=prod my.api:app
the test config still loads instead of production. Here i want to load the prod configuration.
How can I achieve this?
I have found this related github issue, but the solution was not clear from it: https://github.com/facebookresearch/hydra/issues/204
To be clear, i am willing to make fundamental changes if the problem lies within not using @hydra.main()