-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.py
176 lines (151 loc) · 5.74 KB
/
server.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
167
168
169
170
171
172
173
174
175
176
import os
import logging
import json
import requests
import datetime
from flask import Flask, redirect, send_file, Response, stream_with_context, request, abort
from flask_cors import CORS
from flask_session import Session
from flask_caching import Cache
os.environ['ALLOWED_SERVICES'] = 'etl-server:etl_server'
from etl_server.pipelines.blueprint import make_blueprint as pipelines_blueprint
from etl_server.users.blueprint import make_blueprint as users_blueprint
from etl_server.files.blueprint import make_blueprint as files_blueprint
from etl_server.taxonomies.blueprint import make_blueprint as taxonomies_blueprint
from etl_server.datarecords.blueprint import make_blueprint as datarecords_blueprint
from etl_server.permissions import check_permission, Permissions
from etl_server.db_utils import get_engine
from dgp_oauth2 import make_blueprint as auth_blueprint
from server_extra import extra_server_init
# Configuration
configuration = json.load(open('dags/configuration.json'))
default_locale = configuration.get('defaultLocale', 'en')
# Create application
app = Flask(__name__, static_folder='./ui/dist/ui/', static_url_path='/')
# CORS support
CORS(app, supports_credentials=True)
# Session
session = Session()
app.config.from_envvar('SERVER_NAME', silent=True)
app.config['SESSION_TYPE'] = 'filesystem'
app.config['SESSION_FILE_DIR'] = '/tmp/sessions'
app.config['SECRET_KEY'] = '-'
session.init_app(app)
config = {
"CACHE_TYPE": "FileSystemCache", # Flask-Caching related configs
"CACHE_DEFAULT_TIMEOUT": 600,
"CACHE_DIR": "/tmp/cache",
"CACHE_THRESHOLD": 1000,
"CACHE_OPTIONS": {
"mode": 0o700
},
}
cache = Cache(config=config)
cache.init_app(app)
# Encode datetimes properly
class CustomJSONEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, datetime.date):
return obj.isoformat()
return json.JSONEncoder.default(self, obj)
app.json_encoder = CustomJSONEncoder
# Routes
def proxy(base_url, prefix):
@check_permission([Permissions.workbench])
def func(*args, **kwargs):
if request.full_path.startswith(prefix):
url = base_url + request.full_path
print(request.method, url)
if request.method == 'GET':
req = requests.get(url, stream=True)
return Response(stream_with_context(req.iter_content(chunk_size=10)), content_type=req.headers['content-type'])
elif request.method == 'POST':
req = requests.post(url, json=request.json, headers=request.headers)
elif request.method == 'OPTIONS':
req = requests.options(url)
return Response(req.content, content_type=req.headers['content-type'])
abort(400)
return func
def dgp_proxy(app, route, methods=['GET']):
app.add_url_rule(route,
route[1:].replace('/', '_'),
proxy('http://localhost:5001', '/api'),
methods=methods)
dgp_proxy(app, '/api/events/<path:subpath>')
dgp_proxy(app, '/api/config', methods=['POST'])
@app.route('/api/config', methods=['OPTIONS'])
def options():
return {}
app.register_blueprint(
pipelines_blueprint(db_connection_string=os.environ.get('ETLS_DATABASE_URL'),
configuration=configuration),
url_prefix='/api/'
)
app.register_blueprint(
users_blueprint(db_connection_string=os.environ.get('ETLS_DATABASE_URL')),
url_prefix='/api/'
)
app.register_blueprint(
taxonomies_blueprint(db_connection_string=os.environ.get('ETLS_DATABASE_URL')),
url_prefix='/api/'
)
app.register_blueprint(
datarecords_blueprint(db_connection_string=os.environ.get('ETLS_DATABASE_URL')),
url_prefix='/api/'
)
app.register_blueprint(
files_blueprint(
bucket_name=os.environ.get('BUCKET_NAME'),
endpoint_url=os.environ.get('S3_ENDPOINT_URL'),
aws_access_key_id=os.environ.get('AWS_ACCESS_KEY_ID'),
aws_secret_access_key=os.environ.get('AWS_SECRET_ACCESS_KEY'),
aws_region=os.environ.get('AWS_REGION'),
),
url_prefix='/api/'
)
auth_connection_string = os.environ.get('AUTH_DATABASE_URL') or os.environ.get('DATABASE_URL')
app.register_blueprint(
auth_blueprint(os.environ.get('EXTERNAL_ADDRESS'), engine=get_engine(auth_connection_string), cache=cache),
url_prefix='/auth/'
)
if not configuration.get('features', {}).get('disableWebUi', False):
@app.route('/')
@app.route('/logout')
@app.route('/pipelines')
@app.route('/dashboard')
@app.route('/users')
@app.route('/files')
@app.route('/taxonomies')
@app.route('/datarecords/<path:subpath>')
@app.route('/dgp/<path:subpath>')
@app.route('/status/<path:subpath>')
@app.route('/edit/<path:subpath>')
@app.route('/<locale>/logout')
@app.route('/<locale>/pipelines')
@app.route('/<locale>/dashboard')
@app.route('/<locale>/users')
@app.route('/<locale>/files')
@app.route('/<locale>/taxonomies')
@app.route('/<locale>/datarecords/<path:subpath>')
@app.route('/<locale>/dgp/<path:subpath>')
@app.route('/<locale>/status/<path:subpath>')
@app.route('/<locale>/edit/<path:subpath>')
def main(subpath=None, locale=default_locale):
locale = locale[:2]
return send_file(f'ui/dist/ui/{locale}/index.html')
@app.route('/he')
@app.route('/he/')
def main_he():
return send_file(f'ui/dist/ui/he/index.html')
@app.route('/en')
@app.route('/en/')
def main_en():
return send_file(f'ui/dist/ui/en/index.html')
extra_server_init(app)
if __name__=='__main__':
app.run()
else:
gunicorn_error_logger = logging.getLogger('gunicorn.error')
app.logger.handlers.extend(gunicorn_error_logger.handlers)
app.logger.setLevel(logging.DEBUG)
app.logger.info('DGP-APP STARTING')