def create_app(config: CollectorConfig): app = Flask(__name__, instance_relative_config=True) client = AcsClient(ak=config.credential['access_key_id'], secret=config.credential['access_key_secret'], region_id=config.credential['region_id']) @app.route("/") def projectIndex(): req = QueryProjectMetaRequest() req.set_PageSize(100) try: resp = client.do_action_with_exception(req) except Exception as e: return render_template("error.html", errorMsg=e) data = json.loads(resp) return render_template("index.html", projects=data["Resources"]["Resource"]) @app.route("/projects/<string:name>") def projectDetail(name): req = QueryMetricMetaRequest() req.set_PageSize(100) req.set_Project(name) try: resp = client.do_action_with_exception(req) except Exception as e: return render_template("error.html", errorMsg=e) data = json.loads(resp) return render_template("detail.html", metrics=data["Resources"]["Resource"], project=name) @app.route("/yaml/<string:name>") def projectYaml(name): req = QueryMetricMetaRequest() req.set_PageSize(100) req.set_Project(name) try: resp = client.do_action_with_exception(req) except Exception as e: return render_template("error.html", errorMsg=e) data = json.loads(resp) return render_template("yaml.html", metrics=data["Resources"]["Resource"], project=name) app.jinja_env.filters['formatmetric'] = format_metric app.jinja_env.filters['formatperiod'] = format_period app_dispatch = dispatcher.DispatcherMiddleware( app, {'/metrics': make_wsgi_app()}) return app_dispatch
def create_app(config_object=None, testing=False, app_name=config.APP_NAME): app = Flask(__name__) app.secret_key = config.SECRET_KEY app.config.from_object(config) app.config['APP_NAME'] = app_name app.config['TESTING'] = testing app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SESSION_COOKIE_HTTPONLY'] = True app.config['SESSION_COOKIE_SECURE'] = False app.config['SESSION_COOKIE_SAMESITE'] = False if config_object: app.config.from_mapping(config_object) app.json_encoder = CustomJSONEncoder csrf.init_app(app) db = SQLAlchemy(app) migrate.init_app(app, db) # from sample_app.web.blueprints import routes # app.register_blueprint(routes) with app.app_context(): admin.init_app(app) @app.context_processor def jinja_globals(): _globals = { 'hostname': socket.getfqdn(), 'navbar_color': 'blue', } return _globals @app.teardown_appcontext def shutdown_session(exception=None): # pylint: disable=unused-argument db_session.remove() @app.route('/') @app.route('/index') def index(): return "Hello, World!" dashapp = create_dashapp() server = Flask('server') server.wsgi_app = dispatcher.DispatcherMiddleware( app, {'/dash': dashapp.server}) return server
def application_factory(name='public'): if name not in ('admin', 'public'): raise RuntimeError('Application name (for base_url lookup) must be ' 'either `admin` or `public`.') app = flask.Flask(name) # Register Error Handler Function for Keystone Errors. # NOTE(morgan): Flask passes errors to an error handling function. All of # keystone's api errors are explicitly registered in # keystone.exception.KEYSTONE_API_EXCEPTIONS and those are in turn # registered here to ensure a proper error is bubbled up to the end user # instead of a 500 error. for exc in exception.KEYSTONE_API_EXCEPTIONS: app.register_error_handler(exc, _handle_keystone_exception) # Register extra (python) exceptions with the proper exception handler, # specifically TypeError. It will render as a 400 error, but presented in # a "web-ified" manner app.register_error_handler(TypeError, _handle_unknown_keystone_exception) # Add core before request functions app.before_request(req_logging.log_request_info) app.before_request(json_body.json_body_before_request) # Add core after request functions app.after_request(_add_vary_x_auth_token_header) # NOTE(morgan): Configure the Flask Environment for our needs. app.config.update( # We want to bubble up Flask Exceptions (for now) PROPAGATE_EXCEPTIONS=True) for api in keystone.api.__apis__: for api_bp in api.APIs: api_bp.instantiate_and_register_to_app(app) # Load in Healthcheck and map it to /healthcheck hc_app = healthcheck.Healthcheck.app_factory( {}, oslo_config_project='keystone') # Use the simple form of the dispatch middleware, no extra logic needed # for legacy dispatching. This is to mount /healthcheck at a consistent # place app.wsgi_app = wsgi_dispatcher.DispatcherMiddleware( app.wsgi_app, {'/healthcheck': hc_app}) return app
def setup_app(): root_app = flask.Flask('cloudkitty') root_api = flask_restful.Api(root_app) root_api.add_resource(api_root.CloudkittyAPIRoot, '/') dispatch_dict = { '/v1': get_v1_app(), '/v2': get_v2_app(), } # Disabling v2 api in case v1 storage is used if CONF.storage.version < 2: LOG.warning('v1 storage is used, disabling v2 API') dispatch_dict.pop('/v2') app = dispatcher.DispatcherMiddleware(root_app, dispatch_dict) return app
'Current daily resource usage per namespace', ['namespace', 'resource']), RrdPeriod.PERIOD_YEAR_SEC: prometheus_client.Gauge('koa_namespace_monthly_usage', 'Current monthly resource usage per namespace', ['namespace', 'resource']) } # create Flask application app = flask.Flask(__name__, static_url_path=KOA_CONFIG.static_content_location, template_folder='.') cors = CORS(app, resources={r"/dataset/*": {"origins": "127.0.0.1"}}) # Add prometheus wsgi middleware to route /metrics requests wsgi_dispatcher = wsgi.DispatcherMiddleware( app, {'/metrics': prometheus_client.make_wsgi_app()}) @app.route('/favicon.ico') def favicon(): return flask.send_from_directory(os.path.join(app.root_path, 'static'), 'images/favicon.ico', mimetype='image/vnd.microsoft.icon') @app.after_request def add_header(r): r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate" r.headers["Pragma"] = "no-cache" r.headers["Expires"] = "0" r.headers['Cache-Control'] = 'public, max-age=0'