forked from onnela-lab/beiwe-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
83 lines (65 loc) · 2.83 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
import os
import jinja2
from flask import Flask, render_template, redirect
from raven.contrib.flask import Sentry
from werkzeug.contrib.fixers import ProxyFix
from config import load_django
from api import (participant_administration, admin_api, copy_study_api, data_access_api,
data_pipeline_api, mobile_api, survey_api)
from config.settings import SENTRY_ELASTIC_BEANSTALK_DSN, SENTRY_JAVASCRIPT_DSN
from libs.admin_authentication import is_logged_in
from libs.security import set_secret_key
from pages import (admin_pages, mobile_pages, survey_designer, system_admin_pages,
data_access_web_form)
# # if running locally we want to use a sqlite database
# if __name__ == '__main__':
# os.environ['DJANGO_DB_ENV'] = "local"
#
# # if running through WSGI we want
# if not __name__ == '__main__':
# os.environ['DJANGO_DB_ENV'] = "remote"
# Load and set up Django
def subdomain(directory):
app = Flask(__name__, static_folder=directory + "/static")
set_secret_key(app)
loader = [app.jinja_loader, jinja2.FileSystemLoader(directory + "/templates")]
app.jinja_loader = jinja2.ChoiceLoader(loader)
app.wsgi_app = ProxyFix(app.wsgi_app)
return app
# Register pages here
app = subdomain("frontend")
app.register_blueprint(mobile_api.mobile_api)
app.register_blueprint(admin_pages.admin_pages)
app.register_blueprint(mobile_pages.mobile_pages)
app.register_blueprint(system_admin_pages.system_admin_pages)
app.register_blueprint(survey_designer.survey_designer)
app.register_blueprint(admin_api.admin_api)
app.register_blueprint(participant_administration.participant_administration)
app.register_blueprint(survey_api.survey_api)
app.register_blueprint(data_access_api.data_access_api)
app.register_blueprint(data_access_web_form.data_access_web_form)
app.register_blueprint(copy_study_api.copy_study_api)
app.register_blueprint(data_pipeline_api.data_pipeline_api)
# Don't set up Sentry for local development
if os.environ['DJANGO_DB_ENV'] != 'local':
sentry = Sentry(app, dsn=SENTRY_ELASTIC_BEANSTALK_DSN)
@app.route("/<page>.html")
def strip_dot_html(page):
# Strips away the dot html from pages
return redirect("/%s" % page)
@app.context_processor
def inject_dict_for_all_templates():
return {"SENTRY_JAVASCRIPT_DSN": SENTRY_JAVASCRIPT_DSN}
# Extra Production settings
if not __name__ == '__main__':
# Points our custom 404 page (in /frontend/templates) to display on a 404 error
@app.errorhandler(404)
def e404(e):
return render_template("404.html", is_logged_in=is_logged_in()), 404
# Extra Debugging settings
if __name__ == '__main__':
# might be necessary if running on windows/linux subsystem on windows.
# from gevent.wsgi import WSGIServer
# http_server = WSGIServer(('', 8080), app)
# http_server.serve_forever()
app.run(host='0.0.0.0', port=int(os.getenv("PORT", "8080")), debug=True)