简体   繁体   中英

Way to pass arguments to FastAPI app via command line

I'm using python 3.8.0 for my FastAPI app. It uses .env file located on the root of a project directory. I am using dotenv package and location of the .env file is hardcoded within the app. This is my unit file

[Unit]
Description=Gunicorn instance for my_app
After=network.target

[Service]
User=nginx
Group=nginx
WorkingDirectory=/usr/share/nginx/html/my_app/
Environment="PATH=/usr/share/nginx/html/my_app/venv/bin"
ExecStart=/usr/share/nginx/html/my_app/venv/bin/gunicorn --bind unix:/usr/share/nginx/html/my_app/my_app.sock -w 4 -k uvicorn.workers.UvicornWorker app.main:app

[Install]
WantedBy=multi-user.target

Challenge is I want to run 2 versions (production and test) of the same app using 2 different .env on 2 different ports. I'll have to create second unit file. But how can I pass the name of 2 different env file name to the app for further usage. These files contain database connections and etc. I imagine it roughly like this 1st unit file

ExecStart=/usr/share/nginx/html/my_app/venv/bin/gunicorn 
--bind unix:/usr/share/nginx/html/my_app/my_app.sock -w 4 -k uvicorn.workers.UvicornWorker app.main:app --env_file_name=".env.prod"

2nd unit file

ExecStart=/usr/share/nginx/html/my_app/venv/bin/gunicorn 
--bind unix:/usr/share/nginx/html/my_app/my_app.sock -w 4 -k uvicorn.workers.UvicornWorker app.main:app --env_file_name=".env.dev"

You can set the path from which systemd will read the environment for your process exactly in unit file configuration. The setting is called EnvironmnetFile= . Just set the option to the path for .env.prod in one unit file and for the path to .env.test for another.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM