Exemple #1
0
async def make_app():
    app = web.Application()
    app['config'] = config
    app['session_name'] = 'VODOMAT_SESSION'
    app.on_startup.extend([init_db, init_db_redis])
    app.on_cleanup.extend([close_db, close_db_redis])
    app.add_routes(routes)

    db_pool = await init_db(app)
    redis_pool = await init_db_redis(app)

    setup_session(
        app,
        RedisStorage(redis_pool, cookie_name=app['session_name'],
                     max_age=3600))
    setup_security(app, SessionIdentityPolicy(),
                   DBAuthorizationPolicy(db_pool))

    aiohttp_jinja2.setup(
        app,
        loader=jinja2.FileSystemLoader('{}/templates'.format(
            os.path.dirname(__file__))),
        filters={'datetime_from_timestamp': datetime_from_timestamp_filter},
        context_processors=[current_user_context_processor],
    )
    setup_middlewares(app)

    aiojobs.aiohttp.setup(app)

    return app
Exemple #2
0
async def init(loop):
  # Redis
  redis_opts = (config['redis']['host'], config['redis']['port'])
  redis_pool = await create_pool(redis_opts)

  # SQLAlchemy
  sa_cfg = config['postgres']
  dbengine = await create_engine(database=sa_cfg.get('database', 'aiohttp_security'),
                                 user=sa_cfg.get('user', 'admin'))

  app = web.Application()
  app.db_engine = dbengine
  # Auth
  setup_session(app, RedisStorage(redis_pool))
  setup_security(app,
                 SessionIdentityPolicy(),
                 DBAuthorizationPolicy(dbengine))
  aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('./templates'))
  setup_routes(app)
  setup_middlewares(app)


  handler = app._make_handler()

  app_conf = config['app']
  srv = await loop.create_server(handler,
                                 app_conf['host'],
                                 app_conf['port'])

  print('Server started at http://{}:{}'.format(str(app_conf['host']), str(app_conf['port'])))

  return srv, app, handler
Exemple #3
0
async def init(loop):
    redis_pool = await create_pool(('localhost', 6379))
    dbengine = await create_engine(user=config['postgres']['user'],
                                   password=config['postgres']['password'],
                                   database=config['postgres']['database'],
                                   host=config['postgres']['host'])

    app = web.Application()
    app.telegram_token = config['telegram_token']
    app.API_URL = API_URL
    app.dbengine = dbengine
    #app.logger=AccessLogger()
    setup_session(app, RedisStorage(redis_pool))
    setup_security(app, SessionIdentityPolicy(),
                   DBAuthorizationPolicy(dbengine))
    web_handlers = Web()
    web_handlers.configure(app)

    #aiohttp_jinja2.setup(app,
    #                     loader=jinja2.FileSystemLoader(str(base_dir / 'robo_app' / 'templates')))
    aiohttp_jinja2.setup(app,
                         loader=jinja2.FileSystemLoader(str(TEMPLATES_ROOT)))

    handler = app.make_handler()
    srv = await loop.create_server(handler, '127.0.0.1', 8082)
    print('Server started at http://127.0.0.1:8082')

    return srv, app, handler
Exemple #4
0
async def init_app(config):

    app = web.Application()

    app['config'] = config

    setup_routes(app)

    db_pool = await init_db(app)

    redis_pool = await setup_redis(app)
    setup_session(app, RedisStorage(redis_pool))

    # needs to be after session setup because of `current_user_ctx_processor`
    aiohttp_jinja2.setup(
        app,
        loader=jinja2.PackageLoader(PACKAGE_NAME),
        context_processors=[current_user_ctx_processor],
    )

    setup_security(app, SessionIdentityPolicy(),
                   DBAuthorizationPolicy(db_pool))

    log.debug(app['config'])

    return app
Exemple #5
0
def run_server(host, port):
    loop = asyncio.get_event_loop()
    redis_pool = loop.run_until_complete(create_pool(('localhost', 6379)))
    client = motor_asyncio.AsyncIOMotorClient('localhost', 27017)

    # Initiate main application
    app = web.Application(loop=loop, logger=server_logger)
    setup_routes(app, 'main')
    setup_static(app)
    aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('templates/'))

    # Initiate sub-applications
    admin = web.Application(loop=loop, logger=server_logger)
    setup_routes(admin, 'admin')
    aiohttp_jinja2.setup(admin, loader=jinja2.FileSystemLoader('templates/'))
    setup_session(
        admin,
        RedisStorage(redis_pool,
                     max_age=COOKIE_AGE,
                     cookie_name=COOKIE_AUTH_NAME))
    setup_security(admin, SessionIdentityPolicy(), DBAuthorizationPolicy())

    app.add_subapp('/admin/', admin)

    api = web.Application(loop=loop, logger=server_logger)
    setup_routes(api, 'api')
    app.add_subapp('/api/', api)

    # Session and security
    setup_session(
        app,
        RedisStorage(redis_pool,
                     max_age=COOKIE_AGE,
                     cookie_name=COOKIE_AUTH_NAME))
    setup_security(app, SessionIdentityPolicy(), DBAuthorizationPolicy())

    # Run server
    web.run_app(app=app,
                host=host,
                port=port,
                access_log=access_logger,
                access_log_format=
                '%a %l %u %t "%r" %s %b "%{Referrer}i" "%{User-Agent}i"')
Exemple #6
0
async def init_pg(app):
    conf = app['config']['postgres']
    engine = await aiopg.sa.create_engine(
        database=conf['database'],
        user=conf['user'],
        password=conf['password'],
        host=os.environ.get('POSTGRES_HOST', conf['host']),
        port=conf['port'],
        minsize=conf['minsize'],
        maxsize=conf['maxsize'],
    )
    app.db_engine = engine
    setup_security(app,
                   CookiesIdentityPolicy(),
                   DBAuthorizationPolicy(engine))
    return engine
Exemple #7
0
async def init_app(config) -> Application:
    app = web.Application(middlewares=[error_middleware])
    setup_metrics(app, "otus-auth")
    app['config'] = config
    app.add_routes(routes)

    db_pool = await init_db(app)

    redis_pool = await setup_redis(app)
    setup_session(app, RedisStorage(redis_pool))
    setup_security(
        app,
        SessionIdentityPolicy(),
        DBAuthorizationPolicy(db_pool)
    )

    log.debug(app['config'])
    return app
Exemple #8
0
async def init_app(config) -> Application:
    app = web.Application()

    app['config'] = config

    load_and_connect_all_endpoints_from_folder(path='endpoints',
                                               app=app,
                                               version_prefix='v1')

    db_pool = await init_db(app)

    redis_pool = await setup_redis(app)
    setup_session(app, RedisStorage(redis_pool))

    setup_security(app, SessionIdentityPolicy(),
                   DBAuthorizationPolicy(db_pool))

    log.debug(app['config'])

    return app
Exemple #9
0
def main():
    app = web.Application()
    with open("config.json", "r") as f:
        config = json.load(f)
    app["config"] = config
    cors = setup_cors(
        app,
        defaults={
            "*":
            ResourceOptions(
                allow_credentials=True,
                expose_headers="*",
                allow_headers="*",
            )
        },
    )
    app["cors"] = cors
    setup_session(app, SimpleCookieStorage())
    setup_security(app, SessionIdentityPolicy(), DBAuthorizationPolicy())
    app.on_startup.append(init_db)
    app.on_startup.append(init_handlers)
    app.on_startup.append(init_superuser)
    web.run_app(app, host=config["host"], port=config["port"])
Exemple #10
0
async def init_app(config):

    app = web.Application()
    app['config'] = config

    setup_routes(app)

    db_pool = await init_db(app)

    redis_pool = await setup_redis(app)
    setup_session(app, RedisStorage(redis_pool))

    aiohttp_jinja2.setup(
        app,
        loader=jinja2.PackageLoader('for_templates', 'templates'),
        context_processors=[current_user_ctx_processor],
    )

    setup_security(app, SessionIdentityPolicy(),
                   DBAuthorizationPolicy(db_pool))

    log.debug(app['config'])

    return app