-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
133 lines (108 loc) · 3.97 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
import http
import traceback
from datetime import datetime
from flask import Flask, request, jsonify
from flask_cors import CORS
from flask_restx import Api, Resource, fields
from werkzeug.datastructures import FileStorage
from werkzeug.utils import escape
from api import cached_response
from api.auth import token_auth
from config import config
from file_io.feedback_writer import save_feedback
from file_io.file_upload import handle_file_upload
from util.logger import log
app = Flask(__name__)
CORS(
app,
resources={
"/auth/": {"origins": ["http://localhost:8080", "https://pandermatt.ch", "https://kenspace.ch"]},
"/queries/*": {"origins": ["http://localhost:8080", "https://pandermatt.ch", "https://kenspace.ch"]},
"/feedback/*": {"origins": ["http://localhost:8080", "https://pandermatt.ch", "https://kenspace.ch"]},
"/upload/*": {"origins": ["http://localhost:8080", "https://pandermatt.ch", "https://kenspace.ch"]},
}
)
authorizations = {
'Bearer Auth': {
'type': 'apiKey',
'in': 'header',
'name': 'Authorization'
},
}
swagger_ui_enabled = '/'
if config.get_env('PRODUCTION') == 'Y':
swagger_ui_enabled = False
api = Api(app, version='0.1.0', title='KenSpace API',
description='API for KenSpace',
security='Bearer Auth',
authorizations=authorizations,
doc=swagger_ui_enabled
)
queries = api.namespace('queries', description='Query operations')
auth = api.namespace('auth', description='Authentication')
feedback = api.namespace('feedback', description='Submit Feedback')
upload = api.namespace('upload', description='Upload Data')
uuid = api.model('UUID', {
'uuid': fields.String(readOnly=True, description='unique identifier'),
})
@queries.route('/')
class QueryList(Resource):
@token_auth.login_required
def get(self):
"""Get all Queries Result"""
return cached_response.generate_queries(
escape(request.args.get('uuid')),
request.args.get('deletedWords'),
request.headers.get('Authorization'),
request.args.get('settings')
)
@auth.route('/')
class AuthHandler(Resource):
@token_auth.login_required
def get(self):
"""Check Authentication"""
return 'successful'
@feedback.route('/')
class FeedbackHandler(Resource):
@token_auth.login_required
def post(self):
"""Submit feedback to improve your system"""
save_feedback(
request.headers.get('Authorization'),
request.args.get('uuid'),
request.args.get('isHelpful'),
request.args.get('movieTitle'),
request.args.get('search'),
request.args.get('facet'),
request.args.get('delete'),
request.args.get('similarClusterActive'),
request.args.get('resultCount'),
datetime.now()
)
return '', http.HTTPStatus.NO_CONTENT
upload_parser = api.parser()
upload_parser.add_argument('file', location='files',
type=FileStorage, required=True)
@upload.route('/')
@upload.expect(upload_parser)
class Upload(Resource):
@token_auth.login_required
def post(self):
"""Upload your data to analyse"""
response = handle_file_upload(request.files['file'], request.form.get('uploadType'))
return response, 201
@app.after_request
def after_request(response):
log.info('%s %s %s %s %s', request.remote_addr, request.method, request.scheme, request.full_path, response.status)
return response
@app.errorhandler(Exception)
def exceptions(e):
tb = traceback.format_exc()
log.error('%s %s %s %s 5xx INTERNAL SERVER ERROR\n%s', request.remote_addr, request.method, request.scheme,
request.full_path, tb)
return e.status_code
@app.errorhandler(404)
def resource_not_found(e):
return jsonify(error=str(e)), 404
if __name__ == '__main__':
app.run(threaded=False, processes=config.get_env("PROCESSES_NUMBER"), debug=False)