from advcommon.parse import ArgumentParser from advcommon.logging import getLogger, INFO, Config as LogConfig from flask import Flask, request, jsonify from menu import get_menu from os import sep from json import dumps from pathlib import Path import yaml from base64 import b64decode from config import config from socket import gethostname, gethostbyname __author__ = 'jknoll' app = Flask(__name__) getLogger('suds').setLevel(INFO) _logger = getLogger(__name__) _logger_conf = LogConfig(application_name=__name__, log_filename=__name__).configure() @app.route('/') def root(): return app.send_static_file('index.html') @app.route('/api') def doc_api(): return '<HTML>API Documentation:<br>/api/menu<br>/api/yaml/{report_name}</HTML>' @app.route('/api/menu') def json_menu(): user = b64decode(request.headers.get('Authorization')[6:]).split(':')[0] _logger.debug("%s %s" % (user, ' Authenticated')) ''' user = 'roneal' # Lie!! (This lie is not complete it relates just to the displayed menu) _logger.debug( user, ' pretending I am') ''' return dumps(get_menu(user, config['Menu'], args.yaml_path)) @app.route('/api/yaml/<path:path>') def json_yaml(path): report_config = list() # better friendly indication we can not reach that report it could have been decommission for file_path in Path(yaml_path).glob('**/*.yaml'): if str(file_path).split(sep)[-1] == (path + '.yaml'): with open(str(file_path), 'r') as stream: report_config = yaml.load(stream) break return jsonify(report_config) @app.route('/up') def up(): return '<HTML>AAM_WEB Service is running</HTML>' def _parse_args(): parser = ArgumentParser() parser.add_environment_group(choices=('dev', 'prod')) parser.add_argument("-p", "--http_port", help='tcpip/port service should run on default 8080', type=int, default=8080) parser.add_argument("-y", "--yaml_path", help='path we will find al the report yaml files default ./yaml', default='./yaml') args = parser.parse_args() _logger.debug("arguments set to {}".format(vars(args))) return args if __name__ == '__main__': args = _parse_args() yaml_path = args.yaml_path app.run(host=gethostbyname(gethostname()), port=args.http_port, debug=True)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question