-
Notifications
You must be signed in to change notification settings - Fork 5
/
server.py
executable file
·109 lines (79 loc) · 3.14 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
#!/usr/bin/env python
import sys
from flask import Flask, jsonify, request, abort
from flask import send_file
from flask_cors import CORS
from classes.documents import *
app = Flask(__name__)
app.config.from_pyfile('config.py')
app.config.from_mapping(os.environ)
"""Initializes logging."""
if LOG_COLOR:
logging.addLevelName(logging.DEBUG,
"\033[1;00m%s\033[1;0m" % logging.getLevelName(logging.DEBUG))
logging.addLevelName(logging.INFO,
"\033[1;34m%s\033[1;0m" % logging.getLevelName(logging.INFO))
logging.addLevelName(logging.WARNING,
"\033[0;33m%s\033[0;0m" % logging.getLevelName(logging.WARNING))
logging.addLevelName(logging.ERROR,
"\033[1;31m%s\033[1;0m" % logging.getLevelName(logging.ERROR))
logging.addLevelName(logging.CRITICAL,
"\033[0;31m%s\033[0;0m" % logging.getLevelName(logging.CRITICAL))
logging.basicConfig(level=LOG_LEVEL, format=LOG_FORMAT, style='{')
def _log_exception():
exc_type, exc_obj, exc_tb = sys.exc_info()
file_name = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
logging.exception("Exception type#{}#{}#{}".format(
exc_type, file_name, exc_tb.tb_lineno))
@app.route('/api/document', methods=['POST'])
def replace():
required = ['hash', 'type']
if len(request.args) != len(required):
abort(400, {'message': 'invalid arguments'})
for r in required:
if r not in request.args:
abort(400, {'message': '%s argument is not missing'})
for r in request.args:
if r not in required:
abort(400, {'message': '%s argument is not required'})
if request.args['type'] not in SUPPORTED_FILE_TYPES:
abort(400, {'message': 'Type should be one of following [ %s ]' % ', '.join(SUPPORTED_FILE_TYPES)})
hash = request.args['hash']
type = request.args['type']
logging.debug('Hash is %s' % hash)
logging.debug('Type is %s' % type)
dic = request.json or {}
pdf_converter = PdfFactory.factory(type, hash, dic)
return send_file(pdf_converter.generate(), as_attachment=True), 200
@app.errorhandler(403)
def forbidden(ex):
_log_exception()
return jsonify({"code": 403, "message": ex.description}), 403
@app.errorhandler(400)
def forbidden(ex):
_log_exception()
return jsonify({"code": 400, "message": ex.description}), 400
@app.errorhandler(404)
def not_found(ex):
_log_exception()
return jsonify({"code": 404, "message": ex.description}), 404
@app.errorhandler(405)
def not_found(ex):
_log_exception()
return jsonify({"code": 405, "message": ex.description}), 405
@app.errorhandler(500)
def internal_server_error(ex):
logging.error('Server Error: %s', ex.description)
return jsonify({"code": 500, "message": ex.description}), 500
@app.errorhandler(Exception)
def handle_error(e):
code = 500
message = str(e)
if isinstance(e, MainException):
code = e.get_code()
message = e.get_message()
_log_exception()
return jsonify({"code": code, "message": message}), code
if __name__ == '__main__':
CORS(app)
app.run(debug=True)