-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
99 lines (79 loc) · 2.48 KB
/
app.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import os
import os.path
from flask import Flask, jsonify, g, request
from flask_cors import CORS
from flask_login import LoginManager
from api import api_bp
from auth import AuthError
from models.session_model import UserSession
def load_from_dev(app):
if os.path.isfile('secrets/dev.cfg'):
app.config.from_pyfile('secrets/dev.cfg')
else:
load_from_env(app)
def load_from_prod(app):
if os.path.isfile('secrets/prod.cfg'):
app.config.from_pyfile('secrets/prod.cfg')
else:
load_from_env(app)
def load_from_env(app):
if os.environ.get('DATABASE_URL'):
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
def load_var(name):
if os.environ.get(name):
app.config[name] = os.environ[name]
load_var('AUTH0_API_AUDIENCE')
load_var('TRELLO_API_KEY')
load_var('TRELLO_API_TOKEN')
load_var('SESSION_SECRET_KEY')
load_var('CONTACT_DELETE_TOKEN')
def load_config(app, env):
if env is None:
env = os.environ.get('DEPLOY_ENV')
if env == 'local':
load_from_env(app)
elif env == 'dev':
load_from_dev(app)
elif env == 'prod':
load_from_prod(app)
else:
if env is None:
env = 'default'
load_from_env(app)
if env == 'test':
app.config['LOGIN_DISABLED'] = True
app.config['DEPLOY_ENV'] = env
def create_app(env=None):
app = Flask(__name__)
# Initialize app config
app.config.from_object('defaultcfg')
load_config(app, env)
# CORS setup
CORS(app, supports_credentials=True)
# Sessions related initialization
app.secret_key = app.config['SESSION_SECRET_KEY']
login_manager = LoginManager()
login_manager.init_app(app)
# Initialization for flask-restful
app.register_blueprint(api_bp, url_prefix='/api')
@app.route('/')
def home_page():
return 'Hi'
@app.errorhandler(AuthError)
def handle_auth_error(ex):
response = jsonify(ex.error)
response.status_code = ex.status_code
return response
@login_manager.user_loader
def load_user(user_id):
return UserSession.query.get(user_id)
@login_manager.request_loader
def load_test_user(user_id):
if (app.config.get('TESTING')
and request.headers.get('X-Test-Authz')):
assert app.config.get('DEPLOY_ENV') == 'test'
return g.test_user
return None
from models.base_model import db
db.init_app(app)
return app