from flask import Flask, jsonify, make_response from config import RESPONSE_NOT_FOUND, DB, DevConfig from repository import Repository from urls import rules from flask_restful import Api app = Flask(__name__) app.config.from_object(DevConfig) DB.init_app(app) with app.app_context(): DB.drop_all() DB.create_all() Repository().create_initial_testing_data() api = Api(app) for rule in rules: api.add_resource(rule.view, rule.url) @app.errorhandler(RESPONSE_NOT_FOUND) def not_found(error): return make_response(jsonify({'error': 'Not found'}), RESPONSE_NOT_FOUND) if __name__ == '__main__': app.run(debug=True, host='0.0.0.0')
from config import DB, CONFIG import routers app = hug.API(__name__) router = hug.route.API(app) router.get('/metrics', output=hug.output_format.text, api=app)(routers.generate_stats) router.get('/devices', api=app)(routers.list_devices) router.post('/devices/{device_id}/activate', api=app)(routers.acitvate_device) router.post('/devices/discover', api=app)(routers.do_discovery) initialize_logging(CONFIG.LOG_MODE, debug=CONFIG.DEBUG_MODE) setup_access_log(app, Logger('access_log')) DB.init_app(app, CONFIG.SQLALCHEMY_DATABASE_URI) DB.create_models() def signal_handler(signal, frame): if ServiceDiscovery.service_browser.is_alive(): ServiceDiscovery.service_browser.cancel() sys.exit(0) signal.signal(signal.SIGINT, signal_handler) @hug.exception(api=app) def handle_exception(exception):