Server shutdown (Fixes #19)

This commit is contained in:
Miguel Grinberg
2021-06-04 15:00:15 +01:00
parent b810346aa4
commit 0ad538df91
21 changed files with 1075 additions and 639 deletions

View File

@@ -2,7 +2,7 @@ from microdot import Microdot, Response
app = Microdot()
htmldoc = """<!DOCTYPE html>
htmldoc = '''<!DOCTYPE html>
<html>
<head>
<title>Microdot Example Page</title>
@@ -11,16 +11,22 @@ htmldoc = """<!DOCTYPE html>
<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("", methods=["GET", "POST"])
def serial_number(request):
print(request.headers)
return Response(body=htmldoc, headers={"Content-Type": "text/html"})
@app.route('/')
def hello(request):
return Response(body=htmldoc, headers={'Content-Type': 'text/html'})
@app.route('/shutdown')
def shutdown(request):
request.app.shutdown()
return 'The server is shutting down...'
app.run(debug=True)

40
examples/hello_async.py Normal file
View File

@@ -0,0 +1,40 @@
try:
import uasyncio as asyncio
except ImportError:
import asyncio
from microdot_asyncio 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 Response(body=htmldoc, headers={'Content-Type': 'text/html'})
@app.route('/shutdown')
async def shutdown(request):
request.app.shutdown()
return 'The server is shutting down...'
async def main():
await app.start_server(debug=True)
asyncio.run(main())