Example #1
0
def register_blueprints(app: QuartTrio) -> None:
    """Register Flask blueprints / LNbits extensions."""
    app.register_blueprint(core_app)

    for ext in get_valid_extensions():
        try:
            ext_module = importlib.import_module(f"lnbits.extensions.{ext.code}")
            bp = getattr(ext_module, f"{ext.code}_ext")

            app.register_blueprint(bp, url_prefix=f"/{ext.code}")
        except Exception:
            raise ImportError(
                f"Please make sure that the extension `{ext.code}` follows conventions."
            )
Example #2
0
def register_blueprints(app: QuartTrio) -> None:
    """Register Flask blueprints / LNbits extensions."""
    app.register_blueprint(core_app)

    for ext in get_valid_extensions():
        try:
            ext_module = importlib.import_module(
                f"lnbits.extensions.{ext.code}")
            bp = getattr(ext_module, f"{ext.code}_ext")

            @bp.before_request
            async def before_request():
                g.ext_db = open_ext_db(ext.code)

            @bp.teardown_request
            async def after_request(exc):
                g.ext_db.close()

            app.register_blueprint(bp, url_prefix=f"/{ext.code}")
        except Exception:
            raise ImportError(
                f"Please make sure that the extension `{ext.code}` follows conventions."
            )
Example #3
0
    async def serve(self,
                    title,
                    host,
                    port,
                    *,
                    task_status=anyio.TASK_STATUS_IGNORED):
        """Web view server task."""

        # (quart and hypercorn should have a common API for asyncio and trio...)
        async_lib = sniffio.current_async_library()
        if async_lib == 'trio':
            from quart_trio import QuartTrio  # pylint: disable=import-outside-toplevel,import-error
            web_app = QuartTrio('pura')
            web_app.register_blueprint(self.get_blueprint(title))
            async with anyio.create_task_group() as tg:
                urls = await tg.start(
                    hypercorn.trio.serve, web_app,
                    hypercorn.Config.from_mapping(
                        bind=[f'{host}:{port}'],
                        loglevel='WARNING',
                    ))
                _logger.info(f'listening on {urls[0]}')
                task_status.started()
        elif async_lib == 'asyncio':
            web_app = quart.Quart('pura')
            web_app.register_blueprint(self.get_blueprint(title))
            task_status.started()
            await hypercorn.asyncio.serve(
                web_app,
                hypercorn.Config.from_mapping(
                    bind=[f'{host}:{port}'],
                    loglevel='INFO',
                    graceful_timeout=.2,
                ))
            raise CancelledError
        else:
            raise RuntimeError('unsupported async library:', async_lib)
Example #4
0
    outcome = {"win": "a", "loss": "b", "draw": "draw"}[outcome]
    listy = "," in team_a
    team_a = [int(score) for score in team_a.split(",")]
    team_b = [int(score) for score in team_b.split(",")]
    assert len(team_a) == len(
        team_b), "Teams have to have the same amount of players"
    mod_a, mod_b = _elo(team_a, team_b, outcome, k)
    if listy:
        return jsonify([[round(x + mod_a) for x in team_a],
                        [round(x + mod_b) for x in team_b]])
    return jsonify([round(team_a[0] + mod_a), round(team_b[0] + mod_b)])


app.route("/slack", methods=["POST"])(slack)

app.register_blueprint(chord_views, url_prefix="/chords")
app.register_blueprint(krank_views, url_prefix="/krank")
app.register_blueprint(grafana_views, url_prefix="/grafana")


@app.after_request
def add_cors(response):
    response.headers["Access-Control-Allow-Origin"] = "*"
    response.headers[
        "Access-Control-Allow-Headers"] = "Content-Type,Authorization"
    response.headers["Access-Control-Allow-Methods"] = "GET,POST"
    return response


if __name__ == "__main__":
    app.run()