Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App config #56

Merged
merged 2 commits into from
Apr 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion console
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import os
import argparse

from utils import env
from utils import env, generate_app_key

parser = argparse.ArgumentParser(description='Run the k8s.elections app')

Expand All @@ -34,6 +34,10 @@ parser.add_argument('--host',
server is available externally. Defaults to 127.0.0.1 \
making the it only visable on localhost')

parser.add_argument('--generate',
help='Generate 32 byte app secret key for session cookie encryption',
action='store_true')

parser.add_argument('--migrate',
help='Create the database tables',
action='store_true')
Expand All @@ -53,6 +57,11 @@ if __name__ == "__main__":
# ###################################################################### #
args = parser.parse_args()

if args.generate:
print('# ----- Generating 32 byte app secret key ----- #')
generate_app_key()
exit()

if args.migrate:
from config import DATABASE_URL
from elekto.models.sql import migrate
Expand Down
7 changes: 6 additions & 1 deletion utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@
# Author(s): Manish Sahani <[email protected]>

import os
from dotenv import load_dotenv
from dotenv import load_dotenv, set_key

# Load the custom environment file into the program
targetenv = '.env.testing' if os.getenv('TESTING') else '.env'
load_dotenv(os.path.join(os.path.dirname(__file__), targetenv))


def generate_app_key():
key = os.urandom(32).hex()
set_key(targetenv, "APP_KEY", key)


def env(key, default=None):
v = os.getenv(key)
return default if v is None or v == '' else v