-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
57 lines (48 loc) · 2.1 KB
/
config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import os
import urllib.parse
from dotenv import load_dotenv
from uuid import uuid4
basedir = os.environ.get('BASEDIR',os.path.abspath(os.path.dirname(__file__)))
load_dotenv(dotenv_path=os.path.join(basedir,'todo.env'))
def generate_db_uri():
if os.environ.get('MYSQL_HOST'):
# Use MySQL database
host = os.environ.get('MYSQL_HOST')
user = os.environ.get('MYSQL_USER', 'todo')
passwd = os.environ.get('MYSQL_PASSWORD', 'password')
dbname = os.environ.get('MYSQL_DATABASE', 'todo')
passwd_enc = urllib.parse.quote_plus(passwd)
return f'mysql+pymysql://{user}:{passwd_enc}@{host}/{dbname}'
else:
# Use SQLite database
dbpath = os.environ.get('SQLITE_DATABASE_PATH',os.path.join(basedir, 'app.db'))
return 'sqlite:///' + dbpath
def get_secret_key():
# Try reading secret key from environment
key = os.environ.get('SECRET_KEY')
if not key:
# If a secret key is not defined in the environment, try reading one
# from a file, and otherwise, generate a key and store it in a file.
keyfile = os.path.join(basedir,'secret_key')
if os.path.isfile(keyfile):
with open(keyfile,'r') as f:
key = f.read().rstrip('\n')
else:
key = str(uuid4())
um = os.umask(0o077) # don't give other users read access
with open(keyfile,'w') as f:
f.write(key)
os.umask(um) # revert umask to original
return key
class Config(object):
SERVER_NAME = os.environ.get('SERVER_NAME')
SECRET_KEY = get_secret_key()
SQLALCHEMY_DATABASE_URI = generate_db_uri()
SQLALCHEMY_TRACK_MODIFICATIONS = False
MAIL_ENABLED = bool(os.environ.get('MAIL_SERVER'))
MAIL_SERVER = os.environ.get('MAIL_SERVER')
MAIL_PORT = int(os.environ.get('MAIL_PORT') or 25)
MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS','').lower() in ['1', 'true', 'yes', 'y']
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
MAIL_FROM = os.environ.get('MAIL_FROM')