adafruit / Adafruit_CircuitPython_HTTPServer

Simple HTTP Server for CircuitPython
MIT License
46 stars 30 forks source link

Create an httpserver_simpletest example #50

Closed jrrickerson closed 1 year ago

jrrickerson commented 1 year ago

Create a bare minimum example that proves the HTTPServer is running on the device, akin to a "Hello World"

jrrickerson commented 1 year ago

I'm going to try working on this one at PyCon US 2023 sprints

michalpokusa commented 1 year ago

I am not entirely sure what you mean, but there is already a very simple example: https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer/blob/main/examples/httpserver_simple_serve.py

If you delete all docstrings, prints etc. it is only 4-5 lines long, not considering connecting to WiFi. If you want it without a handler for "/", it is 2 lines long.

One could for sure save some characters but this would not be a realistic scenario for using this lib. Correct me if I understood this Issue incorrectly.

michalpokusa commented 1 year ago

The minified version of the example would be something like this:

import os, socketpool, wifi
from adafruit_httpserver.server import HTTPServer

ssid, password = os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD")
wifi.radio.connect(ssid, password)
HTTPServer(socketpool.SocketPool(wifi.radio), "/").serve_forever(str(wifi.radio.ipv4_address))

I believe it should be possible to omit manually connecting to WiFi by using special env vars, making the example even shorter:

import socketpool, wifi
from adafruit_httpserver.server import HTTPServer

HTTPServer(socketpool.SocketPool(wifi.radio), "/").serve_forever(str(wifi.radio.ipv4_address))

but at this stage it kind of misses the point of being an example.

jrrickerson commented 1 year ago

I did see the simple server example, but I thought I could provide another that required a bit less work by the end user, and fit the library standard of having a "_simpletest.py" module in the examples folder. I've submitted a PR for it but let me know if you think it's redundant.

Kinda going for less setup steps rather than less lines of code, if that makes sense

michalpokusa commented 1 year ago

Ok, I see. To be honest I believe it would make more sense to modify existing one, and replace response.send_file("index.html") with response.send("Hello World").

Creating a separate example with only one line modified seems like a overkill for me.

jrrickerson commented 1 year ago

@michalpokusa Thanks, I appreciate the feedback. I can make adjustments to the PR to merge the two examples then.

jrrickerson commented 1 year ago

@michalpokusa I've merged the two examples, and renamed the "simple_serve" example to "simpletest" to adhere to the convention which appears to be in the other CircuitPython libraries and this contributing guide: https://circuitpython.org/contributing/library-infrastructure-issues

Please let me know if you see any other areas for improvement.