예제 #1
0
def app(loop, config, store, jwt_key, cookie_key):
    logger.debug('Creating application')
    from os.path import dirname, normpath
    from warnings import warn
    from aiohttp.web import Application
    from aiohttp_jinja2 import setup as jinja_setup
    from jinja2 import FileSystemLoader
    from rx.scheduler.eventloop import AsyncIOScheduler
    from bigur.auth.middlewares import session
    app = Application(middlewares=[session])
    app['config'] = config
    app['jwt_keys'] = [jwt_key]
    app['cookie_key'] = cookie_key
    app['scheduler'] = AsyncIOScheduler(loop)
    app['provider'] = {}
    templates = normpath(dirname(__file__) + '/../templates')
    jinja_setup(app, loader=FileSystemLoader(templates))

    class WarnWrapper:
        def __getattr__(self, name):
            warn('Use of app[\'store\'] is depricated',
                 DeprecationWarning,
                 stacklevel=2)
            return getattr(store, name)

        def __setattr__(self, name, value):
            warn('Use of app[\'store\'] is depricated',
                 DeprecationWarning,
                 stacklevel=2)
            return setattr(store, name, value)

    app['store'] = WarnWrapper()

    return app
예제 #2
0
async def spawn_app(loop=None):
    logging.basicConfig(level=logging.DEBUG,
                        format='[%(asctime)s] [%(levelname)s] %(message)s)')
    if not loop:
        loop = asyncio.get_event_loop()
    app = web.Application(middlewares=[IndexView.team_middleware])

    jinja_setup(app, loader=jinja2.FileSystemLoader('./templates/'))

    app.router.add_view('/{task_id}/', IndexView)
    app.router.add_static('/static/', path='./static')

    app['tokens'] = {}
    app['db'] = await db_connect(loop)

    return app
예제 #3
0
parser.add_argument("--port",
                    type=int,
                    default=8080,
                    help="The port to run the webserver on.")
args = parser.parse_args()

# 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())
app['static_root_url'] = '/static'
session_setup(app, ECS(os.urandom(32), max_age=1000000))  # Encrypted cookies
# session_setup(app, SimpleCookieStorage(max_age=1000000))  # Simple cookies DEBUG ONLY
jinja_setup(app, loader=FileSystemLoader(os.getcwd() + '/website/templates'))
app.router.add_routes(website.frontend_routes)
app.router.add_routes(website.backend_routes)
app.router.add_routes(website.api_routes)
app.router.add_static('/static',
                      os.getcwd() + '/website/static',
                      append_version=True)

# Add our connections
app['database'] = utils.DatabaseConnection
utils.DatabaseConnection.logger = logger.getChild("db")

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

# Add our bots
예제 #4
0
        return response


app = Application(middlewares=[unauthenticated_middleware])

app.db = None


# Temporal, cambiará bastante en base se desarrolle la abstracción de bd
async def setup_connection_pool(app_: Application):
    if not app_.db:
        app_.db = await asyncpg.create_pool(dsn=db_dsn)


# Registrar Jinja2
jinja_setup(app, loader=FileSystemLoader(templates_path))

# Registrar rutas de módulos
router = router.Router(app)
router.update(modules)
router.register()

# Registrar recursos estáticos
static_resources_path = Path(static_resources_path)
app.router.add_static('/', static_resources_path)


def main():
    loop = asyncio.get_event_loop()
    loop.run_until_complete(setup_connection_pool(app))
    db_session_setup(app, database_session.DatabaseStorage(app.db))
예제 #5
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()
예제 #6
0
    render_template as html
)

# Site
from multidict import MultiDict
from discord.ext import tasks
from discord.ext.commands import Cog

# Local


app = web.Application()
routes = web.RouteTableDef()

jinja_setup(
    app, loader=jinja2.FileSystemLoader(os.path.join(os.getcwd(), "web_templates"))
)


class Webserver(Cog):
    def __init__(self, bot):
        self.bot = bot
        self.web_server.start()
        self.ret = None

        @routes.get('/')
        async def index(request):
            raise web.HTTPFound("/ping")
        
        @routes.get('/ping')
        async def ping(request):
예제 #7
0
    command_prefix=prefix,
    description=
    "A small bot with commands to utilise your Minecraft/Discord experience",
    owner_id=272967064531238912,
    status=Status.idle,
    activity=Activity(type=2, name="ambient Minecraft music"),
    case_insensitive=True,
)

# Create website
site = web.Application()
site["bot"] = bot
site["db"] = bot.db
site.logger = bot._weblogger
rand = lambda: choice("abcdefghijklmnopqrstuvwxyz")
jinja_setup(site, loader=FileSystemLoader("./website/templates"))
session_setup(
    site,
    EncryptedCookieStorage(
        bot.env.get("WEBSITE_COOKIE_TOKEN",
                    rand() * 32).encode(),
        cookie_name="MOOPITYMOOP",
        max_age=3600 * 24 * 31,  # One month
    ))
for mw in middlewares:
    site.middlewares.append(mw)
getLogger("aiohttp_session").setLevel(40)


# Bot Events
@bot.event