Exemplo n.º 1
0
async def start_web_endpoint(config: 'Configuration',
                             integration_context: 'IntegrationContext'):

    LOG.info('Starting web endpoint...')

    web_coros = []

    # prepare the web application
    app = Application(client_max_size=CLIENT_MAX_SIZE)

    jwt = None  # type: Optional[JWTValidator]
    if config.jwks_url:
        LOG.info('JWKS URL: %r', config.jwks_url)
        jwt = JWTValidator(jwks_urls=[config.jwks_url])

        web_coros.append(ensure_future(jwt.poll()))
    else:
        LOG.warn(
            'No JWKS URL Available, all requests requiring authorization will be rejected.'
        )

    auth_handler = AuthHandler(config, jwt)
    await auth_handler.setup(app)

    app.add_routes(_build_control_routes(integration_context))

    if integration_context.webhook_context:
        app.add_routes(integration_context.webhook_context.route_table)

    LOG.info('Starting web server on %s...', config.health_port)
    runner = AppRunner(app,
                       access_log_class=IntegrationAccessLogger,
                       access_log_format='%a %t "%r" %s %b')
    await runner.setup()
    site = TCPSite(runner, '0.0.0.0', config.health_port)

    web_coros.append(ensure_future(site.start()))

    LOG.info('...Web server started')

    return gather(*web_coros)
Exemplo n.º 2
0
    logger.info("Logging in bot")
    loop.run_until_complete(app['bot'].login(app['config']['token']))

    # Connect the database
    logger.info("Creating database pool")
    loop.run_until_complete(
        utils.DatabaseConnection.create_pool(app['config']['database']))

    # HTTP server
    logger.info("Creating webserver...")
    application = AppRunner(app)
    loop.run_until_complete(application.setup())
    webserver = TCPSite(application, host=args.host, port=args.port)

    # Start server
    loop.run_until_complete(webserver.start())
    logger.info(f"Server started - http://{args.host}:{args.port}/")

    # This is the forever loop
    try:
        logger.info("Running webserver")
        loop.run_forever()
    except KeyboardInterrupt:
        pass

    # Clean up our shit
    logger.info("Closing webserver")
    loop.run_until_complete(application.cleanup())
    logger.info("Closing database pool")
    loop.run_until_complete(utils.DatabaseConnection.pool.close())
    logger.info("Closing bot")
Exemplo n.º 3
0
def run_website(args: argparse.Namespace) -> None:
    """
    Starts the website, connects the database, logs in the specified bots, runs the async loop forever.

    Args:
        args (argparse.Namespace): The arguments namespace that wants to be run.
    """

    # Load our imports here so we don't need to require them all the time
    from aiohttp.web import Application, AppRunner, TCPSite
    from aiohttp_jinja2 import setup as jinja_setup
    from aiohttp_session import setup as session_setup, SimpleCookieStorage
    from aiohttp_session.cookie_storage import EncryptedCookieStorage as ECS
    from jinja2 import FileSystemLoader
    import re
    import html
    from datetime import datetime as dt
    import markdown

    os.chdir(args.website_directory)
    set_event_loop()

    # Read config
    with open(args.config_file) as a:
        config = toml.load(a)

    # Create website object - don't start based on argv
    app = Application(loop=asyncio.get_event_loop(), debug=args.debug)
    app['static_root_url'] = '/static'
    for route in config['routes']:
        module = importlib.import_module(f"website.{route}", "temp")
        app.router.add_routes(module.routes)
    app.router.add_static('/static',
                          os.getcwd() + '/website/static',
                          append_version=True)

    # Add middlewares
    if args.debug:
        session_setup(app, SimpleCookieStorage(max_age=1_000_000))
    else:
        session_setup(app, ECS(os.urandom(32), max_age=1_000_000))
    jinja_env = jinja_setup(app,
                            loader=FileSystemLoader(os.getcwd() +
                                                    '/website/templates'))

    # Add our jinja env filters
    def regex_replace(string, find, replace):
        return re.sub(find, replace, string, re.IGNORECASE | re.MULTILINE)

    def escape_text(string):
        return html.escape(string)

    def timestamp(string):
        return dt.fromtimestamp(float(string))

    def int_to_hex(string):
        return format(hex(int(string))[2:], "0>6")

    def to_markdown(string):
        return markdown.markdown(string, extensions=['extra'])

    def display_mentions(string, users):
        def get_display_name(group):
            user = users.get(group.group('userid'))
            if not user:
                return 'unknown-user'
            return user.get('display_name') or user.get('username')

        return re.sub(
            '(?:<|(?:&lt;))@!?(?P<userid>\\d{16,23})(?:>|(?:&gt;))',
            lambda g:
            f'<span class="chatlog__mention">@{get_display_name(g)}</span>',
            string,
            re.IGNORECASE | re.MULTILINE,
        )

    def display_emojis(string):
        def get_html(group):
            return (
                f'<img class="discord_emoji" src="https://cdn.discordapp.com/emojis/{group.group("id")}'
                f'.{"gif" if group.group("animated") else "png"}" alt="Discord custom emoji: '
                f'{group.group("name")}" style="height: 1em; width: auto;">')

        return re.sub(
            r"(?P<emoji>(?:<|&lt;)(?P<animated>a)?:(?P<name>\w+):(?P<id>\d+)(?:>|&gt;))",
            get_html,
            string,
            re.IGNORECASE | re.MULTILINE,
        )

    jinja_env.filters['regex_replace'] = regex_replace
    jinja_env.filters['escape_text'] = escape_text
    jinja_env.filters['timestamp'] = timestamp
    jinja_env.filters['int_to_hex'] = int_to_hex
    jinja_env.filters['markdown'] = to_markdown
    jinja_env.filters['display_mentions'] = display_mentions
    jinja_env.filters['display_emojis'] = display_emojis

    # Add our connections and their loggers
    app['database'] = DatabaseWrapper
    app['redis'] = RedisConnection
    app['logger'] = logger.getChild("route")
    app['stats'] = StatsdConnection

    # Add our config
    app['config'] = config

    loop = app.loop

    # Connect the database pool
    if app['config'].get('database', {}).get('enabled', False):
        db_connect_task = start_database_pool(app['config'])
        loop.run_until_complete(db_connect_task)

    # Connect the redis pool
    if app['config'].get('redis', {}).get('enabled', False):
        re_connect = start_redis_pool(app['config'])
        loop.run_until_complete(re_connect)

    # Add our bots
    app['bots'] = {}
    for index, (bot_name, bot_config_location) in enumerate(
            config.get('discord_bot_configs', dict()).items()):
        bot = Bot(f"./config/{bot_config_location}")
        app['bots'][bot_name] = bot
        if index == 0:
            set_default_log_levels(args)
        try:
            loop.run_until_complete(bot.login())
            bot.load_all_extensions()
        except Exception:
            logger.error(f"Failed to start bot {bot_name}", exc_info=True)
            exit(1)

    # Start the HTTP server
    logger.info("Creating webserver...")
    application = AppRunner(app)
    loop.run_until_complete(application.setup())
    webserver = TCPSite(application, host=args.host, port=args.port)

    # Start the webserver
    loop.run_until_complete(webserver.start())
    logger.info(f"Server started - http://{args.host}:{args.port}/")

    # This is the forever loop
    try:
        logger.info("Running webserver")
        loop.run_forever()
    except KeyboardInterrupt:
        pass

    # We're now done running the bot, time to clean up and close
    loop.run_until_complete(application.cleanup())
    if config.get('database', {}).get('enabled', False):
        logger.info("Closing database pool")
        try:
            if DatabaseWrapper.pool:
                loop.run_until_complete(
                    asyncio.wait_for(DatabaseWrapper.pool.close(),
                                     timeout=30.0))
        except asyncio.TimeoutError:
            logger.error(
                "Couldn't gracefully close the database connection pool within 30 seconds"
            )
    if config.get('redis', {}).get('enabled', False):
        logger.info("Closing redis pool")
        RedisConnection.pool.close()

    logger.info("Closing asyncio loop")
    loop.stop()
    loop.close()
Exemplo n.º 4
0
def run_interactions(args: argparse.Namespace) -> None:
    """
    Starts the bot, connects the database, runs the async loop forever.

    Args:
        args (argparse.Namespace): The arguments namespace that wants to be run.
    """

    from aiohttp.web import Application, AppRunner, TCPSite
    os.chdir(args.bot_directory)
    set_event_loop()

    # And run file
    bot = Bot(config_file=args.config_file, intents=discord.Intents.none())
    loop = bot.loop
    EventLoopCallbackHandler.bot = bot

    # Set up loggers
    bot.logger = logger.getChild("bot")
    set_default_log_levels(args)

    # Connect the database pool
    if bot.config.get('database', {}).get('enabled', False):
        db_connect_task = start_database_pool(bot.config)
        loop.run_until_complete(db_connect_task)

    # Connect the redis pool
    if bot.config.get('redis', {}).get('enabled', False):
        re_connect = start_redis_pool(bot.config)
        loop.run_until_complete(re_connect)

    # Load the bot's extensions
    logger.info('Loading extensions... ')
    bot.load_all_extensions()

    # Run the bot
    logger.info("Logging in bot")
    loop.run_until_complete(bot.login())
    websocket_task = None
    if args.connect:
        logger.info("Connecting bot to gateway")
        websocket_task = loop.create_task(bot.connect())

    # Create the webserver
    app = Application(loop=asyncio.get_event_loop(), debug=args.debug)
    app.router.add_routes(
        commands.get_interaction_route_table(bot,
                                             bot.config.get("pubkey", ""),
                                             path=args.path))

    # Start the HTTP server
    logger.info("Creating webserver...")
    application = AppRunner(app)
    loop.run_until_complete(application.setup())
    webserver = TCPSite(application, host=args.host, port=args.port)

    # Start the webserver
    loop.run_until_complete(webserver.start())
    logger.info(f"Server started - http://{args.host}:{args.port}/")

    # This is the forever loop
    try:
        logger.info("Running webserver")
        loop.run_forever()
    except KeyboardInterrupt:
        pass

    # We're now done running the webserver, time to clean up and close
    if websocket_task:
        websocket_task.cancel()
    if bot.config.get('database', {}).get('enabled', False):
        logger.info("Closing database pool")
        try:
            if DatabaseWrapper.pool:
                loop.run_until_complete(
                    asyncio.wait_for(DatabaseWrapper.pool.close(),
                                     timeout=30.0))
        except asyncio.TimeoutError:
            logger.error(
                "Couldn't gracefully close the database connection pool within 30 seconds"
            )
    if bot.config.get('redis', {}).get('enabled', False):
        logger.info("Closing redis pool")
        RedisConnection.pool.close()

    logger.info("Closing asyncio loop")
    loop.stop()
    loop.close()
Exemplo n.º 5
0
if __name__ == '__main__':
    '''
    Starts the bot (and webserver if specified) and runs forever
    '''

    loop = bot.loop

    print("Starting bot...")
    bot.loop.create_task(bot.start_all())

    if not args.noserver:
        print("Starting server...")
        web_runner = AppRunner(app)
        loop.run_until_complete(web_runner.setup())
        site = TCPSite(web_runner, args.host, args.port)
        loop.run_until_complete(site.start())
        print(f"Server started: http://{args.host}:{args.port}/")

        # Store stuff in the bot for later
        bot.web_runner = web_runner

    # This is the forever loop
    try:
        loop.run_forever()
    except (Exception, KeyboardInterrupt):
        pass
    finally:
        # Logout the bot
        loop.run_until_complete(bot.logout())

        if not args.noserver: