app factory
This commit is contained in:
parent
0af1adc5fe
commit
131896b1c2
126
v6d0auth/app.py
126
v6d0auth/app.py
@ -6,81 +6,71 @@ 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):
|
||||
print(certs.vkey.encode().hex())
|
||||
mycdb = cdb.CDB(loop)
|
||||
loop.create_task(mycdb.job())
|
||||
class V6D0AuthAppFactory(AppFactory):
|
||||
def __init__(self, loop: asyncio.AbstractEventLoop):
|
||||
self.loop = loop
|
||||
|
||||
@routes.get('/')
|
||||
async def home(_request: web.Request):
|
||||
return web.Response(body='v6d0auth\n')
|
||||
def define_routes(self, routes: web.RouteTableDef):
|
||||
print(certs.vkey.encode().hex())
|
||||
mycdb = cdb.CDB(self.loop)
|
||||
self.loop.create_task(mycdb.job())
|
||||
|
||||
@routes.post('/approve')
|
||||
async def approve(request: web.Request):
|
||||
try:
|
||||
cert = mycdb.approve(await request.read())
|
||||
except BadSignatureError:
|
||||
raise web.HTTPUnauthorized
|
||||
except KeyError:
|
||||
raise web.HTTPNotFound
|
||||
else:
|
||||
return web.Response(body=cert)
|
||||
@routes.get('/')
|
||||
async def home(_request: web.Request):
|
||||
return web.Response(body='v6d0auth\n')
|
||||
|
||||
@routes.post('/push')
|
||||
async def push(request: web.Request):
|
||||
try:
|
||||
timeout = mycdb.push(VerifyKey(await request.read())).timeout
|
||||
except KeyError:
|
||||
raise web.HTTPTooManyRequests
|
||||
else:
|
||||
return web.Response(body=str(timeout))
|
||||
|
||||
@routes.post('/pull')
|
||||
async def pull(request: web.Request):
|
||||
try:
|
||||
cert = mycdb.pull(VerifyKey(await request.read()))
|
||||
except KeyError:
|
||||
raise web.HTTPNotFound
|
||||
else:
|
||||
return web.Response(body=cert)
|
||||
|
||||
@routes.get('/pullws')
|
||||
async def pullws(request: web.Request):
|
||||
ws = web.WebSocketResponse()
|
||||
await ws.prepare(request)
|
||||
try:
|
||||
srq = mycdb.requester_mapping[VerifyKey(await ws.receive_bytes())]
|
||||
except TypeError:
|
||||
await ws.close(code=http_websocket.WSCloseCode.POLICY_VIOLATION)
|
||||
else:
|
||||
@routes.post('/approve')
|
||||
async def approve(request: web.Request):
|
||||
try:
|
||||
cert = await srq.future
|
||||
except asyncio.CancelledError:
|
||||
if not srq.future.cancelled():
|
||||
srq.future.cancel()
|
||||
if not srq.cancelled:
|
||||
srq.future = asyncio.get_event_loop().create_future()
|
||||
await ws.close(code=http_websocket.WSCloseCode.TRY_AGAIN_LATER)
|
||||
cert = mycdb.approve(await request.read())
|
||||
except BadSignatureError:
|
||||
raise web.HTTPUnauthorized
|
||||
except KeyError:
|
||||
raise web.HTTPNotFound
|
||||
else:
|
||||
await ws.send_bytes(cert)
|
||||
await ws.close()
|
||||
return ws
|
||||
return web.Response(body=cert)
|
||||
|
||||
@routes.post('/push')
|
||||
async def push(request: web.Request):
|
||||
try:
|
||||
timeout = mycdb.push(VerifyKey(await request.read())).timeout
|
||||
except KeyError:
|
||||
raise web.HTTPTooManyRequests
|
||||
else:
|
||||
return web.Response(body=str(timeout))
|
||||
|
||||
def app_routes(loop: asyncio.AbstractEventLoop) -> web.RouteTableDef:
|
||||
routes = web.RouteTableDef()
|
||||
define_routes(routes, loop)
|
||||
return routes
|
||||
@routes.post('/pull')
|
||||
async def pull(request: web.Request):
|
||||
try:
|
||||
cert = mycdb.pull(VerifyKey(await request.read()))
|
||||
except KeyError:
|
||||
raise web.HTTPNotFound
|
||||
else:
|
||||
return web.Response(body=cert)
|
||||
|
||||
|
||||
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))
|
||||
@routes.get('/pullws')
|
||||
async def pullws(request: web.Request):
|
||||
ws = web.WebSocketResponse()
|
||||
await ws.prepare(request)
|
||||
try:
|
||||
srq = mycdb.requester_mapping[VerifyKey(await ws.receive_bytes())]
|
||||
except TypeError:
|
||||
await ws.close(code=http_websocket.WSCloseCode.POLICY_VIOLATION)
|
||||
else:
|
||||
try:
|
||||
cert = await srq.future
|
||||
except asyncio.CancelledError:
|
||||
if not srq.future.cancelled():
|
||||
srq.future.cancel()
|
||||
if not srq.cancelled:
|
||||
srq.future = asyncio.get_event_loop().create_future()
|
||||
await ws.close(code=http_websocket.WSCloseCode.TRY_AGAIN_LATER)
|
||||
else:
|
||||
await ws.send_bytes(cert)
|
||||
await ws.close()
|
||||
return ws
|
||||
|
16
v6d0auth/appfactory.py
Normal file
16
v6d0auth/appfactory.py
Normal 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
|
@ -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()))
|
||||
|
Loading…
Reference in New Issue
Block a user