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
|
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):
|
||||||
print(certs.vkey.encode().hex())
|
def __init__(self, loop: asyncio.AbstractEventLoop):
|
||||||
mycdb = cdb.CDB(loop)
|
self.loop = loop
|
||||||
loop.create_task(mycdb.job())
|
|
||||||
|
|
||||||
@routes.get('/')
|
def define_routes(self, routes: web.RouteTableDef):
|
||||||
async def home(_request: web.Request):
|
print(certs.vkey.encode().hex())
|
||||||
return web.Response(body='v6d0auth\n')
|
mycdb = cdb.CDB(self.loop)
|
||||||
|
self.loop.create_task(mycdb.job())
|
||||||
|
|
||||||
@routes.post('/approve')
|
@routes.get('/')
|
||||||
async def approve(request: web.Request):
|
async def home(_request: web.Request):
|
||||||
try:
|
return web.Response(body='v6d0auth\n')
|
||||||
cert = mycdb.approve(await request.read())
|
|
||||||
except BadSignatureError:
|
|
||||||
raise web.HTTPUnauthorized
|
|
||||||
except KeyError:
|
|
||||||
raise web.HTTPNotFound
|
|
||||||
else:
|
|
||||||
return web.Response(body=cert)
|
|
||||||
|
|
||||||
@routes.post('/push')
|
@routes.post('/approve')
|
||||||
async def push(request: web.Request):
|
async def approve(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:
|
|
||||||
try:
|
try:
|
||||||
cert = await srq.future
|
cert = mycdb.approve(await request.read())
|
||||||
except asyncio.CancelledError:
|
except BadSignatureError:
|
||||||
if not srq.future.cancelled():
|
raise web.HTTPUnauthorized
|
||||||
srq.future.cancel()
|
except KeyError:
|
||||||
if not srq.cancelled:
|
raise web.HTTPNotFound
|
||||||
srq.future = asyncio.get_event_loop().create_future()
|
|
||||||
await ws.close(code=http_websocket.WSCloseCode.TRY_AGAIN_LATER)
|
|
||||||
else:
|
else:
|
||||||
await ws.send_bytes(cert)
|
return web.Response(body=cert)
|
||||||
await ws.close()
|
|
||||||
return ws
|
|
||||||
|
|
||||||
|
@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.post('/pull')
|
||||||
routes = web.RouteTableDef()
|
async def pull(request: web.Request):
|
||||||
define_routes(routes, loop)
|
try:
|
||||||
return routes
|
cert = mycdb.pull(VerifyKey(await request.read()))
|
||||||
|
except KeyError:
|
||||||
|
raise web.HTTPNotFound
|
||||||
|
else:
|
||||||
|
return web.Response(body=cert)
|
||||||
|
|
||||||
|
@routes.get('/pullws')
|
||||||
def app_with_routes(routes: web.RouteTableDef):
|
async def pullws(request: web.Request):
|
||||||
app = web.Application()
|
ws = web.WebSocketResponse()
|
||||||
app.add_routes(routes)
|
await ws.prepare(request)
|
||||||
return app
|
try:
|
||||||
|
srq = mycdb.requester_mapping[VerifyKey(await ws.receive_bytes())]
|
||||||
|
except TypeError:
|
||||||
def get_app(loop: asyncio.AbstractEventLoop) -> web.Application:
|
await ws.close(code=http_websocket.WSCloseCode.POLICY_VIOLATION)
|
||||||
return app_with_routes(app_routes(loop))
|
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
|
import asyncio
|
||||||
|
|
||||||
from v6d0auth.app import get_app
|
from v6d0auth.app import V6D0AuthAppFactory
|
||||||
from v6d0auth.run_app import run_app
|
from v6d0auth.run_app import run_app
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
loop = asyncio.get_event_loop()
|
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