Server shutdown (Fixes #19)
This commit is contained in:
@@ -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
40
examples/hello_async.py
Normal 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())
|
||||
Reference in New Issue
Block a user