Add server running port from configuration file.

Add a method to shutdown the server.
Refine some code.
This commit is contained in:
Uggla 2015-06-26 01:10:48 +02:00
parent 6bc505011f
commit 9739bfe06e
5 changed files with 31 additions and 15 deletions

View File

@ -0,0 +1 @@
# coding=utf-8

View File

@ -1,5 +1,5 @@
[alexandria] [alexandria]
port=80 port=8080
[itop] [itop]
drvtype=cmdb drvtype=cmdb

View File

@ -1,50 +1,61 @@
# coding=utf-8 # coding=utf-8
from flask import Flask from flask import Flask
#from flask import Response
from flask import jsonify from flask import jsonify
from flask import request
import config import config
# Vars # Vars
alexandria_version="0.1" alexandria_version="0.1"
# Configuration file # Configuration file
conf_file = config.AlexandriaConfiguration('alexandria.conf') conf_file = config.AlexandriaConfiguration("alexandria.conf")
# Initialise Flask # Initialise Flask
app = Flask(__name__) app = Flask(__name__)
app.debug = True app.debug = False
@app.route('/drivers', methods = ['GET'])
@app.route("/drivers", methods = ["GET"])
def api_drivers(): def api_drivers():
data = {'drivers' : conf_file.get_drivers()} data = {"drivers" : conf_file.get_drivers()}
resp = jsonify(data) resp = jsonify(data)
resp.status_code = 200 resp.status_code = 200
return resp return resp
@app.route('/drivers/<driver_name>') @app.route("/drivers/<driver_name>")
def api_driver(driver_name): def api_driver(driver_name):
data = {driver_name : conf_file.get_driver_info(driver_name)} data = {driver_name : conf_file.get_driver_info(driver_name)}
resp = jsonify(data) resp = jsonify(data)
resp.status_code = 200 resp.status_code = 200
return resp 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(): def api_root():
global alexandria_version global alexandria_version
data = { data = {
'Service' : 'Alexandria', "Service" : "Alexandria",
'Version' : alexandria_version "Version" : alexandria_version
} }
resp = jsonify(data) resp = jsonify(data)
resp.status_code = 200 resp.status_code = 200
resp.headers['Link'] = 'http://luisrei.com' resp.headers["AuthorSite"] = "http://uggla.fr"
return resp 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()))

View File

@ -14,3 +14,6 @@ class AlexandriaConfiguration(object):
def get_driver_info(self,driver): def get_driver_info(self,driver):
return self.config.options(driver) return self.config.options(driver)
def get_alexandria_port(self):
return self.config.get("alexandria", "port")

1
examples/example.py Normal file
View File

@ -0,0 +1 @@
# coding=utf-8