2 from flask import Flask
3 from flask import Response
4 from flask.globals import request
5 from flask.helpers import url_for
9 from tic import command
10 from tic.utils import error
11 from flask_cors import CORS
19 <title>TIC-Core web server</title>
20 <h1> TIC-Core web server is working<h1>
23 @app.route('/analysis', methods=['POST'])
26 logger = logging.getLogger(__name__)
27 logger.info('%s - %s %s : data=%s' % (request.remote_addr, request.method, request.path, request.data))
28 repo_info = json.loads(request.data)
29 view_data = command.analyze(repo_info.get('repos'), repo_info.get('recipes'))
30 resp = makeresponse(view_data, None)
31 except error.TICError as err:
33 resp = makeresponse(str(err), err)
34 except ValueError as ve:
36 resp = makeresponse(str(ve), ve)
37 except Exception as ex:
39 resp = makeresponse(str(ex), ex)
43 @app.route('/exports', methods=['POST'])
46 logger = logging.getLogger(__name__)
47 logger.info('%s - %s %s : data=%s' % (request.remote_addr, request.method, request.path, request.data))
49 except error.TICError as err:
51 resp = makeresponse(str(err), err)
52 except ValueError as ve:
54 resp = makeresponse(str(ve), ve)
55 except Exception as ex:
57 resp = makeresponse(str(ex), ex)
62 def start(port_num=59001):
63 # cryptographic random generator
64 app.secret_key = os.urandom(24)
66 with app.test_request_context():
67 print(url_for('index'))
68 print(url_for('analysis'))
69 app.run(host='0.0.0.0', port=port_num)
72 def makeresponse(data, err):
75 if isinstance(err, error.TICError) or isinstance(err, ValueError):
76 status = 400 # Bad Request
77 elif isinstance(err, Exception):
78 status = 500 # Internal Server Error
79 res_body = json.dumps(ResultInfo('false', None, data)._asdict())
81 res_body = json.dumps(ResultInfo('true', data, None)._asdict())
83 resp = Response(status=status,
84 mimetype='application/json',
89 ResultInfoType = collections.namedtuple('ResultInfo', 'result, data, message')
90 def ResultInfo(result='false', data=None, message=None):
91 return ResultInfoType(result, data, message)
93 if __name__ == '__main__':