async def get_app(conf): db = await get_db(conf) model = Model(db, conf) await model.create_indexes() app = web.Application() async def close_mongo(app): db.client.close() app.on_cleanup.append(close_mongo) session_setup(app, MongoStorage(db['sessions'], max_age=3600 * 24 * 365 * 10)) app['conf'] = conf app['courses'] = load_courses(conf.courses_file) app['model'] = model app.add_routes(all_routes) GraphQLView.attach(app, route_path='/api/graphql', schema=Schema, graphiql=True, enable_async=True) #executor=GQLAIOExecutor()) app.router.add_get('/api/subscriptions', subscriptions) return app
async def make_app(): parser = argparse.ArgumentParser( description='Python CAS Gateway', formatter_class=argparse.ArgumentDefaultsHelpFormatter ) parser.add_argument('--redis-url', help='Redis URL', default='redis://127.0.0.1:6379/0') parser.add_argument('--timeout', type=int, help='Request timeout', default=10) parser.add_argument('--bind-host', help='Address to bind to', default='0.0.0.0') parser.add_argument('--bind-port', type=int, help='Port to bind to', default='8000') parser.add_argument('--cas-version', type=int, help='CAS version in use', default=3) parser.add_argument('--chunk-size', type=int, help='Chunk size for streaming responses back to the client', default=1024*4) parser.add_argument('--require-attribute', help='Require the given CAS attribute to be present and ' 'not set to an empty string. Requires CAS version 3.') required_named = parser.add_argument_group('required named arguments') required_named.add_argument('--backend-url', help='URL to the backend to be proxied (e.g. http://myhost.com:8888)', required=True) required_named.add_argument('--cas-url', help='URL to the CAS server', required=True) args = parser.parse_args() app = web.Application() print("Configuring...") app.settings = type('Settings', (object,), dict( backend_url=URL(args.backend_url), cas_url=URL(args.cas_url), redis_url=URL(args.redis_url), timeout=args.timeout, bind_host=args.bind_host, bind_port=args.bind_port, # aiohttp_cas was this as a string cas_version=str(args.cas_version), require_attribute=args.require_attribute, ))() print("Connecting to redis...") pool = await create_pool( (app.settings.redis_url.host or '127.0.0.1', app.settings.redis_url.port or 6379), db=int(app.settings.redis_url.path.strip('/') or 0), create_connection_timeout=10, password=app.settings.redis_url.password, ) await remotes_setup(app, XForwardedRelaxed()) session_setup(app, RedisStorage(pool)) u = app.settings.cas_url cas_setup( app=app, host='{}:{}'.format(u.host, u.port) if u.port != 80 else u.host, host_prefix=app.settings.cas_url.path, version=app.settings.cas_version, host_scheme=app.settings.cas_url.scheme ) app.router.add_route('GET', '/cas-gateway-ready', ready_check) app.router.add_route('*', '/{tail:.*}', handle) return app
def app_factory() -> web.Application: app = web.Application() jinja2_setup(app, loader=jinja2.FileSystemLoader( [Path(__file__).parent / "templates"])) session_setup(app, SimpleCookieStorage()) app.add_subapp( "/auth/dataporten/", dataporten( FEIDE_CLIENT_ID, FEIDE_CLIENT_SECRET, on_login=on_dataporten_login, scopes=[ 'profile', 'userid', 'openid', 'groups', 'peoplesearch', 'email', 'userid-feide' ], json_data=False, ), ) app.add_routes([web.get("/", index), web.get("/auth/logout", logout)]) return app
def main(args=None): main_dir = Path(__file__).parent.parent.resolve() with open(main_dir / 'logger.yaml', 'r') as stream: dictConfig(yaml.load(stream)) host = '0.0.0.0' port = 8000 sslcontext = None loop = asyncio.get_event_loop() loop.set_debug(True) server = web.Application() server.router.add_get('/', urls.index, name='index') server.router.add_get('/login', urls.login, name='login') #server.router.add_post('/query' , urls.query, name='query') # Where the templates are template_loader = jinja2.FileSystemLoader(str(main_dir / 'templates')) aiohttp_jinja2.setup(server, loader=template_loader) # Session middleware fernet_key = fernet.Fernet.generate_key() secret_key = base64.urlsafe_b64decode( fernet_key) # 32 url-safe base64-encoded bytes session_setup(server, EncryptedCookieStorage(secret_key)) # ...and cue music LOG.info(f"Start BeaconUI server on {host}:{port}") web.run_app(server, host=host, port=port, shutdown_timeout=0, ssl_context=sslcontext)
def add_routes(app, secret_key): handle_html.timeout = app["ini"].getint("sessions", "session_idle_timeout") cache_page.timeout = app["ini"].getint("sessions", "cached_page_timeout") #app.router.add_route('POST', '/pv/v1/', handle_v1) app.router.add_get('/', GET_frontpage) app.router.add_get('/index', GET_index) app.router.add_get('/login', GET_login) app.router.add_post('/login', POST_login) app.router.add_get('/settings', GET_settings) app.router.add_post('/settings', POST_settings) app.router.add_get('/mapmaker', GET_mapmaker) app.router.add_get('/map', GET_map) app.router.add_get('/admin', GET_admin) app.router.add_post('/admin', POST_admin) app.router.add_get('/report', GET_report) app.router.add_post('/report', POST_report) app.router.add_get('/mazetest', GET_test) app.router.add_static('/static/', path='static', name='static') session_setup(app, EncryptedCookieStorage(secret_key))
def create_app(): app = web.Application() app.router.add_get('/', index) app.router.add_post('/start-job/', start_job) app['downloader'] = Downloader(redis_settings=redis_settings) app.on_shutdown.append(shutdown) session_setup(app, SimpleCookieStorage()) return app
async def init() -> web.Application: """Initialise web server.""" LOG.info("Initialise web server.") # Initialise server object server = web.Application() # Create encrypted session storage # Encryption key must be 32 len bytes session_setup(server, EncryptedCookieStorage(CONFIG.app["session_key"].encode())) # Gather endpoints server.router.add_routes(routes) return server
async def make_app(loop=None): logger.debug("create app start") loop = loop or asyncio.get_event_loop() app = web.Application(loop=loop, middlewares=[ middleware_404, ]) app.session_pool = await aioredis.create_pool( **cfg['http']['session']["link"]) redis_storage = RedisStorage(app.session_pool, max_age=cfg["http"]["session"]["max_age"] * 24 * 3600) session_setup(app, redis_storage) router.setup(app) logger.debug("crate app success") return app
async def get_app(conf): mongo_client = AsyncIOMotorClient(conf.mongodb.connection_uri) mongo_db = mongo_client[conf.mongodb.db_name] model = Model(mongo_db, conf) await model.create_indexes() # We use hash function because EncryptedCookieStorage needs # a byte string of specific size. session_secret_hash = sha256(conf.session_secret.encode()).digest() app = web.Application() session_setup(app, EncryptedCookieStorage(session_secret_hash)) app['conf'] = conf app['courses'] = load_courses(conf.data_dir) app['model'] = model app.add_routes(all_routes) return app
def setup(app): # Session middleware fernet_key = fernet.Fernet.generate_key() secret_key = base64.urlsafe_b64decode(fernet_key) # 32 url-safe base64-encoded bytes storage = EncryptedCookieStorage(secret_key, cookie_name=SESSION_STORAGE) session_setup(app, storage) # # CSRF middleware # aiohttp_csrf.setup(app, # policy=aiohttp_csrf.policy.FormPolicy(CSRF_FIELD_NAME), # storage=aiohttp_csrf.storage.SessionStorage(SESSION_STORAGE)) # app.middlewares.append(aiohttp_csrf.csrf_middleware) # Capture 404 and 500 app.middlewares.append(error_middleware)
async def get_app(conf): mongo_client = AsyncIOMotorClient(conf.mongodb.connection_uri) mongo_db = mongo_client[conf.mongodb.db_name] model = Model(mongo_db, conf) await model.create_indexes() app = web.Application() async def close_mongo(app): mongo_client.close() app.on_cleanup.append(close_mongo) session_setup(app, MongoStorage(mongo_db['sessions'], max_age=3600*24*90)) app['conf'] = conf app['courses'] = load_courses(conf.courses_file) app['model'] = model app.add_routes(all_routes) return app
def app_factory() -> web.Application: app = web.Application() jinja2_setup( app, loader=jinja2.FileSystemLoader([Path(__file__).parent / "templates"]) ) session_setup(app, SimpleCookieStorage()) app.add_subapp( "/auth/github/", github( "a1b8c7904865ac38baba", "147d82d8ded7a74899fcc4da2b61f8d305bf81c5", on_login=on_github_login, ), ) app.add_routes([web.get("/", index), web.get("/auth/logout", logout)]) return app
def __init__(self, paths: dict): # Rutas de la aplicación self.paths = paths self.db_config = dict() with open(self.paths['config'].joinpath('db.json')) as db_cf: self.db_config = json.load(db_cf) self._router = Router() # Rutas a archivos estáticos self._router.add_static('/static', self.paths['static']) # Fernet key para encriptar las cookies de las sesiones self.fernet_key = fernet.Fernet.generate_key() self.secret_key = urlsafe_b64decode(self.fernet_key) # Interfaz de la base de datos self.db = Connection(self.db_config['host'], self.db_config['port'], self.db_config['user'], self.db_config['password'], self.db_config['database'], self.db_config['schema']) self.mvc = Mvc(self, self._router, self.db, path=self.paths['app']) super().__init__(router=self._router) # Inicializar el gestor de templates Jinja2 jinja2_setup( self, loader=FileSystemLoader( str(self.paths['resources'].joinpath('views').resolve()))) # Inicializar sesiones session_setup( self, EncryptedCookieStorage(self.secret_key, cookie_name='CAJAMARQUESO_SRL'))
def app_factory(client_id: str, client_secret: str) -> web.Application: fernet_key = cryptography.fernet.Fernet.generate_key() secret_key = base64.urlsafe_b64decode(fernet_key) app = web.Application() # session_setup(app, SimpleCookieStorage()) # used for testing purpose session_setup(app, EncryptedCookieStorage(secret_key)) jinja2_setup(app, loader=jinja2.FileSystemLoader( [Path(__file__).parent / "templates"])) app.add_subapp( "/google/", oauth2_app( # ..., client_id=client_id, client_secret=client_secret, authorize_url="https://accounts.google.com/o/oauth2/v2/auth", token_url="https://www.googleapis.com/oauth2/v4/token", on_login=on_google_login, on_error=on_google_error, scopes=['email', 'profile', 'openid'])) app.add_routes([web.get("/", index), web.get("/auth/logout", logout)]) return app
status=Status.dnd, commandline_args=args, case_insensitive=True, shard_count=args.shardcount, shard_ids=shard_ids, shard_id=args.min, ) # Create website object - don't start based on argv app = Application(loop=bot.loop, debug=True) app.add_routes(api_routes) app.router.add_static('/static', getcwd() + '/website/static') app['bot'] = bot app['static_root_url'] = '/static' jinja_setup(app, loader=FileSystemLoader(getcwd() + '/website/templates')) session_setup(app, ECS(token_bytes(32))) if __name__ == '__main__': ''' Starts the bot (and webserver if specified) and runs forever ''' loop = bot.loop loop.set_debug(True) logger.info("Creating database pool") loop.run_until_complete( DatabaseConnection.create_pool(bot.config['database'])) logger.info('Loading extensions... ') bot.load_all_extensions()
f'{request.remote} ' f'"{request.method} {request.rel_url}" ' f'done in {time}s: {response.status} ' f'- "{request.headers.get("User-Agent")}"') if __name__ == '__main__': app = web.Application() loop = asyncio.get_event_loop() if cfg.get('proxy', {}).get('enabled', False): loop.run_until_complete( remotes_setup( app, XForwardedStrict([cfg.get('proxy', {}).get('trusted')]))) # generate key secret_key = urlsafe_b64decode(Fernet.generate_key()) session_setup(app, EncryptedCookieStorage(secret_key)) aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader( str( path.join(path.dirname(__file__), 'res/templates/')))) dc = cfg["discord"] auth_app = oauth2_app( client_id=dc['client_id'], client_secret=dc['client_secret'], authorize_url="https://discordapp.com/api/oauth2/authorize", token_url="https://discordapp.com/api/oauth2/token", scopes=["identify"], json_data=False, on_login=handle_on_login) app.add_subapp("/login/discord/", auth_app)
def launch_explorer(): global app parser = argparse.ArgumentParser(prog="nulsexplorer", description="Launches a Nuls Explorer") parser.add_argument('-c', '--config', action="store", dest="config_file") parser.add_argument('-p', '--port', action="store", type=int, dest="port", default=8080) parser.add_argument('--host', action="store", type=str, dest="host", default="127.0.0.1") parser.add_argument('--debug', action="store_true", dest="debug", default=False) args = parser.parse_args() config = Config(schema=get_defaults()) app['config'] = config app.config = config if args.config_file is not None: app['config'].yaml.load(args.config_file) from nulsexplorer.web.controllers.errors import setup_middlewares setup_middlewares(app) init_cors() log_level = logging.INFO if args.debug: log_level = logging.DEBUG logging.basicConfig( level=log_level, format="%(asctime)s [%(levelname)s] %(name)s: %(message)s") LOGGER.info("Starting up.") app['config'].explorer.port.value = args.port app['config'].explorer.host.value = args.host model.init_db(ensure_indexes=(not args.debug)) LOGGER.info("Database initialized.") # session_opts = { # 'session.cookie_expires': True, # 'session.encrypt_key': app['config']['fleeter'].get('secret', "CHOOSE A SECRET DAMNIT"), # 'session.httponly': True, # 'session.timeout': 3600 * 24, # 1 day # 'session.type': 'cookie', # 'session.validate_key': True, # } host = app['config'].explorer.host.value port = app['config'].explorer.port.value session_secret = app['config'].explorer.secret.get( "CHOOSE A SECRET DAMNIT") session_secret = '{0:<32.32}'.format(session_secret) session_setup(app, EncryptedCookieStorage(session_secret.encode('utf-8'))) #app.middlewares.append(auth_middleware_factory) #app = SessionMiddleware(app, session_opts) #prepare_server() start_connector() start_jobs() #if args.debug: # debug(True) LOGGER.info("Starting main loop.") loop = asyncio.get_event_loop() handler = app.make_handler() f = loop.create_server(handler, host, port) srv = loop.run_until_complete(f) LOGGER.info('serving on %s', srv.sockets[0].getsockname()) loop.run_forever()
def get_app(cluster_manager, config): templates_paths = [str(Path(__file__).parent / "templates")] if config.templates_path: # prepend the custom template path so custom templates will overwrite any default ones templates_paths.insert(0, config.templates_path) static_assets_path = Path(__file__).parent / "templates" / "assets" if config.static_assets_path: # overwrite assets path static_assets_path = Path(config.static_assets_path) object_links = collections.defaultdict(list) if config.object_links: for link_def in config.object_links.split(","): resource_type, sep, url_template = link_def.partition("=") object_links[resource_type].append({ "href": url_template, "title": "External link for object {name}", "icon": "external-link-alt", }) label_links = collections.defaultdict(list) if config.label_links: for link_def in config.label_links.split(","): label, sep, url_template = link_def.partition("=") label_links[label].append({ "href": url_template, "title": "External link for {label} label with value '{label_value}'", "icon": "external-link-alt", }) app = web.Application() aiohttp_jinja2.setup( app, loader=jinja2.FileSystemLoader(templates_paths), trim_blocks=True, lstrip_blocks=True, ) env = aiohttp_jinja2.get_env(app) env.filters.update( pluralize=jinja2_filters.pluralize, yaml=jinja2_filters.yaml, highlight=jinja2_filters.highlight, age_color=jinja2_filters.age_color, cpu=jinja2_filters.cpu, memory=jinja2_filters.memory, ) env.globals["version"] = __version__ env.globals["object_links"] = object_links env.globals["label_links"] = label_links app.add_routes(routes) app.router.add_static("/assets", static_assets_path) # behind proxy app.middlewares.append(XForwardedRelaxed().middleware) secret_key = os.getenv("SESSION_SECRET_KEY") or Fernet.generate_key() secret_key = base64.urlsafe_b64decode(secret_key) session_setup( app, EncryptedCookieStorage(secret_key, cookie_name="KUBE_WEB_VIEW")) authorize_url = os.getenv("OAUTH2_AUTHORIZE_URL") access_token_url = os.getenv("OAUTH2_ACCESS_TOKEN_URL") if authorize_url and access_token_url: logger.info( f"Using OAuth2 middleware with authorization endpoint {authorize_url}" ) app.middlewares.append(auth) app.middlewares.append(error_handler) app[CLUSTER_MANAGER] = cluster_manager app[CONFIG] = config return app
logger.info("Creating bot") loop.run_until_complete(app['bot'].login(app['config']['token'])) logger.info("Creating database pool") loop.run_until_complete( DatabaseConnection.create_pool(app['config']['database'])) # Connect the redis logger.info("Creating redis pool") loop.run_until_complete(RedisConnection.create_pool( app['config']['redis'])) # Connect redis to middleware logger.info("Connecting Redis to app") storage = RedisStorage(RedisConnection.pool) session_setup(app, storage) # Start the server unless I said otherwise webserver = None ssl_webserver = None # HTTP server logger.info("Creating webserver...") application = AppRunner(app) loop.run_until_complete(application.setup()) webserver = TCPSite(application, host=args.host, port=args.port) # SSL server try: if not args.nossl: ssl_context = SSLContext()
def setup_session(app, SECRET_KEY): cookie_storage = EncryptedCookieStorage( hashlib.sha256(bytes(SECRET_KEY, 'utf-8')).digest()) session_setup(app, cookie_storage)
if __name__ == "__main__": path = op.dirname(op.abspath(__file__)) config = read_configuration_file(path) lang = config["recorder"].get("language", DEFAULT_LANGUAGE) set_language(lang) setup_i18n(path, lang) app = web.Application(middlewares=[error_middleware, babel_middleware]) app.recorder = Recordings(config["recorder"], path) app.wakeup = Awakenings(config["wakeup"], path) session_setup(app, SimpleCookieStorage()) app.middlewares.append(aiohttp_session_flash.middleware) template_dir = op.join(path, "templates") aiohttp_jinja2.setup( app, loader=FileSystemLoader(template_dir), context_processors=(aiohttp_session_flash.context_processor, )) jinja2_env = aiohttp_jinja2.get_env(app) jinja2_env.globals['_'] = _ app.router.add_get("/recording/cancel/{id:\d+}/", cancel_recording, name="cancel_recording") app.router.add_get("/wakeup/cancel/{id:\d+}/", cancel_awakening,
async def setup(app, config): session_setup(app, EncryptedCookieStorage(config['secret'].encode('utf-8')))
parser.add_argument("config_file", help="The configuration for the bot.") parser.add_argument("gold_config_file", help="The configuration for the Gold version of the bot.") parser.add_argument("--host", type=str, default='0.0.0.0', help="The host IP to run the webserver on.") 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) with open(args.gold_config_file) as a: gold_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_static('/static', os.getcwd() + '/website/static', append_version=True) # Add our connections and their loggers app['database'] = utils.DatabaseConnection utils.DatabaseConnection.logger = logger.getChild("db") utils.DatabaseConnection.logger.setLevel(logging.DEBUG) app['redis'] = utils.RedisConnection utils.RedisConnection.logger = logger.getChild("redis") utils.RedisConnection.logger.setLevel(logging.DEBUG) app['logger'] = logger logger.setLevel(logging.DEBUG)
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 async def on_ready(): """Async setup function for the bot""" if hasattr(bot, "ready_time"): return bot.ready_time = dt.utcnow()
def _initialize_session(self): fernet_key = fernet.Fernet.generate_key() secret_key = base64.urlsafe_b64decode(fernet_key) session_setup(self.app, EncryptedCookieStorage(secret_key))
default='0.0.0.0', help="The host IP to run the webserver on.") 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
return response app = web.Application() aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader( str(Path(__file__).parent / "templates"))) env = aiohttp_jinja2.get_env(app) env.filters.update(yaml=filter_yaml, highlight=filter_highlight) env.globals["version"] = __version__ app.add_routes(routes) app.router.add_static("/assets", Path(__file__).parent / "templates" / "assets") # behind proxy app.middlewares.append(XForwardedRelaxed().middleware) secret_key = os.getenv("SESSION_SECRET_KEY") or Fernet.generate_key() secret_key = base64.urlsafe_b64decode(secret_key) session_setup(app, EncryptedCookieStorage(secret_key, cookie_name="KUBE_WEB_VIEW")) authorize_url = os.getenv("OAUTH2_AUTHORIZE_URL") access_token_url = os.getenv("OAUTH2_ACCESS_TOKEN_URL") if authorize_url and access_token_url: app.middlewares.append(auth) web.run_app(app)
def setup_session(app): # use the same session key for all instances of the application # as gunicorn can create more than one fernet_key = app["config"]["application"]["session_secret_key"] secret_key = base64.urlsafe_b64decode(fernet_key) session_setup(app, EncryptedCookieStorage(secret_key))