-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
244 lines (193 loc) · 7.06 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import json
import yaml
import glob
import subprocess
import re
import sys
from flask import Flask, render_template, request, make_response
from waitress import serve
import argparse
import mimeparse
import traceback
from rdflib import URIRef
from app import CypherBackend, SparqlProxy, ApiError
def jsonify(data, status=200, indent=3, sort_keys=False):
response = make_response(json.dumps(
data, indent=indent, sort_keys=sort_keys))
response.headers['Content-Type'] = 'application/json; charset=utf-8'
response.headers['mimetype'] = 'application/json'
response.headers['Access-Control-Allow-Origin'] = '*'
response.status_code = status
return response
app = Flask(__name__)
githash = None
def render(template, **vars):
title = template.split(".")[0]
# TODO: add title
return render_template(template, title=title, githash=githash, **vars)
@app.errorhandler(ApiError)
def handle_api_error(error):
response = jsonify(error.to_dict())
response.status_code = error.status
return response
@app.errorhandler(Exception)
def handle_exception(error):
if app.config["debug"]:
print(traceback.format_exc())
if hasattr(error, 'message'):
message = error.message
else:
message = str(error)
return handle_api_error(ApiError(message))
@app.route('/')
def index():
return render('index.html')
@app.route('/license')
def license():
return render('license.html')
@app.context_processor
def utility_processor():
return dict(URIRef=URIRef)
rdf_formats = {
'application/x-turtle': 'turtle',
'text/turtle': 'turtle',
'application/rdf+xml': 'xml',
'application/trix': 'trix',
'application/n-quads': 'nquads',
'application/n-triples': 'nt',
'text/n-triples': 'nt',
'text/rdf+nt': 'nt',
'application/n3': 'n3',
'text/n3': 'n3',
'text/rdf+n3': 'n3'
}
@app.route('/terminology')
@app.route('/terminology/')
def terminology():
# TODO: server RDF as well
return render('terminologies.html')
@app.route('/repository')
@app.route('/repository/')
def repository():
return render('repositories.html')
@app.route('/collection', defaults={'id': None})
@app.route('/collection/', defaults={'id': None})
@app.route('/collection/<int:id>')
def collection(id):
if id:
format = request.args.get("format")
uri = "https://graph.nfdi4objects.net/collection/" + str(id)
graph = app.config["sparql-proxy"].request(
"DESCRIBE <" + uri + ">",
{"named-graph-uri": "https://graph.nfdi4objects.net/collection/"})
if "html" in request.headers["Accept"] or format == "html":
if len(graph) > 0:
return render('collection.html', uri=uri, graph=graph)
else:
return render('collection.html', uri=uri, graph=None), 404
else:
mimetype = "text/plain"
if format in set(rdf_formats.values()):
mimetype = [
type for type in rdf_formats if rdf_formats[type] == format][0]
else:
accept = request.headers.get("Accept")
types = list(rdf_formats.keys())
mimetype = mimeparse.best_match(types, accept)
if mimetype in rdf_formats:
format = rdf_formats[mimetype]
else:
format = "turtle"
mimetype = "text/turtle"
print("Format, mimetype")
print(format, mimetype)
response = make_response("Not found", 404)
response.mimetype = "text/plain"
if len(graph) > 0:
# TODO: add known namespaces for pretty Turtle
response = make_response(graph.serialize(format=format), 200)
response.mimetype = mimetype
return response
else:
# TODO: server RDF as well
return render('collections.html')
# Detect write queries the simple way. This also block some valid read-queries.
def isAllowedCypherQuery(cmd: str) -> bool:
return re.search('merge|create|delete|set', cmd, re.IGNORECASE) is None
@app.route('/api/cypher', methods=('GET', 'POST'))
def cypher_api():
query = ''
if 'query' in request.args: # GET
query = request.args.get('query')
elif request.data: # POST
query = request.data.decode('UTF-8')
if query:
if isAllowedCypherQuery(query):
answer = app.config["cypher-backend"].execute(query)
else:
raise ApiError("Cypher query is not allowed!", 403)
else:
raise ApiError('missing or empty "query" parameter', 400)
return jsonify(answer)
@app.route('/api/sparql', methods=('GET', 'POST'))
def sparql_api():
return app.config["sparql-proxy"].proxyRequest(request)
@app.route('/cypher')
def cypher_form():
return render('cypher.html')
@app.route('/sparql')
def sparql_form():
return render('sparql.html', **config["sparql"])
def extend_examples(examples):
extended = []
for ex in examples:
if isinstance(ex, str):
for file in glob.glob(ex):
lines = open(file).read().split("\n")
name = re.sub(r"^#\s*", "", lines[0])
extended.append({"name": name, "query": "\n".join(lines)})
else:
extended.append(ex)
return extended
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--port', type=int,
default=8000, help="Server port")
parser.add_argument(
'-w', '--wsgi', action=argparse.BooleanOptionalAction, help="Use WSGI server")
parser.add_argument('-c', '--config', type=str,
default="config.yaml", help="Config file")
parser.add_argument('-d', '--debug', action=argparse.BooleanOptionalAction)
args = parser.parse_args()
opts = {"port": args.port}
if args.debug:
opts["debug"] = True
with open(args.config) as stream:
try:
config = yaml.safe_load(stream)
except yaml.YAMLError as err:
msg = "Error in %s" % (args.config)
if hasattr(err, 'problem_mark'):
mark = err.problem_mark
msg += " at line %s char %s" % (mark.line + 1, mark.column + 1)
print(msg, file=sys.stderr)
sys.exit(1)
config["sparql"]["examples"] = extend_examples(
config["sparql"]["examples"])
config["cypher"]["examples"] = extend_examples(
config["cypher"]["examples"])
for key in config.keys():
app.config[key] = config[key]
app.config["cypher-backend"] = CypherBackend(config['cypher'])
app.config["sparql-proxy"] = SparqlProxy(
config["sparql"]["endpoint"], args.debug)
app.config["debug"] = args.debug
try:
githash = subprocess.run(['git', 'rev-parse', '--short=8', 'HEAD'],
stdout=subprocess.PIPE).stdout.decode('utf-8').strip()
except Exception:
pass
if args.wsgi:
serve(app, host="0.0.0.0", **opts)
else:
app.run(host="0.0.0.0", **opts)