-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapplication.py
114 lines (94 loc) · 3.66 KB
/
application.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env python
import sys
if sys.version_info < (3, 0):
sys.stderr.write("Sorry, requires Python 3.4 or later\n")
sys.exit(1)
import os
import logging
from flask import Flask
from gludb.config import Database, default_database
from config import env_populate
from gluten.utils import project_file, first, app_logger
from gluten.models import User, Taxonomy, Transcript
from gluten.auth import auth
from gluten.main_app import main
from gluten.admin import admin
# Note that application as the main WSGI app is required for Python apps
# on Elastic Beanstalk. Also note that we provide the default config, but
# someone must supply an actual config file pointed to by the env variable
# GLUTEN_CONFIG_FILE. See ./local.sh for an example of how to handle this
application = Flask(__name__)
application.config.from_object('config.DefaultConfig')
application.config.from_envvar('GLUTEN_CONFIG_FILE')
application.secret_key = application.config.get('FLASK_SECRET')
# Set any environment var's requested by the config file
for name in env_populate:
os.environ[name] = application.config.get(name)
# Final app settings depending on whether or not we are set for debug mode
# Note that once we get everything working, we'll have be able to use the
# logging help in gluten.utils (like app_logger)
if application.config.get('DEBUG', None):
# Debug mode - running on a workstation
application.debug = True
logging.basicConfig(level=logging.DEBUG)
else:
# We are running on AWS Elastic Beanstalk (or something like it)
application.debug = False
# See .ebextensions/01logging.config
logging.basicConfig(
filename='/opt/python/log/gluten.log',
level=logging.INFO
)
app_logger().info('Application debug is %s', application.debug)
# Register our blueprints
application.register_blueprint(auth)
application.register_blueprint(main)
application.register_blueprint(admin)
# This will be called before the first request is ever serviced
@application.before_first_request
def before_first():
app_logger().info('Handling database init')
if application.debug:
# Debug/local dev
default_database(Database('sqlite', filename=project_file('.test.db')))
else:
# Production!
default_database(Database('dynamodb'))
# Make sure we have our tables
User.ensure_table()
Transcript.ensure_table()
Taxonomy.ensure_table()
# Some debug data we might find useful
if application.debug:
TEST_EMAIL = application.config.get('TEST_EMAIL')
me = first(User.find_by_index('idx_email', TEST_EMAIL))
if not me:
me = User(name='Test User', email=TEST_EMAIL)
me.save()
if not Transcript.find_all():
ts1 = Transcript.from_xml_file(
project_file('test/sample/SampleTranscript.xml')
)
ts1.script_identifier = 'Original Owned'
ts1.owner = me.id
ts1.tagger = ''
ts1.id = ''
ts1.save()
ts2 = Transcript.from_xml_file(
project_file('test/sample/SampleTranscript.xml')
)
ts2.script_identifier = 'New Assigned'
ts2.owner = me.id
ts2.tagger = me.id
ts2.source_transcript = ts1.id
ts2.id = '' # Ensure new id on save
ts2.save()
# Our entry point - called when our application is started "locally".
# This WILL NOT be run by Elastic Beanstalk
def main():
# Listen on all addresses if running under Vagrant, else listen
# on localhost
host = '0.0.0.0' if os.environ['USER'] == 'vagrant' else '127.0.0.1'
application.run(host=host)
if __name__ == '__main__':
main()