Created
May 8, 2024 12:05
-
-
Save astynax/50bc897c5801abd0f78e453f3554e41d to your computer and use it in GitHub Desktop.
An example of stateful handler with geberator based continuations.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uuid | |
from aiohttp import web | |
CONTS = {} | |
async def handle(request: web.Request) -> web.Response: | |
k = request.query.get("cont") | |
if k is None: | |
key = uuid.uuid4().hex | |
cont = wizard(request, key) | |
CONTS[key] = cont | |
return await anext(cont) | |
elif (cont := CONTS.get(k)) is not None: | |
return await cont.asend(request) # TODO: handle StopIteration | |
raise web.HTTPNotFound(text="Bad session") | |
async def wizard(_: web.Request, key: str): | |
req = yield web.Response(text=f"""<!DOCTYPE html> | |
<html lang="en"> | |
<body> | |
<h2>Welcome to aioh-cont</h2> | |
<form action="/"> | |
<input name="cont" value="{key}" hidden> | |
<label for="name">Name:</label> | |
<input name="name"> | |
<input type="submit"> | |
</form> | |
</body> | |
</html> | |
""" , content_type="text/html") | |
name = req.query['name'] | |
yield web.Response(text=f"""<!DOCTYPE html> | |
<html lang="en"> | |
<body> | |
<h2>Hello, {name}!</h2> | |
<a href="/">Again</a> | |
</body> | |
</html>""" , content_type="text/html") | |
app = web.Application() | |
app.router.add_route('GET', '/', handle) | |
if __name__ == '__main__': | |
web.run_app(app) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment