Skip to content

Commit

Permalink
Merge 9c27f28 into 337db9f
Browse files Browse the repository at this point in the history
  • Loading branch information
brainelectronics authored Feb 18, 2023
2 parents 337db9f + 9c27f28 commit 0f22dd8
Show file tree
Hide file tree
Showing 5 changed files with 522 additions and 23 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,9 @@ This is a list of available webpages
|-----|-------------|
| `/` | Root index page, to choose from the available pages |
| `/select` | Select and configure a network |
| `/configure` | Manage already configured networks |
| `/configure` | Manage already configured networks |
| `/scan_result` | JSON of available networks |
| `/shutdown` | Shutdown webserver and return from `run` function |

To leave from the Webinterface, just press CTRL+C and wait until all threads
finish running. This takes around 1 second. The device will return to its REPL
Expand Down
11 changes: 10 additions & 1 deletion changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ r"^\#\# \[\d{1,}[.]\d{1,}[.]\d{1,}\] \- \d{4}\-\d{2}-\d{2}$"
<!-- ## [Unreleased] -->

## Released
## [1.10.0] - 2023-02-18
### Added
- `microdot_asyncio` in `microdot` folder
- `/shutdown` endpoint to stop webserver

### Changed
- All webserver functions are `async`, see #28

## [1.9.0] - 2023-02-17
### Added
- `test-release` and `release` workflows create changelog based (pre-)releases
Expand Down Expand Up @@ -240,8 +248,9 @@ r"^\#\# \[\d{1,}[.]\d{1,}[.]\d{1,}\] \- \d{4}\-\d{2}-\d{2}$"
- `sendfile` function implemented in same way as on Micropythons PicoWeb

<!-- Links -->
[Unreleased]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager/compare/1.9.0...develop
[Unreleased]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager/compare/1.10.0...develop

[1.10.0]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager//tree/1.10.0
[1.9.0]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager//tree/1.9.0
[1.8.0]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager//tree/1.8.0
[1.7.1]: https://github.com/brainelectronics/Micropython-ESP-WiFi-Manager//tree/1.7.1
Expand Down
29 changes: 29 additions & 0 deletions microdot/microdot.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,9 @@ def func(request, response):
return response
return 'Hello, World!'
Note that the function is not called if the request handler raises an
exception and an error response is returned instead.
"""
self.after_request_handlers.append(f)
return f
Expand Down Expand Up @@ -746,6 +749,7 @@ def __init__(self):
self.url_map = []
self.before_request_handlers = []
self.after_request_handlers = []
self.after_error_request_handlers = []
self.error_handlers = {}
self.shutdown_requested = False
self.debug = False
Expand Down Expand Up @@ -907,6 +911,24 @@ def func(request, response):
self.after_request_handlers.append(f)
return f

def after_error_request(self, f):
"""Decorator to register a function to run after an error response is
generated. The decorated function must take two arguments, the request
and response objects. The return value of the function must be an
updated response object. The handler is invoked for error responses
generated by Microdot, as well as those returned by application-defined
error handlers.
Example::
@app.after_error_request
def func(request, response):
# ...
return response
"""
self.after_error_request_handlers.append(f)
return f

def errorhandler(self, status_code_or_exception_class):
"""Decorator to register a function as an error handler. Error handler
functions for numeric HTTP status codes must accept a single argument,
Expand Down Expand Up @@ -947,6 +969,8 @@ def mount(self, subapp, url_prefix=''):
self.before_request_handlers.append(handler)
for handler in subapp.after_request_handlers:
self.after_request_handlers.append(handler)
for handler in subapp.after_error_request_handlers:
self.after_error_request_handlers.append(handler)
for status_code, handler in subapp.error_handlers.items():
self.error_handlers[status_code] = handler

Expand Down Expand Up @@ -1094,6 +1118,7 @@ def handle_request(self, sock, addr):
status_code=res.status_code))

def dispatch_request(self, req):
after_request_handled = False
if req:
if req.content_length > req.max_content_length:
if 413 in self.error_handlers:
Expand Down Expand Up @@ -1126,6 +1151,7 @@ def dispatch_request(self, req):
res = handler(req, res) or res
for handler in req.after_request_handlers:
res = handler(req, res) or res
after_request_handled = True
elif f in self.error_handlers:
res = self.error_handlers[f](req)
else:
Expand Down Expand Up @@ -1166,6 +1192,9 @@ def dispatch_request(self, req):
res = Response(*res)
elif not isinstance(res, Response):
res = Response(res)
if not after_request_handled:
for handler in self.after_error_request_handlers:
res = handler(req, res) or res
return res


Expand Down
Loading

0 comments on commit 0f22dd8

Please sign in to comment.