lasselukkari / aWOT

Arduino web server library.
MIT License
283 stars 41 forks source link

Feature Request / Documentation Question: Custom 404 page / Default Route #107

Closed EmperorArthur closed 3 years ago

EmperorArthur commented 3 years ago

Hello,

I was recently evaluating your library to replace our custom Arduino HTTP Server implementation. Of all the libraries examined so far, yours is definitely the most polished, and certainly has better routing capabilities than the nested if statements ours uses. Please do not take my suggestions as thinking your library is "bad."

We would like to be able to serve custom fallback pages, and I can not find a method to do so. Does this ability exist? If this is not possible, it would be a much appreciated feature.

These are the three use cases we currently have, and why I believe it is important:

Proposed Solution

I believe replacing/appending return m_response.sendStatus(404); in "Application::m_process()" with a check for a custom "Router::Middleware" would easily enable this function.

EmperorArthur commented 3 years ago

If you are interested, I should be able to submit a pull request to resolve this issue.

lasselukkari commented 3 years ago

Hi. Thanks for the feedback. I believe all of these can be done with the library already.

Did you see the CustomNotFound example? I believe it does what you want.

A generic reply to OPTIONS can be done with a middleware that is set to be the first one with app.use(). Then just check the request.method() type and if it's OPTIONS send your reply with the headers and then call res.end() to stop the middleware chain. The AuthMiddleware example kinda works the same way.

The third one in the list would be hard to implement and imo not a good idea considering the benefits. If you really need it you can use app.use(); with or without path and then just check the method type manually.

lasselukkari commented 3 years ago

I agree that it could make sense to implement the feature for the custom not found page. I can do that. I have mixed feelings about the dedicated middleware for the options method.

lasselukkari commented 3 years ago

How about something like this: https://github.com/lasselukkari/aWOT/compare/app-not-found?expand=1

lasselukkari commented 3 years ago

After giving it a second thought I could add back the overloaded versions of app.METHOD() that do not specify paths. I had these before, but removed them at some point because I had no use cases for them personally. The required changes are really small.

lasselukkari commented 3 years ago

It was a 5 minute job: https://github.com/lasselukkari/aWOT/compare/app-not-found...method-middleware?expand=1

EmperorArthur commented 3 years ago

Awesome! I really appreciate everything you're doing.

It really looks like that last one is convenient, but could also be done by documentation.

lasselukkari commented 3 years ago

These features have been now merged to master.