From 9739bfe06e1e5282dafa61c0958d329065caf54d Mon Sep 17 00:00:00 2001 From: Uggla Date: Fri, 26 Jun 2015 01:10:48 +0200 Subject: [PATCH] Add server running port from configuration file. Add a method to shutdown the server. Refine some code. --- alexandria-client/alexandria-client.py | 1 + alexandria/alexandria.conf | 2 +- alexandria/app.py | 37 +++++++++++++++++--------- alexandria/config.py | 5 +++- examples/example.py | 1 + 5 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 alexandria-client/alexandria-client.py create mode 100644 examples/example.py diff --git a/alexandria-client/alexandria-client.py b/alexandria-client/alexandria-client.py new file mode 100644 index 0000000..bf893c0 --- /dev/null +++ b/alexandria-client/alexandria-client.py @@ -0,0 +1 @@ +# coding=utf-8 \ No newline at end of file diff --git a/alexandria/alexandria.conf b/alexandria/alexandria.conf index 0ca2580..a3763bb 100644 --- a/alexandria/alexandria.conf +++ b/alexandria/alexandria.conf @@ -1,5 +1,5 @@ [alexandria] -port=80 +port=8080 [itop] drvtype=cmdb diff --git a/alexandria/app.py b/alexandria/app.py index bcd84b9..be7c650 100644 --- a/alexandria/app.py +++ b/alexandria/app.py @@ -1,50 +1,61 @@ # coding=utf-8 from flask import Flask -#from flask import Response from flask import jsonify +from flask import request import config - # Vars alexandria_version="0.1" # Configuration file -conf_file = config.AlexandriaConfiguration('alexandria.conf') +conf_file = config.AlexandriaConfiguration("alexandria.conf") # Initialise Flask app = Flask(__name__) -app.debug = True +app.debug = False -@app.route('/drivers', methods = ['GET']) + +@app.route("/drivers", methods = ["GET"]) def api_drivers(): - data = {'drivers' : conf_file.get_drivers()} + data = {"drivers" : conf_file.get_drivers()} resp = jsonify(data) resp.status_code = 200 return resp -@app.route('/drivers/') +@app.route("/drivers/") def api_driver(driver_name): data = {driver_name : conf_file.get_driver_info(driver_name)} resp = jsonify(data) resp.status_code = 200 return resp -@app.route('/', methods = ['GET']) +@app.route('/shutdown', methods=['POST']) +def shutdown(): + shutdown_server() + return 'Server shutting down...' + +@app.route("/", methods = ["GET"]) def api_root(): global alexandria_version data = { - 'Service' : 'Alexandria', - 'Version' : alexandria_version + "Service" : "Alexandria", + "Version" : alexandria_version } resp = jsonify(data) resp.status_code = 200 - resp.headers['Link'] = 'http://luisrei.com' + resp.headers["AuthorSite"] = "http://uggla.fr" return resp +def shutdown_server(): + func = request.environ.get('werkzeug.server.shutdown') + if func is None: + raise RuntimeError('Not running with the Werkzeug Server') + func() -if __name__ == '__main__': - app.run() + +if __name__ == "__main__": + app.run(port=int(conf_file.get_alexandria_port())) diff --git a/alexandria/config.py b/alexandria/config.py index cd6ebab..058d85f 100644 --- a/alexandria/config.py +++ b/alexandria/config.py @@ -13,4 +13,7 @@ class AlexandriaConfiguration(object): return self.config.sections() def get_driver_info(self,driver): - return self.config.options(driver) \ No newline at end of file + return self.config.options(driver) + + def get_alexandria_port(self): + return self.config.get("alexandria", "port") \ No newline at end of file diff --git a/examples/example.py b/examples/example.py new file mode 100644 index 0000000..bf893c0 --- /dev/null +++ b/examples/example.py @@ -0,0 +1 @@ +# coding=utf-8 \ No newline at end of file