From d07270ec9de966c253b493083235a9ad4d8af632 Mon Sep 17 00:00:00 2001 From: mb Date: Sun, 30 Oct 2022 21:27:36 +0100 Subject: [PATCH] adding dotenv to requirements, switching to using .env file for the settings --- Makefile | 4 ++-- README.md | 17 +++++++---------- octomode.py | 15 ++------------- requirements.txt | 1 + settings.py | 17 +++++++++++++++++ 5 files changed, 29 insertions(+), 25 deletions(-) create mode 100644 settings.py diff --git a/Makefile b/Makefile index 8af7037..f23781a 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,4 @@ -SHELL := /bin/bash +#! make default: run @@ -6,4 +6,4 @@ setup: @if [ ! -d ".venv" ]; then python3 -m venv .venv && .venv/bin/pip install -r requirements.txt; fi run: - @/bin/bash -c "set -a && source config.env && set +a && .venv/bin/python octomode.py" + @.venv/bin/python octomode.py diff --git a/README.md b/README.md index c84b367..c1054df 100644 --- a/README.md +++ b/README.md @@ -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) -Then you can configure your environment. +`nano .env` -You can do this in two ways: - -* by editing the `config.env` file -* by storing the configuration settings as *environment variables* before running octomode: +Configure your environment, save the following configuration settings as to a file called `.env`: ``` -$ export OCTOMODE_PORTNUMBER=XXX -$ export OCTOMODE_PAD_URL=XXX -$ export OCTOMODE_PAD_API_URL=XXX -$ export OCTOMODE_PAD_API_KEY=XXX +OCTOMODE_PORTNUMBER=XXX +OCTOMODE_PAD_URL=XXX +OCTOMODE_PAD_API_URL=XXX +OCTOMODE_PAD_API_KEY=XXX ``` - **OCTOMODE_PORTNUMBER**: *optional*, default: `5001` @@ -106,7 +103,7 @@ $ export OCTOMODE_PAD_API_KEY=XXX `make run` (runs the Flask application) -Open the application at port `5001`, for example: or . +Open the application at port `5001`, for example: or . ### Dependencies diff --git a/octomode.py b/octomode.py index 120a067..eeb129b 100755 --- a/octomode.py +++ b/octomode.py @@ -14,19 +14,8 @@ import pypandoc # To read the Markdown metadat 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.config.from_object(Config) - -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) +APP.config.from_pyfile('settings.py') # --- @@ -189,4 +178,4 @@ def pagedjs(name): if __name__ == '__main__': 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) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 68b9db1..82264c5 100644 --- a/requirements.txt +++ b/requirements.txt @@ -14,3 +14,4 @@ pypandoc==1.7.2 typing-extensions==4.0.1 urllib3==1.26.8 zipp==3.7.0 +python-dotenv==0.21.0 \ No newline at end of file diff --git a/settings.py b/settings.py new file mode 100644 index 0000000..9ba0f6c --- /dev/null +++ b/settings.py @@ -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) \ No newline at end of file