async def server_init(app, loop): container = Container(config, log_config) await container.on_init Session( app, AIORedisSessionInterface(container.cache, expiry=config['SESSION_EXPIRY']))
async def setup_db(app: Sanic, loop: Loop) -> None: global client, redis await init_db() redis = await get_redis(loop=loop) # init extensions fabrics session.init_app(app, interface=AIORedisSessionInterface(redis)) app.async_session = aiohttp.ClientSession() Path(config.UPLOAD_FOLDER).mkdir(parents=True, exist_ok=True)
async def session_and_logger_init(app, loop): # session app.redis = await aioredis.create_redis_pool("redis://localhost") Session().init_app(app, interface=AIORedisSessionInterface(app.redis)) app.oauth_client = aiohttp.ClientSession() # logger handler = RotatingFileHandler("sc2mafia.log", maxBytes=16 * 1024 * 1024, backupCount=64) handler.setLevel(logging.INFO) formatter = logging.Formatter( fmt="%(asctime)s - (%(name)s)[%(levelname)s]: %(message)s", datefmt="%Y-%m-%d %H:%M:%S") handler.setFormatter(formatter) logger.addHandler(handler)
async def start_db(app, loop): ''' before server starts ''' try: app.database = Database( os.getenv('DATABASE_URL', 'postgresql://<user>:<password>@localhost/<db-name>')) await app.database.connect() except Exception as e: print('Something went wrong connecting to DB: {}', e) app.redis = await aioredis.create_redis_pool( os.getenv('REDIS_URL', 'redis://localhost')) # init extensions fabrics session.init_app(app, interface=AIORedisSessionInterface(app.redis))
async def before_server_start(_app, _): """Initialize database connection and Redis cache.""" _app.ctx.engine = create_async_engine(SANIC_CONFIG["DB_URL"]) caches.set_config(redis_cache_config) _app.ctx.redis = await aioredis.Redis.from_url(_app.config["redis"]) # init extensions fabrics session.init_app( _app, interface=AIORedisSessionInterface( _app.ctx.redis, samesite="Strict", cookie_name="session" if _app.config["DEBUG"] else "__Host-session", ), )
async def start_db(app, loop): ''' before server starts ''' try: app.database = Database( os.getenv('DATABASE_URL', 'postgresql://*****:*****@localhost/hermes')) await app.database.connect() except Exception as e: print('Error connecting to database') app.redis = await aioredis.create_redis_pool( os.getenv('REDIS_URL', 'redis://localhost')) # init extensions fabrics session.init_app(app, interface=AIORedisSessionInterface(app.redis))
async def init_cache(_app: Sanic, _) -> None: """Initialize db connections, session_interface and cache.""" _app.ctx.engine = create_async_engine( "postgresql+asyncpg://" f"{SANIC_CONFIG['DB_USER']}:{SANIC_CONFIG['DB_PASSWORD']}" f"@{SANIC_CONFIG['DB_HOST']}/{SANIC_CONFIG['DB_DATABASE']}", pool_size=5, ) _app.ctx.redis = await aioredis.Redis.from_url(_app.config["redis"], decode_responses=True) # Pass the getter method for the connection pool into the session. session.init_app( _app, interface=AIORedisSessionInterface( _app.ctx.redis, samesite="Strict", secure=not _app.config["DEBUG"], cookie_name="session" if _app.config["DEBUG"] else "__Host-session", ), )
async def server_init(app, loop): app.redis = await RedisPool.get_pool() session.init_app(app, interface=AIORedisSessionInterface(app.redis))
async def setup_redis(app, loop): app.redis = await aioredis.create_redis_pool(loop=loop, **redis_config) session = Session() session.init_app(app, interface=AIORedisSessionInterface(app.redis))
async def server_init(app, loop): app.db = await db_setup() app.redis = await aioredis.create_redis_pool(app.config['REDIS_URL']) session.init_app(app=app, interface=AIORedisSessionInterface(app.redis))
async def server_init(app, loop): app.redis = await aioredis.create_redis_pool(app.config["REDIS"], loop=loop, db=15) Session(app, interface=AIORedisSessionInterface(app.redis))