adding dotenv to requirements, switching to using .env file for the settings

This commit is contained in:
mb 2022-10-30 21:27:36 +01:00
parent 8068ed312b
commit d07270ec9d
5 changed files with 29 additions and 25 deletions

View File

@ -1,4 +1,4 @@
SHELL := /bin/bash #! make
default: run default: run
@ -6,4 +6,4 @@ setup:
@if [ ! -d ".venv" ]; then python3 -m venv .venv && .venv/bin/pip install -r requirements.txt; fi @if [ ! -d ".venv" ]; then python3 -m venv .venv && .venv/bin/pip install -r requirements.txt; fi
run: run:
@/bin/bash -c "set -a && source config.env && set +a && .venv/bin/python octomode.py" @.venv/bin/python octomode.py

View File

@ -83,18 +83,15 @@ You can clone this repository to run octomode on your own computer or server.
`make setup` (sets up a virtual environment and install the requirements, you only need to do this once) `make setup` (sets up a virtual environment and install the requirements, you only need to do this once)
Then you can configure your environment. `nano .env`
You can do this in two ways: Configure your environment, save the following configuration settings as to a file called `.env`:
* by editing the `config.env` file
* by storing the configuration settings as *environment variables* before running octomode:
``` ```
$ export OCTOMODE_PORTNUMBER=XXX OCTOMODE_PORTNUMBER=XXX
$ export OCTOMODE_PAD_URL=XXX OCTOMODE_PAD_URL=XXX
$ export OCTOMODE_PAD_API_URL=XXX OCTOMODE_PAD_API_URL=XXX
$ export OCTOMODE_PAD_API_KEY=XXX OCTOMODE_PAD_API_KEY=XXX
``` ```
- **OCTOMODE_PORTNUMBER**: *optional*, default: `5001` - **OCTOMODE_PORTNUMBER**: *optional*, default: `5001`
@ -106,7 +103,7 @@ $ export OCTOMODE_PAD_API_KEY=XXX
`make run` (runs the Flask application) `make run` (runs the Flask application)
Open the application at port `5001`, for example: <http://localhost:5001> or <http://111.111.111.111:5001>. Open the application at port `5001`, for example: <http://localhost:5001> or <http://mydomainname.ext:5001>.
### Dependencies ### Dependencies

View File

@ -14,19 +14,8 @@ import pypandoc
# To read the Markdown metadat # To read the Markdown metadat
import markdown import markdown
class Config(object):
PORTNUMBER = int(os.environ.get('OCTOMODE_PORTNUMBER', 5001))
PAD_URL = os.environ.get('OCTOMODE_PAD_URL', 'https://pad.vvvvvvaria.org')
PAD_API_URL = os.environ.get('OCTOMODE_PAD_API_URL', 'https://pad.vvvvvvaria.org/api/1.2.15')
PAD_API_KEY = os.environ.get('OCTOMODE_PAD_API_KEY', '')
APP = Flask(__name__) APP = Flask(__name__)
APP.config.from_object(Config) APP.config.from_pyfile('settings.py')
if APP.config.get('OCTOMODE_PAD_API_KEY', '') == '':
print("error: you must provide a value for OCTOMODE_PAD_API_KEY")
print("error: e.g. export OCTOMODE_PAD_API_KEY=...")
exit(1)
# --- # ---
@ -189,4 +178,4 @@ def pagedjs(name):
if __name__ == '__main__': if __name__ == '__main__':
APP.debug=True APP.debug=True
APP.run(host="0.0.0.0", port=APP.config["PORTNUMBER"], threaded=True) APP.run(host="0.0.0.0", port=APP.config["PORTNUMBER"], threaded=True)

View File

@ -14,3 +14,4 @@ pypandoc==1.7.2
typing-extensions==4.0.1 typing-extensions==4.0.1
urllib3==1.26.8 urllib3==1.26.8
zipp==3.7.0 zipp==3.7.0
python-dotenv==0.21.0

17
settings.py Normal file
View File

@ -0,0 +1,17 @@
import os
from dotenv import load_dotenv
# Load environment variables from the .env file
load_dotenv()
# Bind them to Python variables
PORTNUMBER = int(os.environ.get('OCTOMODE_PORTNUMBER', 5001))
PAD_URL = os.environ.get('OCTOMODE_PAD_URL', 'https://pad.vvvvvvaria.org')
PAD_API_URL = os.environ.get('OCTOMODE_PAD_API_URL', 'https://pad.vvvvvvaria.org/api/1.2.15')
PAD_API_KEY = os.environ.get('OCTOMODE_PAD_API_KEY', '')
# Check if API key is provided
if not PAD_API_KEY or PAD_API_KEY == "XXX":
print("error: you must provide a value for OCTOMODE_PAD_API_KEY")
print("error: e.g. export OCTOMODE_PAD_API_KEY=...")
exit(1)