|
7 | 7 | __version__ = '0.1' |
8 | 8 | __author__ = 'Alice Ferrazzi' |
9 | 9 | __license__ = 'GNU GPLv2+' |
| 10 | + |
| 11 | +from flask import Flask |
| 12 | +from flask_restful import Api |
| 13 | +import multiprocessing |
| 14 | +import argparse |
| 15 | +from .resources import AgentInfo, dispatcher |
| 16 | + |
| 17 | + |
| 18 | +def parse_args(): |
| 19 | + parser = argparse.ArgumentParser() |
| 20 | + |
| 21 | + parser.add_argument('-j', '--jobs', type=int, |
| 22 | + default=multiprocessing.cpu_count(), |
| 23 | + help='Specify the number of make jobs') |
| 24 | + |
| 25 | + parser.add_argument('-H', '--host', type=str, |
| 26 | + default='0.0.0.0', |
| 27 | + help='Specify the host') |
| 28 | + |
| 29 | + parser.add_argument('-P', '--port', type=int, |
| 30 | + default='5000', |
| 31 | + help='Specify the port') |
| 32 | + |
| 33 | + parser.add_argument('-T', '--threaded', action='store_true', |
| 34 | + help='Enable threading') |
| 35 | + |
| 36 | + parser.add_argument('-d', '--debug', action='store_true', |
| 37 | + help='Enable debugging') |
| 38 | + |
| 39 | + return parser.parse_args() |
| 40 | + |
| 41 | + |
| 42 | +def create_app(cmdline_args): |
| 43 | + """ |
| 44 | + Create server application |
| 45 | + RESTful api version 1.0 |
| 46 | + """ |
| 47 | + |
| 48 | + app = Flask(__name__, static_url_path="") |
| 49 | + api = Api(app) |
| 50 | + |
| 51 | + api.add_resource(AgentInfo.AgentAPI, '/elivepatch/api/', |
| 52 | + endpoint='root') |
| 53 | + |
| 54 | + # get agento information |
| 55 | + api.add_resource(AgentInfo.AgentAPI, '/elivepatch/api/v1.0/agent', |
| 56 | + endpoint='agent') |
| 57 | + |
| 58 | + # where to retrieve the live patch when ready |
| 59 | + api.add_resource(dispatcher.SendLivePatch, |
| 60 | + '/elivepatch/api/v1.0/send_livepatch', |
| 61 | + endpoint='send_livepatch') |
| 62 | + |
| 63 | + # where to receive the config file |
| 64 | + api.add_resource(dispatcher.GetFiles, '/elivepatch/api/v1.0/get_files', |
| 65 | + endpoint='config', |
| 66 | + resource_class_kwargs={'cmdline_args': cmdline_args}) |
| 67 | + return app |
| 68 | + |
| 69 | + |
| 70 | +def run(): |
| 71 | + cmdline_args = parse_args() |
| 72 | + app = create_app(cmdline_args) |
| 73 | + app.run(debug=cmdline_args.debug, |
| 74 | + host=cmdline_args.host, port=cmdline_args.port, |
| 75 | + threaded=cmdline_args.threaded) |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + run() |
0 commit comments