Example #1
0
def create_app(cmdline=False, test_config=None):
    app = Flask(__name__)
    env_var = "RELENGAPI_SETTINGS"
    if test_config:
        app.config.update(**test_config)
    else:
        if env_var in os.environ and os.environ[env_var]:
            app.config.from_envvar(env_var)
        else:
            logger.warning(
                "Using default settings; to configure relengapi, set " "%s to point to your settings file" % env_var
            )

    # add the necessary components to the app
    app.db = db.make_db(app)
    app.celery = celery.make_celery(app)
    layout.init_app(app)
    auth.init_app(app)
    api.init_app(app)
    aws.init_app(app)
    memcached.init_app(app)

    app.relengapi_blueprints = {}
    for bp in blueprints:
        if cmdline:
            logger.info("registering blueprint %s", bp.name)
        app.register_blueprint(bp, url_prefix="/%s" % bp.name)
        app.relengapi_blueprints[bp.name] = bp

    # set up a random session key if none is specified
    if not app.config.get("SECRET_KEY"):
        logger.warning("setting per-process session key - sessions will be reset on " "process restart")
        app.secret_key = os.urandom(24)

    @app.before_request
    def add_db():
        g.db = app.db

    @app.route("/")
    def root():
        # render all of the blueprints' templates first
        bp_widgets = []
        for bp in app.blueprints.itervalues():
            bp_widgets.extend(bp.root_widget_templates or [])
        bp_widgets.sort()
        bp_widgets = [tpl for (_, tpl, condition) in bp_widgets if not condition or condition()]
        return render_template("root.html", bp_widgets=bp_widgets)

    @app.route("/versions")
    @api.apimethod(VersionInfo)
    def versions():
        dists = {}
        for dist in introspection.get_distributions().itervalues():
            dists[dist.key] = DistributionInfo(
                project_name=dist.project_name, version=dist.version, relengapi_metadata={}
            )
        blueprints = {}
        relengapi_dist = pkg_resources.get_distribution("relengapi")
        for bp in app.relengapi_blueprints.itervalues():
            blueprints[bp.name] = BlueprintInfo(distribution="relengapi", version=relengapi_dist.version)
        return VersionInfo(distributions=dists, blueprints=blueprints)

    return app
Example #2
0
def create_app(cmdline=False, test_config=None):
    app = Flask(__name__)
    relengapi_logging.configure_logging(app)
    logger = structlog.get_logger()

    env_var = "RELENGAPI_SETTINGS"
    if test_config:
        app.config.update(**test_config)
    else:
        if env_var in os.environ and os.environ[env_var]:
            app.config.from_envvar(env_var)
        else:
            logger.warning(
                "Using default settings; to configure relengapi, set " "%s to point to your settings file" % env_var
            )

    # reconfigure logging now that we have loaded configuration
    relengapi_logging.configure_logging(app)
    # and re-construct the logger to get updated configuration
    logger = structlog.get_logger()

    # add the necessary components to the app
    app.db = db.make_db(app)
    app.celery = celery.make_celery(app)
    layout.init_app(app)
    auth.init_app(app)
    api.init_app(app)
    aws.init_app(app)
    memcached.init_app(app)

    app.relengapi_blueprints = {}
    for bp in blueprints:
        if cmdline:
            logger.info("registering blueprint %s", bp.name)
        app.register_blueprint(bp, url_prefix="/%s" % bp.name)
        app.relengapi_blueprints[bp.name] = bp

    # set up a random session key if none is specified
    if not app.config.get("SECRET_KEY"):
        logger.warning("setting per-process session key - sessions will be reset on " "process restart")
        app.secret_key = os.urandom(24)

    request_id_header = app.config.get("REQUEST_ID_HEADER")

    def get_req_id_uuid():
        return str(uuid.uuid4())

    def get_req_id_header():
        return request.headers.get(request_id_header) or get_req_id_uuid()

    get_req_id = get_req_id_header if request_id_header else get_req_id_uuid

    @app.before_request
    def setup_request():
        # set up `g`
        g.db = app.db
        g.request_id = get_req_id()

        # reset the logging context, deleting any info for the previous request
        # in this thread and binding new
        relengapi_logging.reset_context(request_id=g.request_id, user=str(current_user))

    @app.route("/")
    def root():
        # render all of the blueprints' templates first
        bp_widgets = []
        for bp in app.blueprints.itervalues():
            bp_widgets.extend(bp.root_widget_templates or [])
        bp_widgets.sort()
        bp_widgets = [tpl for (_, tpl, condition) in bp_widgets if not condition or condition()]
        return render_template("root.html", bp_widgets=bp_widgets)

    @app.route("/robots.txt")
    def robots_txt():
        return render_template("robots.txt")

    @app.route("/versions")
    @api.apimethod(VersionInfo)
    def versions():
        dists = {}
        for dist in introspection.get_distributions().itervalues():
            dists[dist.key] = DistributionInfo(
                project_name=dist.project_name, version=dist.version, relengapi_metadata={}
            )
        blueprints = {}
        relengapi_dist = pkg_resources.get_distribution("relengapi")
        for bp in app.relengapi_blueprints.itervalues():
            blueprints[bp.name] = BlueprintInfo(distribution="relengapi", version=relengapi_dist.version)
        return VersionInfo(distributions=dists, blueprints=blueprints)

    return app