def routes(self) -> t.Iterable[web.RouteDef]: return [ web.RouteDef(method='POST', path='/', handler=self.write_content, kwargs={}), web.RouteDef(method='GET', path='/', handler=self.read_content, kwargs={}), ]
def __init__(self, google: GoogleClient = None): controller = FacebookController(google=google) self._add( web.RouteDef(method='GET', path='/', handler=controller.get, kwargs={})) pass
def iter_routes(self, prefix: str = "") -> Iterator[web.RouteDef]: prefix = prefix.rstrip("/") for name in dir(self): if name.startswith("_"): continue attr = getattr(self, name) if isinstance(attr, RouteTable): yield from attr.iter_routes( f"{prefix}/{attr.prefix.lstrip('/')}") else: try: info = attr.http_route_info except AttributeError: pass else: yield web.RouteDef( method=info["method"], path=f"{prefix}/{info['path'].lstrip('/')}", handler=attr, kwargs=info["kwargs"], )
def route(method, path, handler, **kwargs): return web.RouteDef(method, path, trio_asyncio.aio2trio(handler), kwargs)
def websocket(path, handler, **kwargs): handler = trio_asyncio.aio2trio(handler) return web.RouteDef(hdrs.METH_GET, path, lambda r:_aio_ws_handler(r, handler), kwargs)
try: await ws.prepare(request) sender = await scheduler.spawn(ws_sender(ws)) async for msg in ws: if msg.type == aiohttp.WSMsgType.TEXT: if msg.data == 'close': await ws.close() else: # MSG handler pass # await ws.send_str(msg.data + '/answer') elif msg.type == aiohttp.WSMsgType.ERROR: print('ws connection closed with exception %s' % ws.exception()) print('websocket connection closed') except CancelledError: logger.info('CancelledError fetched') except Exception: logger.exception('ex') finally: await sender.close() return ws # Registering route dome.routes.append(web.RouteDef('GET', '/ws', websocket_handler, kwargs={}))
def route(self, method, path, handler, **kwargs): self._items.append(web.RouteDef(method, path, handler, kwargs))
import argparse from pathlib import Path from aiohttp import web import stateserver parser = argparse.ArgumentParser() parser.add_argument('-d', '--state-dir', type=Path, default=Path.cwd()) parser.add_argument('-p', '--port', type=int, default=48402) args = parser.parse_args() app = web.Application() app.add_routes([ web.RouteDef(path=f'/states{r.path}', method=r.method, handler=r.handler, kwargs=r.kwargs) for r in stateserver.make_routes(state_dir=args.state_dir) ]) app.add_routes([web.static(prefix='/static', path=Path.cwd() / 'static')]) web.run_app(app, port=args.port)