belyalov / tinyweb

Simple and lightweight HTTP async server for micropython
MIT License
239 stars 40 forks source link

Added @catchall decorator, to serve custom 404 #34

Closed metachris closed 3 years ago

metachris commented 3 years ago

I've added an @catchall decorator which works very similar to @route. The handler is called on every 404, giving users to ability to serve custom 404 pages and status codes (also useful for captive portals).

Not sure if you want to add this, but I think it's very useful and wanted to offer this contribution :) If you want it, I can add tests and docs too.

@app.catchall()
def catchall_handler(req, resp):
    response.code = 404
    await response.start_html()
    await response.send('<html><body><h1>My custom 404!</h1></html>\n')
belyalov commented 3 years ago

Thanks for contributing! Mind adding unittest?

e.g. https://github.com/belyalov/tinyweb/blob/04c1e1e8f31f7e6841adcb79bded57334d7c3539/test/test_server.py#L281-L283 and https://github.com/belyalov/tinyweb/blob/04c1e1e8f31f7e6841adcb79bded57334d7c3539/test/test_server.py#L347-L365

Otherwise looks good! :)

metachris commented 3 years ago

Will do! Might be a few days until I find a bit of time tho (2 kids) :)

metachris commented 3 years ago

Added unit test

belyalov commented 3 years ago

Cool! ) Looks awesome