33Copyright (c) 2019 - present AppSeed.us
44"""
55
6- import os
6+ import os , random , string
77
88class Config (object ):
99
1010 basedir = os .path .abspath (os .path .dirname (__file__ ))
1111
12- # Set up the App SECRET_KEY
13- # SECRET_KEY = config('SECRET_KEY' , default='S#perS3crEt_007')
14- SECRET_KEY = os .getenv ('SECRET_KEY' , 'S#perS3crEt_007' )
15-
16- # This will create a file in <app> FOLDER
17- SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os .path .join (basedir , 'db.sqlite3' )
18- SQLALCHEMY_TRACK_MODIFICATIONS = False
19-
2012 # Assets Management
21- ASSETS_ROOT = os .getenv ('ASSETS_ROOT' , '/static/assets' )
13+ ASSETS_ROOT = os .getenv ('ASSETS_ROOT' , '/static/assets' )
2214
15+ # Set up the App SECRET_KEY
16+ SECRET_KEY = os .getenv ('SECRET_KEY' , None )
17+ if not SECRET_KEY :
18+ SECRET_KEY = '' .join (random .choice ( string .ascii_lowercase ) for i in range ( 32 ))
19+
20+ # Social AUTH context
2321 SOCIAL_AUTH_GITHUB = False
2422
25- GITHUB_ID = os .getenv ('GITHUB_ID' )
26- GITHUB_SECRET = os .getenv ('GITHUB_SECRET' )
23+ GITHUB_ID = os .getenv ('GITHUB_ID' , None )
24+ GITHUB_SECRET = os .getenv ('GITHUB_SECRET' , None )
2725
2826 # Enable/Disable Github Social Login
2927 if GITHUB_ID and GITHUB_SECRET :
30- SOCIAL_AUTH_GITHUB = True
31-
28+ SOCIAL_AUTH_GITHUB = True
29+
30+ SQLALCHEMY_TRACK_MODIFICATIONS = False
31+
32+ DB_ENGINE = os .getenv ('DB_ENGINE' , None )
33+ DB_USERNAME = os .getenv ('DB_USERNAME' , None )
34+ DB_PASS = os .getenv ('DB_PASS' , None )
35+ DB_HOST = os .getenv ('DB_HOST' , None )
36+ DB_PORT = os .getenv ('DB_PORT' , None )
37+ DB_NAME = os .getenv ('DB_NAME' , None )
38+
39+ USE_SQLITE = True
40+
41+ # try to set up a Relational DBMS
42+ if DB_ENGINE and DB_NAME and DB_USERNAME :
43+
44+ try :
45+
46+ # Relational DBMS: PSQL, MySql
47+ SQLALCHEMY_DATABASE_URI = '{}://{}:{}@{}:{}/{}' .format (
48+ DB_ENGINE ,
49+ DB_USERNAME ,
50+ DB_PASS ,
51+ DB_HOST ,
52+ DB_PORT ,
53+ DB_NAME
54+ )
55+
56+ USE_SQLITE = False
57+
58+ except Exception as e :
59+
60+ print ('> Error: DBMS Exception: ' + str (e ) )
61+ print ('> Fallback to SQLite ' )
62+
63+ if USE_SQLITE :
64+
65+ # This will create a file in <app> FOLDER
66+ SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os .path .join (basedir , 'db.sqlite3' )
67+
3268class ProductionConfig (Config ):
3369 DEBUG = False
3470
@@ -37,21 +73,9 @@ class ProductionConfig(Config):
3773 REMEMBER_COOKIE_HTTPONLY = True
3874 REMEMBER_COOKIE_DURATION = 3600
3975
40- # PostgreSQL database
41- SQLALCHEMY_DATABASE_URI = '{}://{}:{}@{}:{}/{}' .format (
42- os .getenv ('DB_ENGINE' , 'mysql' ),
43- os .getenv ('DB_USERNAME' , 'appseed_db_usr' ),
44- os .getenv ('DB_PASS' , 'pass' ),
45- os .getenv ('DB_HOST' , 'localhost' ),
46- os .getenv ('DB_PORT' , 3306 ),
47- os .getenv ('DB_NAME' , 'appseed_db' )
48- )
49-
50-
5176class DebugConfig (Config ):
5277 DEBUG = True
5378
54-
5579# Load all possible configurations
5680config_dict = {
5781 'Production' : ProductionConfig ,
0 commit comments