def __init__(self): self.static_config = load_config("./config/config.yaml") self.database_file = "./craftbeerpi.db" logger.info("Init CraftBeerPI") policy = auth.SessionTktAuthentication(urandom(32), 60, include_ip=True) middlewares = [web.normalize_path_middleware(), session_middleware(EncryptedCookieStorage(urandom(32))), auth.auth_middleware(policy), error_middleware] self.app = web.Application(middlewares=middlewares) self._setup_shutdownhook() self.initializer = [] self.bus = CBPiEventBus(self.app.loop, self) self.ws = CBPiWebSocket(self) self.job = JobController(self) self.actor = ActorController(self) self.sensor = SensorController(self) self.plugin = PluginController(self) self.system = SystemController(self) self.config = ConfigController(self) self.kettle = KettleController(self) self.step = StepController(self) self.dashboard = DashboardController(self) self.http_step = StepHttpEndpoints(self) self.http_sensor = SensorHttpEndpoints(self) self.http_config = ConfigHttpEndpoints(self) self.http_actor = ActorHttpEndpoints(self) self.http_kettle = KettleHttpEndpoints(self) self.http_dashboard = DashBoardHttpEndpoints(self) self.notification = NotificationController(self) self.login = Login(self)
def test_merge_slash(self, path, status, cli): extra_middlewares = [ web.normalize_path_middleware(append_slash=False)] client = yield from cli(extra_middlewares) resp = yield from client.get(path) assert resp.status == status
def build_application(loop=None): # language=rst """The entry point of the authorization administration service. :returns int: the exit status of the process. See also the ``console_scripts`` in :file:`setup.py`. """ # Build the application app = web.Application(middlewares=[ aiohttp_extras.middleware, web.normalize_path_middleware(), authorization.middleware ], loop=loop) app['config'] = config.load() app['etag'] = aiohttp_extras.ETagGenerator().update( app['config']['authz_admin']).etag swagger_path = os.path.join(os.path.dirname(__file__), 'openapi.yml') _logger.info("Loading swagger file '%s'", swagger_path) app['swagger'] = swagger_parser.SwaggerParser(swagger_path=swagger_path) app['metadata'] = database.metadata() add_routes(app) app.on_response_prepare.append(on_response_prepare) app.on_startup.append(database.initialize_app) return app
def setup_service(architecture: str, configuration: Configuration) -> web.Application: """ create web application :param architecture: repository architecture :param configuration: configuration instance :return: web application instance """ application = web.Application(logger=logging.getLogger("http")) application.on_shutdown.append(on_shutdown) application.on_startup.append(on_startup) application.middlewares.append( web.normalize_path_middleware(append_slash=False, remove_slash=True)) application.middlewares.append(exception_handler(application.logger)) application.logger.info("setup routes") setup_routes(application) application.logger.info("setup templates") aiohttp_jinja2.setup(application, loader=jinja2.FileSystemLoader( configuration.getpath("web", "templates"))) application.logger.info("setup configuration") application["configuration"] = configuration application.logger.info("setup watcher") application["watcher"] = Watcher(architecture, configuration) return application
def create_app(loop): app = web.Application(middlewares=[web.normalize_path_middleware()]) app.update(name='games_scrapper') app.on_startup.append(setup_routes) return app
def setup_middlewares(): return [ web.normalize_path_middleware( append_slash=False, remove_slash=True, ), ]
def config_and_start_webserver(port): app = web.Application(middlewares=[ web.normalize_path_middleware(append_slash=True, merge_slashes=True), exception_handler_middleware, ]) # Legacy routes app.add_routes([web.get('/data/json/samples', samples_legacy), web.get(r'/data/json/archive/{run}/{path:.+}', archive_legacy), web.get(r'/plotfairy/archive/{run}/{path:.+}', render_legacy), web.get('/plotfairy/overlay', render_overlay_legacy), web.get(r'/jsrootfairy/archive/{run}/{path:.+}', jsroot_legacy),]) # Version 1 API routes app.add_routes([web.get('/api/v1/samples', samples_v1), web.get('/api/v1/layouts', layouts_v1), web.get(r'/api/v1/archive/{run}/{path:.+}', archive_v1), web.get(r'/api/v1/render/{run}/{path:.+}', render_v1), web.get('/api/v1/render_overlay', render_overlay_v1), web.get(r'/api/v1/json/{run}/{path:.+}', jsroot_legacy), web.get('/api/v1/json_overlay', jsroot_overlay), web.get(r'/api/v1/lumis/{run}/{dataset:.+}', available_lumis_v1), web.post('/api/v1/register', register), web.get('/api/v1/datasets', dataset_search), web.get('/api/v1/latest_runs', latest_runs)]) # Routes for HTML files app.add_routes([web.get('/', index), web.static('/', get_absolute_path('../frontend/'), show_index=True)]) app.on_shutdown.append(on_shutdown) web.run_app(app, port=port)
async def test_merge_slash(self, path: Any, status: Any, cli: Any) -> None: extra_middlewares = [web.normalize_path_middleware(append_slash=False)] client = await cli(extra_middlewares) resp = await client.get(path) assert resp.status == status assert resp.url.query == URL(path).query
def __init__(self, bot: Bot, host: str, port: int): self.bot = bot self.app = web.Application( middlewares=[web.normalize_path_middleware()]) self.app.add_routes([web.post('/callback', self.callback)]) cors = aiohttp_cors.setup(self.app, defaults={ "*": aiohttp_cors.ResourceOptions( allow_credentials=True, expose_headers="*", allow_headers="*", ) }) ufw_resource = cors.add(self.app.router.add_resource("/ufw/{wallet}")) cors.add(ufw_resource.add_route("GET", self.ufw)) wfu_resource = cors.add(self.app.router.add_resource("/wfu/{user}")) cors.add(wfu_resource.add_route("GET", self.wfu)) users_resource = cors.add(self.app.router.add_resource("/users")) cors.add(users_resource.add_route("GET", self.users)) active_resource = cors.add( self.app.router.add_resource("/active/{server_id}")) cors.add(active_resource.add_route("GET", self.get_active)) self.logger = logging.getLogger() self.host = host self.port = port self.min_amount = 10 if Env.banano() else 0.1
async def test_append_and_merge_slash(self, path, status, cli): extra_middlewares = [ web.normalize_path_middleware()] client = await cli(extra_middlewares) resp = await client.get(path) assert resp.status == status
async def test_add_trailing_when_necessary( self, path, status, cli): extra_middlewares = [ web.normalize_path_middleware(merge_slashes=False)] client = await cli(extra_middlewares) resp = await client.get(path) assert resp.status == status
def function919(self, arg343, arg529, function231): var3761 = [ web.normalize_path_middleware(append_slash=False, merge_slashes=False) ] var2962 = yield from function231(var3761) var77 = yield from var2962.get(arg343) assert (var77.arg529 == arg529)
def __init__(self, host: str="localhost", port: int=9999, username: Optional[str]=None, password: str=None) -> None: super().__init__(host=host, port=port) self.username = username self.password = password self.network = get_network_type() self.app.middlewares.extend([web.normalize_path_middleware(append_slash=False, remove_slash=True), self.authenticate, self.check_network])
async def test_add_trailing_when_necessary(self, path, status, cli): extra_middlewares = [ web.normalize_path_middleware(merge_slashes=False) ] client = await cli(extra_middlewares) resp = await client.get(path) assert resp.status == status
async def main(): """The main entry point. """ set_time_zone(config.TIME_ZONE) app = web.Application(middlewares=[web.normalize_path_middleware()]) app.add_routes([web.get('/anonymous/', issue_token)]) return app
async def test_merge_slash(self, path, status, cli) -> None: extra_middlewares = [ web.normalize_path_middleware(append_slash=False)] client = await cli(extra_middlewares) resp = await client.get(path) assert resp.status == status assert resp.url.query == URL(path).query
def app_factory(config: Mapping[str, Any]) -> web.Application: app = web.Application( middlewares=[views.error_middleware, web.normalize_path_middleware()]) # load the configuration file app["config"] = config # Add the client session for pooling outgoing connections app.cleanup_ctx.append(client_session) # Connect the database and set up a map of websockets app["db"] = db.Database(config["database_filename"]) # And the routes for the main app app.add_routes(views.routes) # Add the API app app.add_subapp("/api", api.api_app()) # Set up the user session for cookies aiohttp_session.setup( app, EncryptedCookieStorage( base64.urlsafe_b64decode(app["config"]["session_key"])), ) # Set up the templating engine and the static endpoint env = aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader( get_resource_path("templates"))) env.globals.update(jinja2_helpers.GLOBALS) app["static_root_url"] = "/assets" app.router.add_static("/assets", get_resource_path("assets")) # Set up the Spotify app to instigate the OAuth flow app["spotify_app"] = aiohttp_spotify.spotify_app( client_id=config["spotify_client_id"], client_secret=config["spotify_client_secret"], redirect_uri=config["spotify_redirect_uri"], handle_auth=auth.handle_auth, default_redirect=app.router["play"].url_for(), scope=[ "streaming", "user-read-email", "user-read-private", "user-modify-playback-state", "user-read-playback-state", "user-read-currently-playing", ], ) app["spotify_app"]["main_app"] = app app.add_subapp("/spotify", app["spotify_app"]) # Attach the socket.io interface api.sio.attach(app) return app
def get_app(args): """prepare and return aiohttp app instance""" app = web.Application(middlewares=[web.normalize_path_middleware()]) app["settings"] = args app.add_routes([ web.get("/", handle_index_page), web.get(r"/archive/{hash:[\d\w]{4,}}/", archivate), ]) return app
async def test_no_trailing_slash_when_disabled(self, path, status, cli): extra_middlewares = [ web.normalize_path_middleware(append_slash=False, merge_slashes=False) ] client = await cli(extra_middlewares) resp = await client.get(path) assert resp.status == status
def __init__(self): self.path = os.sep.join(os.path.abspath(__file__).split( os.sep)[:-1]) # The path to the package dir self.version = __version__ self.static_config = load_config( os.path.join(".", 'config', "config.yaml")) self.database_file = os.path.join(".", 'config', "craftbeerpi.db") logger.info("Init CraftBeerPI") policy = auth.SessionTktAuthentication(urandom(32), 60, include_ip=True) middlewares = [ web.normalize_path_middleware(), session_middleware(EncryptedCookieStorage(urandom(32))), auth.auth_middleware(policy), error_middleware ] self.app = web.Application(middlewares=middlewares) self.app["cbpi"] = self self._setup_shutdownhook() self.initializer = [] self.bus = CBPiEventBus(self.app.loop, self) self.job = JobController(self) self.config = ConfigController(self) self.ws = CBPiWebSocket(self) self.actor = ActorController(self) self.sensor = SensorController(self) self.plugin = PluginController(self) self.log = LogController(self) self.system = SystemController(self) self.kettle = KettleController(self) self.step: StepController = StepController(self) self.recipe: RecipeController = RecipeController(self) self.notification: NotificationController = NotificationController( self) self.satellite = None if self.static_config.get("mqtt", False) is True: self.satellite: SatelliteController = SatelliteController(self) self.dashboard = DashboardController(self) self.http_step = StepHttpEndpoints(self) self.http_recipe = RecipeHttpEndpoints(self) self.http_sensor = SensorHttpEndpoints(self) self.http_config = ConfigHttpEndpoints(self) self.http_actor = ActorHttpEndpoints(self) self.http_kettle = KettleHttpEndpoints(self) self.http_dashboard = DashBoardHttpEndpoints(self) self.http_plugin = PluginHttpEndpoints(self) self.http_system = SystemHttpEndpoints(self) self.http_log = LogHttpEndpoints(self) self.http_notification = NotificationHttpEndpoints(self) self.login = Login(self)
async def test_no_trailing_slash_when_disabled( self, path, status, cli): extra_middlewares = [ web.normalize_path_middleware( append_slash=False, merge_slashes=False)] client = await cli(extra_middlewares) resp = await client.get(path) assert resp.status == status
async def test_add_trailing_when_necessary( self, path: Any, status: Any, cli: Any ) -> None: extra_middlewares = [web.normalize_path_middleware(merge_slashes=False)] client = await cli(extra_middlewares) resp = await client.get(path) assert resp.status == status assert resp.url.query == URL(path).query
async def get_app(swagger=True, doc="/ui", prefix="", static_path="/static/swagger", base_path="", enable_train=True, enable_predict=True): """Get the main app.""" global APP if APP: return APP APP = web.Application(debug=CONF.debug, client_max_size=CONF.client_max_size) APP.middlewares.append(web.normalize_path_middleware()) model.register_v2_models(APP) v2app = v2.get_app(enable_train=enable_train, enable_predict=enable_predict) APP.add_subapp("/v2", v2app) versions.register_version("stable", v2.get_version) APP.add_routes(versions.routes) LOG.info("Serving loaded V2 models: %s", list(model.V2_MODELS.keys())) if CONF.warm: for _, m in model.V2_MODELS.items(): LOG.debug("Warming models...") await m.warm() if swagger: # init docs with all parameters, usual for ApiSpec aiohttp_apispec.setup_aiohttp_apispec( app=APP, title="DEEP as a Service API endpoint", info={ "description": API_DESCRIPTION, }, externalDocs={ "description": "API documentation", "url": "https://deepaas.readthedocs.org/", }, basePath=base_path, version=deepaas.__version__, url="/swagger.json", swagger_path=doc if doc else None, prefix=prefix, static_path=static_path, in_place=True, ) return APP
async def test_remove_trailing_when_necessary(self, path, status, cli) -> None: extra_middlewares = [ web.normalize_path_middleware( append_slash=False, remove_slash=True, merge_slashes=False)] client = await cli(extra_middlewares) resp = await client.get(path) assert resp.status == status assert resp.url.query == URL(path).query
def create_app() -> web.Application: """Creates application with setup contexts""" app: web.Application = web.Application( middlewares=(web.normalize_path_middleware(remove_slash=True, append_slash=False), ), ) add_routes(app) app.cleanup_ctx.append(setup_rabbit) return app
async def test_remove_and_merge_slash(self, path, status, cli): extra_middlewares = [ web.normalize_path_middleware(append_slash=False, remove_slash=True) ] client = await cli(extra_middlewares) resp = await client.get(path) assert resp.status == status assert resp.url.query == URL(path).query
def init(loop): tracemalloc.start() # cache_manager.Cache() middlewares = [ web.normalize_path_middleware( redirect_class=web.HTTPTemporaryRedirect), json_util.request_parser_middleware, auth.auth_middleware, auth.acl_middleware, app_log.access_log_middleware, ] app = web.Application(loop=loop, middlewares=middlewares) cors = aiohttp_cors.setup(app, defaults={ "*": aiohttp_cors.ResourceOptions( expose_headers='*', allow_headers='*', allow_credentials=True) }) # fernet_key = fernet.Fernet.generate_key() # secret_key = base64.urlsafe_b64decode(fernet_key) # aiohttp_session.setup(app, EncryptedCookieStorage(secret_key)) app.middlewares.append(json_util.response_encoder_middleware) cors.add(app.router.add_get('/', index)) cors.add(app.router.add_post('/rest/send_task/', receive_task)) for route, dimension in zip(('cache', 'tasks', 'checks'), (Cache, Task, Check)): cors.add(app.router.add_post(f'/rest/{route}/', getter(dimension))) cors.add( app.router.add_post('/rest/get_last_checks/', cached_get_last_checks)) cors.add(app.router.add_get('/files/{filename}', get_file)) app.router.add_post('/archive/', get_archive) app.router.add_get('/archive/', get_archive) app.router.add_get('/queue_size/', queue_size) app['running'] = True app['mem_cache'] = TTLDictNew() app['mem_cache_requests'] = weakref.WeakSet() app['refresher'] = aio.aio.ensure_future( cache_manager.Cache().refresher(app)) app['queue'] = aio.aio.Queue(loop=loop) app['queue_processing'] = aio.aio.ensure_future(process_checks(app)) # app['result_cache'] = aio.aio.ensure_future(get_last_checks(app)) app['mem_log'] = aio.aio.ensure_future(memory_log(app)) app.on_startup.append(clear_running_checks) app.on_startup.append(start_workers) app.on_shutdown.append(on_shutdown) return app
def main(): log = logging.getLogger() log.setLevel(logging.INFO) ch_format = ColoredFormatter( "%(asctime)s - %(threadName)s - %(name)s - %(log_color)s %(levelname)s - %(reset)s %(purple)s %(message)s", datefmt=None, reset=True, log_colors={ "DEBUG": "cyan", "INFO": "green", "WARNING": "yellow", "ERROR": "red", "CRITICAL": "red,bg_white", }, secondary_log_colors={}, style="%") ch = logging.StreamHandler(sys.stdout) ch.setFormatter(ch_format) log.addHandler(ch) if not os.path.exists(LOG_FOLDER): try: log.warning("Log folder does not exists. Creating...") os.makedirs(LOG_FOLDER) log.info("Created!") except Exception as err: print("Error happened during log folder creating") print(repr(err)) print("Cannot continue, sorry") sys.exit(1) fh = handlers.RotatingFileHandler(os.path.join(LOG_FOLDER, "adapt-info-back.log"), maxBytes=(1024 * 100), backupCount=10) fh_format = logging.Formatter( "%(asctime)s - [%(threadName)s] - %(name)s - [%(levelname)s] - %(message)s" ) fh.setFormatter(fh_format) log.addHandler(fh) app = web.Application(middlewares=(web.normalize_path_middleware(), cors_factory, auth_middleware)) packages = ["api"] for route in get_routes(packages): app.router.add_route(*route) healthcheck = HealthCheck() app.router.add_get("/api/healthcheck", healthcheck) app.internal_api_token = TOKEN # app.router.add_static("/static", STATIC_PATH, name="static") log.info("server started!") web.run_app(app, host=HOST, port=PORT)
async def get_application(self): app = web.Application(debug=True) app.middlewares.append(web.normalize_path_middleware()) deepaas.model.v2.register_models(app) v2app = v2.get_app() app.add_subapp("/v2", v2app) return app
def __init__(self, host: str, port: int): self.app = web.Application( middlewares=[web.normalize_path_middleware()]) self.app.add_routes([web.post('/', self.gateway)]) self.host = host self.port = port self.websocket = None if config.Config.instance().node_ws_url is not None: self.websocket = WebsocketClient( config.Config.instance().node_ws_url, self.block_arrival_handler)
async def create(): autoload('{{cookiecutter.project_slug}}.views') # ap = argparse.ArgumentParser() # init logging logging.basicConfig(level=logging.DEBUG) # setup application and extensions app = web.Application() app.router.add_routes(routes) app.middlewares.append(web.normalize_path_middleware(append_slash=True)) app.middlewares.append(correlation_id) app.middlewares.append(render_json) return app
def get_app(): app = web.Application(middlewares=[ web.normalize_path_middleware(append_slash=True, merge_slashes=True) ]) app.router.add_get('/', IndexView) app.router.add_get('/containers/', ContainersView) app.router.add_post('/containers/', ContainersView) app.router.add_get('/containers/{name}/', GetContainerView) app.router.add_post('/containers/{name}/start/', StartContainerView) app.router.add_post('/containers/{name}/stop/', StopContainerView) app.router.add_post('/containers/{name}/remove/', RemoveContainerView) return app
def setup_routes(app): resource = app.router.add_resource( '/callback/{UCVpEbs319mxk0sFn_unyVYg}', name='callback') resource.add_route('GET', hub_challenge) resource.add_route('POST', feed_callback) app.router.add_route('POST', '/subscription/', subscribe_via_url) app.middlewares.append(web.normalize_path_middleware( merge_slashes=False, redirect_class=web.HTTPPermanentRedirect)) resource = app.router.add_resource('/subscription/{UCVpEbs319mxk0sFn_unyVYg}') resource.add_route('PUT', subscribe) resource.add_route('DELETE', partial(subscribe, subscribe=False)) return app
async def test_bug_3669(aiohttp_client): async def paymethod(request): return web.Response(text="OK") app = web.Application() app.router.add_route('GET', '/paymethod', paymethod) app.middlewares.append( web.normalize_path_middleware(append_slash=False, remove_slash=True) ) client = await aiohttp_client( app, server_kwargs={'skip_url_asserts': True} ) resp = await client.get('/paymethods') assert resp.status == 404 assert resp.url.path != '/paymethod'
async def test_cannot_remove_and_add_slash(self) -> None: with pytest.raises(AssertionError): web.normalize_path_middleware(append_slash=True, remove_slash=True)