ASGI support

This commit is contained in:
Miguel Grinberg
2022-05-25 23:47:37 +01:00
parent 1ae51ccdf7
commit 7e8ecb1997
8 changed files with 368 additions and 64 deletions

37
examples/hello_asgi.py Normal file
View File

@@ -0,0 +1,37 @@
try:
import uasyncio as asyncio
except ImportError:
import asyncio
from microdot_asgi import Microdot, Response
app = Microdot()
htmldoc = '''<!DOCTYPE html>
<html>
<head>
<title>Microdot Example Page</title>
</head>
<body>
<div>
<h1>Microdot Example Page</h1>
<p>Hello from Microdot!</p>
<p><a href="/shutdown">Click to shutdown the server</a></p>
</div>
</body>
</html>
'''
@app.route('/')
async def hello(request):
return htmldoc, 200, {'Content-Type': 'text/html'}
@app.route('/shutdown')
async def shutdown(request):
request.app.shutdown()
return 'The server is shutting down...'
if __name__ == '__main__':
app.run(debug=True)

View File

@@ -24,7 +24,7 @@ htmldoc = '''<!DOCTYPE html>
@app.route('/')
async def hello(request):
return Response(body=htmldoc, headers={'Content-Type': 'text/html'})
return htmldoc, 200, {'Content-Type': 'text/html'}
@app.route('/shutdown')