async def setup_routes(app, handler): h = handler client = h.client routes = [web.get('/', h.home, name='home')] index_all = index_settings['index_all'] index_private = index_settings['index_private'] index_group = index_settings['index_group'] index_channel = index_settings['index_channel'] exclude_chats = index_settings['exclude_chats'] include_chats = index_settings['include_chats'] if index_all: async for chat in client.iter_dialogs(): alias_id = None if chat.id in exclude_chats: continue if chat.is_user: if index_private: alias_id = generate_alias_id(chat) elif chat.is_group: if index_group: alias_id = generate_alias_id(chat) else: if index_channel: alias_id = generate_alias_id(chat) if not alias_id: continue p = f"/{alias_id}" r = [ web.get(p, h.index), web.get(p + r"/logo", h.logo), web.get(p + r"/{id:\d+}/view", h.info), web.get(p + r"/{id:\d+}/download", h.download_get), web.head(p + r"/{id:\d+}/download", h.download_head), web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get), web.head(p + r"/{id:\d+}/thumbnail", h.thumbnail_head), ] routes += r log.debug( f"Index added for {chat.id} :: {chat.title} at /{alias_id}") else: for chat_id in include_chats: chat = await client.get_entity(chat_id) alias_id = generate_alias_id(chat) p = f"/{alias_id}" r = [ web.get(p, h.index), web.get(p + r"/logo", h.logo), web.get(p + r"/{id:\d+}/view", h.info), web.get(p + r"/{id:\d+}/download", h.download_get), web.head(p + r"/{id:\d+}/download", h.download_head), web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get), web.head(p + r"/{id:\d+}/thumbnail", h.thumbnail_head), ] routes += r log.debug( f"Index added for {chat.id} :: {chat.title} at /{alias_id}") app.add_routes(routes)
async def setup_routes(app, handler): h = handler client = h.client index_all = index_settings['index_all'] index_private = index_settings['index_private'] index_group = index_settings['index_group'] index_channel = index_settings['index_channel'] exclude_chats = index_settings['exclude_chats'] include_chats = index_settings['include_chats'] routes = [ web.get('/', h.home) ] if index_all: #print(await client.get_dialogs()) async for chat in client.iter_dialogs(): alias_id = None if chat.id in exclude_chats: continue entity = chat.entity if isinstance(entity, User) and not index_private: print(f'{chat.title}, private: {index_private}') continue elif isinstance(entity, Channel) and not index_channel: print(f'{chat.title}, channel: {index_channel}') continue elif isinstance(entity, Chat) and not index_group: print(f'{chat.title}, group: {index_group}') continue alias_id = h.generate_alias_id(chat) p = "/{chat:" + alias_id + "}" routes.extend([ web.get(p, h.index), web.get(p + r"/logo", h.logo), web.get(p + r"/{id:\d+}/view", h.info), web.get(p + r"/{id:\d+}/download", h.download_get), web.head(p + r"/{id:\d+}/download", h.download_head), web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get), ]) log.debug(f"Index added for {chat.id} at /{alias_id}") else: for chat_id in include_chats: chat = await client.get_entity(chat_id) alias_id = h.generate_alias_id(chat) p = "/{chat:" + alias_id + "}" routes.extend([ web.get(p, h.index), web.get(p + r"/logo", h.logo), web.get(p + r"/{id:\d+}/view", h.info), web.get(p + r"/{id:\d+}/download", h.download_get), web.head(p + r"/{id:\d+}/download", h.download_head), web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get), ]) log.debug(f"Index added for {chat.id} at /{alias_id}") routes.append(web.view(r'/{wildcard:.*}', h.wildcard)) app.add_routes(routes)
async def setup_routes(app, handler): h = handler client = h.client p = r"/{chat:[^/]+}" routes = [ web.get('/', h.home), web.post('/otg', h.dynamic_view), web.get('/otg', h.otg_view), web.get('/pc', h.playlist_creator), web.get(p, h.index), web.get(p + r"/logo", h.logo), web.get(p + r"/{id:\d+}/view", h.info), web.get(p + r"/{id:\d+}/download", h.download_get), web.head(p + r"/{id:\d+}/download", h.download_head), web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get), #below routes work without alias id web.get(r"/{id:\d+}/view", h.info), web.get(r"/{id:\d+}/download", h.download_get), web.head(r"/{id:\d+}/download", h.download_head), web.view(r'/{wildcard:.*}', h.wildcard) ] index_all = index_settings['index_all'] index_private = index_settings['index_private'] index_group = index_settings['index_group'] index_channel = index_settings['index_channel'] exclude_chats = index_settings['exclude_chats'] include_chats = index_settings['include_chats'] if index_all: async for chat in client.iter_dialogs(): alias_id = None if chat.id in exclude_chats: continue if chat.is_user: if index_private: alias_id = generate_alias_id(chat) elif chat.is_channel: if index_channel: alias_id = generate_alias_id(chat) else: if index_group: alias_id = generate_alias_id(chat) if not alias_id: continue log.debug( f"Index added for {chat.id} :: {chat.title} at /{alias_id}") else: for chat_id in include_chats: chat = await client.get_entity(chat_id) alias_id = generate_alias_id(chat) log.debug( f"Index added for {chat.id} :: {chat.title} at /{alias_id}") app.add_routes(routes)
def setup_routes(app, handler): h = handler app.add_routes( [ web.get('/', h.index, name='index'), web.get(r"/{id:\d+}/view", h.info, name='info'), web.get(r"/{id:\d+}/download", h.download_get), web.head(r"/{id:\d+}/download", h.download_head), web.get(r"/{id:\d+}/thumbnail", h.thumbnail_get), web.head(r"/{id:\d+}/thumbnail", h.thumbnail_head), ] )
def _create_api(self, spec) -> None: resource = Resource(spec, self.injector) self.add_routes([ web.get('/', RootResource(spec).get), web.get('/{resource}/{id}', resource.get, allow_head=False), web.get('/{resource}', resource.get, allow_head=False), web.head('/{resource}/{id}', resource.head), web.head('/{resource}', resource.head), web.patch('/{resource}/{id}', resource.patch), web.patch('/{resource}', resource.patch), ])
def setup_routes(app, handler): h = handler routes = [web.get('/', h.home, name='home')] for chat_id in chat_ids: p = f"/{chat_id}" r = [ web.get(p, h.index), web.get(p + r"/{id:\d+}/view", h.info), web.get(p + r"/{id:\d+}/download", h.download_get), web.head(p + r"/{id:\d+}/download", h.download_head), web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get), web.head(p + r"/{id:\d+}/thumbnail", h.thumbnail_head), ] routes += r app.add_routes(routes)
async def start(self): await self.app.certificate.public_cert_bytes() ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) ssl_context.load_cert_chain( certfile=os.path.join(self.app.base, "server.crt"), keyfile=os.path.join(self.app.base, "server.key"), ) for f in self.app.friend_list.friends(): ssl_context.load_verify_locations(cadata=f.cert) ssl_context.check_hostname = False ssl_context.verify_mode = ssl.CERT_REQUIRED self.web_app = web.Application() self.web_app.add_routes([ web.head("/", self.handle_head), web.post("/", self.handle_post), web.get("/f/{file_id}", self.handle_file), ]) self.runner = web.AppRunner(self.web_app) await self.runner.setup() self.site = web.TCPSite( self.runner, "0.0.0.0", await self.app.identity.port(), ssl_context=ssl_context, ) await self.site.start()
def main(): """ Main method for the Talos webserver. Sets up and runs the webserver :return: Exit code """ settings = load_settings() if settings["tokens"].get("ssl_cert"): sslcontext = ssl.SSLContext(ssl.PROTOCOL_SSLv23) cert = pathlib.Path(__file__).parent / settings["tokens"]["ssl_cert"] key = pathlib.Path(__file__).parent / settings["tokens"]["ssl_key"] sslcontext.load_cert_chain(cert, key) else: sslcontext = None app = TalosApplication() app.apply_settings(settings) site_handler = handlers.SiteHandler(app=app) auth_handler = handlers.AuthHandler(app=app) api_handler = handlers.APIHandler(app=app) app.add_routes([ web.get("/{tail:(?!api/|auth/).*}", site_handler.get), web.head("/{tail:(?!api/|auth/).*}", site_handler.head), web.get("/api/{tail:.*}", api_handler.get), web.post("/api/{tail:.*}", api_handler.post), web.get("/auth/{tail:.*}", auth_handler.get) ]) web.run_app(app, port=443 if sslcontext else 80, ssl_context=sslcontext) return 0
def test_head(router: Any) -> None: async def handler(request): pass router.add_routes([web.head("/", handler)]) assert len(router.routes()) == 1 route = list(router.routes())[0] assert route.handler is handler assert route.method == "HEAD" assert str(route.url_for()) == "/"
def test_head(router): async def handler(request): pass router.add_routes([web.head('/', handler)]) assert len(router.routes()) == 1 route = list(router.routes())[0] assert route.handler is handler assert route.method == 'HEAD' assert str(route.url_for()) == '/'
def main(): """ Main method for server redirector """ app = web.Application() handler = HTTPSRedirecter() app.add_routes([ web.get("/{tail:.*}", handler.all), web.post("/{tail:.*}", handler.all), web.head("/{tail:.*}", handler.all) ]) web.run_app(app, port=80)
def main(): """ Main method for server redirector """ app = web.Application() handler = HTTPSRedirecter() app.add_routes([ web.get("/{tail:.*}", handler.all), web.post("/{tail:.*}", handler.all), web.head("/{tail:.*}", handler.all) ]) web.run_app(app, port=80)
def setup_routes(app, handler): h = handler routes = [ web.get('/', h.home, name='home') ] for chat_id in chat_ids: while True: alias_id = ''.join([random.choice(string.ascii_letters + string.digits) for _ in range(len(str(chat_id)))]) if alias_id in alias_ids: continue alias_ids.append(alias_id) break p = f"/{alias_id}" r = [ web.get(p, h.index), web.get(p + r"/logo", h.logo), web.get(p + r"/{id:\d+}/view", h.info), web.get(p + r"/{id:\d+}/download", h.download_get), web.head(p + r"/{id:\d+}/download", h.download_head), web.get(p + r"/{id:\d+}/thumbnail", h.thumbnail_get), web.head(p + r"/{id:\d+}/thumbnail", h.thumbnail_head), ] routes += r app.add_routes(routes)
def setUp(self): self.DATA_PATH = './tests/data' self.HOST = '127.0.0.1' self.PORT = 8000 self.SERVER_ADDRESS = (self.HOST, self.PORT) self.app = web.Application() self.app.add_routes([ web.get('/{tail:.*}', handle_factory(self.DATA_PATH)), web.post('/{tail:.*}', handle_factory(self.DATA_PATH)), web.put('/{tail:.*}', handle_factory(self.DATA_PATH)), web.patch('/{tail:.*}', handle_factory(self.DATA_PATH)), web.head('/{tail:.*}', handle_factory(self.DATA_PATH)), web.delete('/{tail:.*}', handle_factory(self.DATA_PATH)), web.options('/{tail:.*}', handle_factory(self.DATA_PATH)), ]) self.test_server = TestServer(self.app, host=self.HOST, port=self.PORT)
async def test_http_head_200(self): async def handle_head(request): await request.content.read() return web.Response(status=200, headers={'content-length': '20'}) app = web.Application() app.add_routes([web.head('/page', handle_head)]) runner = web.AppRunner(app) await runner.setup() self.add_async_cleanup(runner.cleanup) site = web.TCPSite(runner, '0.0.0.0', 8080) await site.start() request, close = Pool() self.add_async_cleanup(close) code, _, body = await request(b'HEAD', 'http://localhost:8080/page') await buffered(body) self.assertEqual(code, b'200')
def main(): parser = argparse.ArgumentParser(description='HTTP data mocker') parser.add_argument('data_path', help='The data folder') parser.add_argument('-p', '--port', help='The TCP port', default=TCP_PORT, type=int) parser.add_argument('--host', help='The host', default=HOST) parser.add_argument('-l', '--log-file', help='save log messages in a given file') parser.add_argument('-v', '--verbose', action='store_true', default=False, help='print more detailed log messages') parser.add_argument( '--version', action='version', version=f'Mocker version {pkg_resources.require("mocker")[0].version}') args = parser.parse_args() logging_level = logging.DEBUG if args.verbose else logging.INFO logging.basicConfig(filename=args.log_file, level=logging_level) if os.path.exists(args.data_path): app = web.Application() app.add_routes([ web.get('/{tail:.*}', handle_factory(args.data_path)), web.post('/{tail:.*}', handle_factory(args.data_path)), web.put('/{tail:.*}', handle_factory(args.data_path)), web.patch('/{tail:.*}', handle_factory(args.data_path)), web.head('/{tail:.*}', handle_factory(args.data_path)), web.delete('/{tail:.*}', handle_factory(args.data_path)), web.options('/{tail:.*}', handle_factory(args.data_path)), ]) try: web.run_app(app, host=args.host, port=args.port) except KeyboardInterrupt: print('Exiting Mocker...') else: print('Folder {} not found'.format(args.data_path))
def get_common_routes(alias_id): p = "/{chat:" + alias_id + "}" return [ web.get(p, h.index, name=f"index_{alias_id}"), web.get(p + r"/logo", h.logo, name=f"logo_{alias_id}"), web.get(p + r"/{id:\d+}/view", h.info, name=f"info_{alias_id}"), web.get( p + r"/{id:\d+}/thumbnail", h.thumbnail_get, name=f"thumbnail_get_{alias_id}", ), web.get( p + r"/{id:\d+}/{filename}", h.download_get, name=f"download_get_{alias_id}", ), web.head( p + r"/{id:\d+}/{filename}", h.download_head, name=f"download_head_{alias_id}", ), ]
def get_common_routes(handler: Views, alias_id: str) -> List[RouteDef]: p = "/{chat:" + alias_id + "}" return [ web.get(p, handler.index, name=f"index_{alias_id}"), web.get(p + r"/logo", handler.logo, name=f"logo_{alias_id}"), web.get(p + r"/{id:\d+}/view", handler.info, name=f"info_{alias_id}"), web.get( p + r"/{id:\d+}/thumbnail", handler.thumbnail_get, name=f"thumbnail_get_{alias_id}", ), web.get( p + r"/{id:\d+}/{filename}", handler.download_get, name=f"download_get_{alias_id}", ), web.head( p + r"/{id:\d+}/{filename}", handler.download_head, name=f"download_head_{alias_id}", ), ]
requests_cache.cache_response(res_cache_tuple, request.rel_url) return response else: return web.Response(text="failed") app = web.Application() app.add_routes([ web.get('/{controller}/{tail:.*}', handle), web.get('/{controller}', handle), web.get('/', handle), web.post('/{controller}/{tail:.*}', handle), web.post('/{controller}', handle), web.post('/', handle), web.delete('/{controller}/{tail:.*}', handle), web.delete('/{controller}', handle), web.delete('/', handle), web.put('/{controller}/{tail:.*}', handle), web.put('/{controller}', handle), web.put('/', handle), web.patch('/{controller}/{tail:.*}', handle), web.patch('/{controller}', handle), web.patch('/', handle), web.head('/{controller}/{tail:.*}', handle), web.head('/{controller}', handle), web.head('/', handle) ]) if __name__ == '__main__': web.run_app(app)
app.add_routes( [web.get("/library/books/{book_id}/pages/{page_id}", getBookSinglePage)]) app.add_routes([web.get("/library/books/{book_id}/authors/", getBookAuthors)]) app.add_routes([web.get("/library/authors/", getAuthors)]) # All POST app.add_routes([web.post("/library/books/", addBook)]) # All PUT app.add_routes([web.put("/library/books/{book_id}", updateBook)]) # All PATCH app.add_routes([ web.patch("/library/books/{book_id}/pages/{page_id}", updateBookSinglePage) ]) # All DELETE app.add_routes([web.delete("/library/books/{book_id}", deleteBook)]) # All HEAD app.add_routes( [web.head("/library/books/{book_id}/pages/", getBookAllPagesInfo)]) if __name__ == '__main__': x = DB() x.create_connection() x.create_db() x.create_value() x.select_all_book() web.run_app(app)
async def run_application(): logger = get_root_logger('elasticsearch-proxy') with logged(logger, 'Examining environment', []): env = normalise_environment(os.environ) port = env['PORT'] ip_whitelist = env['INCOMING_IP_WHITELIST'] staff_sso_client_base = env['STAFF_SSO_BASE'] staff_sso_client_id = env['STAFF_SSO_CLIENT_ID'] staff_sso_client_secret = env['STAFF_SSO_CLIENT_SECRET'] kibana_url_no_password = URL('http://127.0.0.1:5601') vcap_services = json.loads(env['VCAP_SERVICES']) es_uri = vcap_services['elasticsearch'][0]['credentials']['uri'] redis_uri = vcap_services['redis'][0]['credentials']['uri'] es_parsed = URL(es_uri) es_user = es_parsed.user es_password = es_parsed.password kibana_url = kibana_url_no_password.with_user(es_user).with_password( es_password) client_session = aiohttp.ClientSession( skip_auto_headers=['Accept-Encoding']) async def handle(request): url = kibana_url.with_path(request.url.path) request_body = await request.read() headers = { header: request.headers[header] for header in ['Kbn-Xsrf', 'Kbn-Version', 'Content-Type'] if header in request.headers } with logged( request['logger'], 'Elasticsearch request by (%s) to (%s) (%s) (%s) (%s)', [ request['me_profile']['email'], request.method, str(url), request.url.query, request_body, ], ): async with client_session.request( request.method, str(url), params=request.url.query, data=request_body, headers=headers, ) as response: response_body = await response.read() response_headers = { key: value for key, value in response.headers.items() if key != 'Transfer-Encoding' } return web.Response(status=response.status, body=response_body, headers=response_headers) redis_pool = await aioredis.create_pool(redis_uri) redis_storage = RedisStorage(redis_pool, max_age=60 * 60 * 24) with logged(logger, 'Creating listening web application', []): app = web.Application(middlewares=[ server_logger(logger), authenticate_by_ip(INCORRECT, ip_whitelist), session_middleware(redis_storage), authenticate_by_staff_sso(client_session, staff_sso_client_base, staff_sso_client_id, staff_sso_client_secret), ]) app.add_routes([ web.delete(r'/{path:.*}', handle), web.get(r'/{path:.*}', handle), web.post(r'/{path:.*}', handle), web.put(r'/{path:.*}', handle), web.head(r'/{path:.*}', handle), ]) class NullAccessLogger(aiohttp.abc.AbstractAccessLogger): # pylint: disable=too-few-public-methods def log(self, request, response, time): pass runner = web.AppRunner(app, access_log_class=NullAccessLogger) await runner.setup() site = web.TCPSite(runner, '0.0.0.0', port) await site.start()