Skip to content

4.0.0 - Huge refactor, more examples, authentication, configurable MIMETypes #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

michalpokusa
Copy link
Contributor

@michalpokusa michalpokusa commented Apr 28, 2023

New Features 🎉:
- Added Basic and Bearer authentication options on server and per route level
- Rewritten logic for MIMETypes, now it is possible to remove unused, thus saving memory, and register new ones
- root_path in Server constructor is now optional, if not specified or None, access to filesystem is disabled, which resolves #52
- request parameter in handler functions now has access to server that received it
- EDIT1: Request.json() for easy parsing of JSON POST data
- EDIT1: host and port attributes on Server
- EDIT1: Debug mode for displaying messages useful during development
- EDIT1: Added append_slash to Server.route
- EDIT2: Added ... and .... wildcards
- EDIT3: New classes fro reponse types: ChunkedResponse, JSONResponse, FileResponse, Redirect
- Added ability to stop and start a Server again
- Documented option to host multiple servers on single board, despite not being the intended use, it is possible and might be useful in some cases

Modified Features 🏗️:
- server.route() now can accept list of methods in addition to single one
- Routes now respect trailing / at the end of a path, so /example and /example/ are two diffrent paths
- serve_forever does not raise any error and continues to work no matter what
- Response.send_file default to root_path instead of root of the filesystem
- EDIT3: Response is now separated into many classes for diffrent usages like sending file or JSON data
- EDIT3: Response objects are now returned from handler functions instead of being used in context managers, this makes them easier and more intuitive to use, repalced all public methods with classes that have same functionality
- URL parameters are now keyword instead of positional, which makes them much more flexible

Refactor ⚙️:
- Removed HTTP... from class names in order to unify naming with other libs, so e.g. HTTPServer becomes Server, HTTPRequest becomes Request etc.
- It is now possible to import directly from adafruit_httpserver instead of using multiple import statements for each component
- Replaced CommonHTTPStatus with objects of Status class
- EDIT1: Refactor of _Routes and _Route classes

Docs 📘:
- Updated docs to new API
- Added new examples that show previously unexplained usages
- Added :emphasize-lines: to examples to mark most important parts of them

Testing of these changes would be greatly appreciated 😉

@michalpokusa
Copy link
Contributor Author

michalpokusa commented Apr 28, 2023

Just out of curiosity I tried starting multiple servers on one board at the same time, of couse on diffrent ports.
Although it was not an intended use, it seems to work fine 😂.

Not sure whether it is worth documenting...
I added an example to the docs.

@michalpokusa michalpokusa force-pushed the 4.0.0-examples-refactor-authentication-mimetypes branch from 289f75e to 415218b Compare April 29, 2023 15:07
@michalpokusa michalpokusa force-pushed the 4.0.0-examples-refactor-authentication-mimetypes branch from 415218b to 61135f1 Compare April 29, 2023 15:14
@FoamyGuy
Copy link
Contributor

I tested this out today on a Feather S2 TFT. It seems to work great, I do have a few changes to suggest:

  • If the user doesn't have proper CIRCUITPY_WIFI_SSID or CIRCUITPY_WIFI_PASSWORD and they try to run in automatic mode raise some error telling them the values aren't set.
  • Add a argument or flag of some sort that allows serve_forever() to print exceptions if they occur to make troubleshooting route handlers easier
  • I tried to request from /implicit-require/ and got a not found type response I think. If I understand correctly it is supposed to allow the trailing slash.
  • serve_forever() should be able (even if opionally) to re-raise the KeyboardInterrupt exception so that user can stop with ctrl c

@michalpokusa michalpokusa force-pushed the 4.0.0-examples-refactor-authentication-mimetypes branch from 2ad4a4c to a424d27 Compare May 3, 2023 13:53
@michalpokusa michalpokusa marked this pull request as ready for review May 3, 2023 15:20
@michalpokusa michalpokusa force-pushed the 4.0.0-examples-refactor-authentication-mimetypes branch from abdcb05 to 3359668 Compare May 4, 2023 00:05
@michalpokusa michalpokusa marked this pull request as draft May 5, 2023 23:11
@michalpokusa
Copy link
Contributor Author

During a recent Deep Dive stream from @FoamyGuy, @Neradoc suggested implementing default handlers for specific error types.

Current implementation does not allow that, I am now working on rewrite of response.py that would change the behaviour of lib, so that a Response is returned from handler, which is definitely a more intuitive approach that current context manager version. This change would allow implementing default handlers and it would be backwards compatible, e.g. as version 4.1.0.

This is a moderately large change and it requires a lot of changes in docs, examples and nearly a complete rewrite of response logic. I will try to include it inside this PR as including it in future 5.0.0 and again reinventing the API might be frustrating, as all code that uses adafruit_httpserver would have to be modifid again.

@michalpokusa michalpokusa marked this pull request as ready for review May 8, 2023 22:28
@michalpokusa
Copy link
Contributor Author

michalpokusa commented May 8, 2023

I believe current state of this PR is satisfying. All the changes, both added initially and ones added after creating PR are listed in the first post above, with EDIT next to them if they were added/modified later. I do not plan any more features here, already there are plenty of new ones.

This PR is a good foundation for implementing e.g. middleware or default handlers in the future, if the need for them comes.

Testing and code reviews or any other feedback would be appreciated.

Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking good to me at this point.

I tested nearly all examples in this repo on a Feather S2 TFT 8.1.0-rc.0.

I submitted adafruit/Adafruit_Learning_System_Guides#2511 to update the only usage outside of this repo that I could find. I searched the Learn repo and the Library Bundle Examples.

Thank you again for all of the improvements to this library @michalpokusa!

@FoamyGuy FoamyGuy merged commit 2e205dc into adafruit:main May 22, 2023
@michalpokusa
Copy link
Contributor Author

Thank you for testing and merging! 👍

adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request May 23, 2023
Updating https://github.com/adafruit/Adafruit_CircuitPython_EPD to 2.11.2 from 2.11.1:
  > Merge pull request adafruit/Adafruit_CircuitPython_EPD#65 from sdomoszlai13/fix-annotations-epd.py

Updating https://github.com/adafruit/Adafruit_CircuitPython_asyncio to 0.5.21 from 0.5.20:
  > Merge pull request adafruit/Adafruit_CircuitPython_asyncio#41 from Neradoc/fix-package-prefix

Updating https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer to 4.0.0 from 3.0.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_HTTPServer#54 from michalpokusa/4.0.0-examples-refactor-authentication-mimetypes
  > Run pre-commit
  > Update pre-commit hooks
  > Merge pull request adafruit/Adafruit_CircuitPython_HTTPServer#53 from adafruit/not-robust

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How do I safely disable all access to filesystem?
2 participants