-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
166 lines (134 loc) · 4.63 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import os
from logging.config import dictConfig
import flask
from flask import Flask, redirect, Blueprint, url_for, render_template
from flask_cors import CORS
from flask_executor import Executor
from apis import api_blueprint
base_url = '/viruclust/'
api_url = base_url + 'api'
repo_static_url = base_url + 'repo_static'
def get_env_variable(name):
try:
return os.environ[name]
except KeyError:
message = "Expected environment variable '{}' not set.".format(name)
raise Exception(message)
# def get_db_uri():
# database_name = "vcm_du_1"
#
# try:
# # /home/metadata/virusurf_active_databases.txt
# with open("/home/metadata/virusurf_active_databases.txt") as f:
# lines = f.readlines()
# database_name = [x for x in lines if 'gisaid' not in x][0]
# except IOError:
# pass
#
# # postgres_url = get_env_variable("POSTGRES_URL")
# # postgres_user = get_env_variable("POSTGRES_USER")
# # postgres_pw = get_env_variable("POSTGRES_PW")
# # postgres_db = get_env_variable("POSTGRES_DB")
# postgres_url = "localhost"
# postgres_user = "geco"
# postgres_pw = "geco78"
# postgres_db = database_name.strip()
#
# application_name = []
#
# if 'ENV' in my_app.config:
# application_name.append(my_app.config['ENV'])
#
# application_name.append(os.uname()[1])
#
# application_name.append(base_url)
#
# application_name = ", ".join(application_name)
#
# result = 'postgresql+psycopg2://{user}:{pw}@{url}/{db}?application_name="{application_name}"'.format(
# user=postgres_user,
# pw=postgres_pw,
# url=postgres_url,
# db=postgres_db,
# application_name=application_name, )
# print(result)
# return result
dictConfig({
'version': 1,
'formatters': {'default': {
'format': '[%(asctime)s] %(levelname)s in %(module)s:%(lineno)d: %(message)s',
}},
'handlers': {'wsgi': {
'class': 'logging.StreamHandler',
'stream': 'ext://flask.logging.wsgi_errors_stream',
'formatter': 'default'
}},
'root': {
'level': 'DEBUG',
'handlers': ['wsgi']
}
})
my_app = Flask(__name__)
cors = CORS(my_app)
my_app.debug = False
# my_app.config['SQLALCHEMY_DATABASE_URI'] = get_db_uri()
# my_app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
# my_app.config['SQLALCHEMY_POOL_SIZE'] = 1
# my_app.config['SQLALCHEMY_MAX_OVERFLOW'] = 30
my_app.config['EXECUTOR_PROPAGATE_EXCEPTIONS'] = True
my_app.config['EXECUTOR_MAX_WORKERS'] = 20
# db.init_app(my_app)
executor_inner = Executor(my_app)
simple_page = Blueprint('root_pages', __name__,
static_folder='../frontend/dist/static',
template_folder='../frontend/dist')
graph_pages = Blueprint('static', __name__,
# static_url_path='/',
static_folder='./repo_static/',
# template_folder='../frontend/dist'
)
# base url defined in apis init
@simple_page.route('/')
def index():
flask.current_app.logger.info("serve index")
return render_template('index.html')
# base url defined in apis init
@simple_page.route('/epitope/')
def index_epitope():
flask.current_app.logger.info("serve index")
return render_template('index.html')
# Make a "catch all route" so all requests match our index.html file.
# This lets us use the new history APIs in the browser.
@simple_page.route('/', defaults={'path': ''})
@simple_page.route('/<path:path>')
def redirect_all(path):
if path.startswith("epi"):
return redirect(url_for('.index_epitope'))
else:
return redirect(url_for('.index'))
# register blueprints
my_app.register_blueprint(api_blueprint, url_prefix=api_url)
my_app.register_blueprint(graph_pages, url_prefix=repo_static_url)
my_app.register_blueprint(simple_page, url_prefix=base_url)
my_app.app_context().push()
# redirect all to base url
@my_app.route('/', defaults={'path': ''})
@my_app.route('/<path:path>')
def index_all(path):
return redirect(base_url)
# if __name__ == '__main__':
# my_app.run()
# prevent cached responses
@my_app.after_request
def add_header(r):
"""
Add headers to both force latest IE rendering engine or Chrome Frame,
and also to cache the rendered page for 10 minutes.
"""
if "Cache-Control" not in r.headers:
r.cache_control.max_age = 1 # 1 seconds # 5 min
# r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
# r.headers["Pragma"] = "no-cache"
# r.headers["Expires"] = "0"
# r.headers['Cache-Control'] = 'public, max-age=0'
return r