Skip to content

Allow no route to be defined for only serving static files #47

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

Merged
merged 1 commit into from
Apr 19, 2023

Conversation

Neradoc
Copy link
Contributor

@Neradoc Neradoc commented Apr 13, 2023

Just removed the exception when no route has been setup.
Makes something like that work to serve a static website.

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

PORT = 80
ROOT = "/www"

wifi.radio.connect(os.getenv("WIFI_SSID"), os.getenv("WIFI_PASSWORD"))
pool = socketpool.SocketPool(wifi.radio)
server = HTTPServer(pool)

print(f"Listening on http://{wifi.radio.ipv4_address}:{PORT}")
server.serve_forever(str(wifi.radio.ipv4_address), port=PORT, root_path=ROOT)

@anecdata
Copy link
Member

anecdata commented Apr 13, 2023

Intended to serve an index.html only, but not directory listings or other filenames?
(Valid paths return empty page)

edit: paths ending in full filename work fine

@Neradoc
Copy link
Contributor Author

Neradoc commented Apr 13, 2023

Without this PR, trying to access any page with the above code.py causes:

Traceback (most recent call last):
  File "code.py", line 15, in <module>
  File "adafruit_httpserver/server.py", line 77, in serve_forever
  File "adafruit_httpserver/server.py", line 162, in poll
  File "adafruit_httpserver/route.py", line 109, in find_handler
ValueError: No routes added

With the fix it serves all static files in root_path.
That avoids having to create a dummy route like:

@server.route("/")
def base(request: HTTPRequest):
    with HTTPResponse(request, content_type=MIMEType.TYPE_HTML) as response:
        response.send_file("index.html")

Basically there should be no need to declare a route to serve a static website.

For example the Reactle app used in the standalone http server guide works with that code.py and that PR. (Once placed in www)

Copy link
Member

@anecdata anecdata left a comment

Choose a reason for hiding this comment

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

Definitely an improvement for index.html and other files. Deleted my other comments (probably some erroneous testing), I'll re-test properly and comment if there's anything odd.

edit: 404 on a valid directory is the behavior pre-PR too

Copy link
Member

@anecdata anecdata left a comment

Choose a reason for hiding this comment

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

LGTM!

@anecdata anecdata merged commit f323111 into adafruit:main Apr 19, 2023
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Apr 20, 2023
Updating https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k to 2.4.1 from 2.4.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#107 from BiffoBear/raw_dns
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#110 from dgnuff/dhcp-buffer-overflow
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#106 from BiffoBear/bugfix_socket_send

Updating https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer to 2.5.0 from 2.4.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_HTTPServer#48 from Neradoc/respond-to-file-head
  > Merge pull request adafruit/Adafruit_CircuitPython_HTTPServer#47 from Neradoc/allow-no-route

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.

2 participants