app factory

This commit is contained in:
AF 2021-11-29 13:16:38 +03:00
parent 0af1adc5fe
commit 131896b1c2
3 changed files with 76 additions and 70 deletions

View File

@ -6,13 +6,19 @@ from nacl.signing import VerifyKey
from v6d0auth import certs, cdb
__all__ = ('get_app',)
__all__ = ('V6D0AuthAppFactory',)
from v6d0auth.appfactory import AppFactory
def define_routes(routes: web.RouteTableDef, loop: asyncio.AbstractEventLoop):
class V6D0AuthAppFactory(AppFactory):
def __init__(self, loop: asyncio.AbstractEventLoop):
self.loop = loop
def define_routes(self, routes: web.RouteTableDef):
print(certs.vkey.encode().hex())
mycdb = cdb.CDB(loop)
loop.create_task(mycdb.job())
mycdb = cdb.CDB(self.loop)
self.loop.create_task(mycdb.job())
@routes.get('/')
async def home(_request: web.Request):
@ -68,19 +74,3 @@ def define_routes(routes: web.RouteTableDef, loop: asyncio.AbstractEventLoop):
await ws.send_bytes(cert)
await ws.close()
return ws
def app_routes(loop: asyncio.AbstractEventLoop) -> web.RouteTableDef:
routes = web.RouteTableDef()
define_routes(routes, loop)
return routes
def app_with_routes(routes: web.RouteTableDef):
app = web.Application()
app.add_routes(routes)
return app
def get_app(loop: asyncio.AbstractEventLoop) -> web.Application:
return app_with_routes(app_routes(loop))

16
v6d0auth/appfactory.py Normal file
View File

@ -0,0 +1,16 @@
from aiohttp import web
class AppFactory:
def define_routes(self, routes: web.RouteTableDef) -> None:
raise NotImplementedError
def routes(self) -> web.RouteTableDef:
routes = web.RouteTableDef()
self.define_routes(routes)
return routes
def app(self) -> web.Application:
app = web.Application()
app.add_routes(self.routes())
return app

View File

@ -1,8 +1,8 @@
import asyncio
from v6d0auth.app import get_app
from v6d0auth.app import V6D0AuthAppFactory
from v6d0auth.run_app import run_app
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(run_app(get_app(loop)))
loop.run_until_complete(run_app(V6D0AuthAppFactory(loop).app()))