async def main(): app = web.Application() app['websockets'] = [] redis_pool = await aioredis.create_pool('redis://localhost') storage = redis_storage.RedisStorage(redis_pool, max_age=600) app['redis_pool'] = redis_pool conn = await asyncpg.connect(user='******', database='chat', host='127.0.0.1') app['conn'] = conn async def dispose_redis_pool(app): try: await redis_pool.execute('FLUSHALL') except RuntimeError: # when no keys are registered in redis pass redis_pool.close() await redis_pool.wait_closed() await conn.execute('''TRUNCATE "log"''') await conn.close() setup(app, storage) app.on_cleanup.append(dispose_redis_pool) app.add_routes(routes) return app
async def run(): app = web.Application() routes.static('/', './static') app.add_routes(routes) redis = await create_pool((getenv('redis'), 6379)) storage = redis_storage.RedisStorage(redis) setup(app, storage) shutil.rmtree(spool, ignore_errors=True) makedirs(spool) return app
def configure_redis(app): """ Configure Redis for sessions """ # save sessions thru redis loop = asyncio.get_event_loop() redis_pool = loop.run_until_complete(make_redis_pool()) setup( app, redis_storage.RedisStorage(redis_pool=redis_pool, cookie_name='FIXATED')) async def dispose_redis_pool(app_): """Gracefully closing underlying connection""" redis_pool.close() await redis_pool.wait_closed() app.on_cleanup.append(dispose_redis_pool)
async def init(loop, config_args=None): # CONFIG config.configure(config_args) logger.info('Env: {env}'.format(env=config.get('env'))) # SESSION redis_db = config.get('redis_database', 0) redis_pool = await aioredis.create_pool(('localhost', 6379), db=redis_db) storage = redis_storage.RedisStorage( redis_pool, cookie_name="AIOHTTP_SESSION-{redis_db}".format(redis_db=redis_db)) app = web.Application( loop=loop, middlewares=[session_middleware(storage), db_handler]) app.redis_pool = redis_pool # QUEUE app.queue = Queue(connection=Redis()) # WEBSOCKET """ app['websockets'] = [] """ handler = app.make_handler() # ROUTES for route in routes: app.router.add_route(route[0], route[1], route[2], name=route[3]) if config.get('env', 'production') == 'development': static_path = os.path.join(ROOT, 'dist-dev') else: if config.get('release', 'latest') == 'latest': latest_version_path = os.path.join(ROOT, 'releases', 'latest.txt') if os.path.exists(latest_version_path): with open(latest_version_path, 'r') as fd: release_version = fd.read() else: raise Exception("The latest.txt file doesn't exists") else: release_version = config.get('release') static_path = os.path.join(ROOT, 'releases', release_version) app.router.add_static('/', static_path, name='static') logger.info( "Serving static: {static_path}".format(static_path=static_path)) aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader(static_path)) # PREPARE HOOK async def after_request(request, response): if hasattr(request, 'db_session'): request.db_session.end() request.db_session.db.client.close() app.on_response_prepare.append(after_request) # SHUTDOWN app.on_shutdown.append(on_shutdown) serv_generator = loop.create_server(handler, config.get('server_host'), config.get('server_port')) return serv_generator, handler, app
async def init(loop, port=None, watch_change=False): logging.basicConfig(level=logging.INFO) logger = logging.getLogger("magrit_app.main") redis_cookie = await create_pool(('0.0.0.0', 6379), db=0, maxsize=50, loop=loop) redis_conn = await create_reconnecting_redis(('0.0.0.0', 6379), db=1, loop=loop) app = web.Application(loop=loop, client_max_size=16384**2, middlewares=[ error_middleware, session_middleware( redis_storage.RedisStorage(redis_cookie)) ]) aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('templates')) add_route = app.router.add_route add_route('GET', '/', index_handler) add_route('GET', '/index', index_handler) add_route('GET', '/contact', serve_contact_form) add_route('POST', '/contact', store_contact_info) add_route('GET', '/modules', serve_main_page) add_route('GET', '/modules/', serve_main_page) add_route('GET', '/modules/{expr}', serve_main_page) # add_route('GET', '/layers', list_user_layers) add_route('POST', '/layers/add', receiv_layer) add_route('POST', '/layers/delete', remove_layer) add_route('GET', '/extrabasemaps', get_extrabasemaps) add_route('GET', '/get_layer/{expr}', handler_exists_layer) add_route('POST', '/get_layer2', handler_exists_layer2) add_route('POST', '/compute/{function}', geo_compute) add_route('POST', '/stats', get_stats_json) add_route('POST', '/sample', get_sample_layer) add_route('POST', '/convert_to_topojson', convert) add_route('POST', '/convert_topojson', convert_topo) add_route('POST', '/convert_csv_geo', convert_csv_geo) add_route('POST', '/convert_extrabasemap', convert_extrabasemap) add_route('POST', '/convert_tabular', convert_tabular) # add_route('POST', '/cache_topojson/{params}', cache_input_topojson) add_route('POST', '/helpers/calc', calc_helper) app.router.add_static('/static/', path='static', name='static') app['redis_conn'] = redis_conn app['app_users'] = set() app['logger'] = logger app['version'] = get_version() with open('static/json/sample_layers.json', 'r') as f: app['db_layers'] = json.loads(f.read().replace('/static', 'static'))[0] app['ThreadPool'] = ThreadPoolExecutor(4) app['ProcessPool'] = ProcessPoolExecutor(4) app['app_name'] = "Magrit" app['geo_function'] = { "stewart": call_stewart, "gridded": carto_gridded, "links": links_map, "carto_doug": carto_doug, "olson": compute_olson } if watch_change: app['FileWatcher'] = JsFileWatcher() # app.on_startup.append(on_startup) app.on_shutdown.append(on_shutdown) prepare_list_svg_symbols() if not port: return app else: handler = app.make_handler() srv = await loop.create_server(handler, '0.0.0.0', port) return srv, app, handler
async def get_storage(): redis = await aioredis.create_pool(("localhost", 6379)) return redis_storage.RedisStorage(redis)