Skip to content
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

Static file serving: redirect / to /index.html #1435

Closed
shot-codes opened this issue Mar 1, 2025 · 4 comments
Closed

Static file serving: redirect / to /index.html #1435

shot-codes opened this issue Mar 1, 2025 · 4 comments
Labels

Comments

@shot-codes
Copy link

shot-codes commented Mar 1, 2025

Pain point
I have a folder of static files that I serve:

app = socketio.ASGIApp(
    sio,
    static_files={
        "/":  "/home/.../build",
    },
)

It's frustrating that I have to access it via the URL pattern localhost:5000/index.html

Desired solution
If serving an entire folder I would like the route to automatically point to index.html, while still being able to resolve all other files in the build directory

Alternatives
The alternative is running something like Flask, FastAPI, Django etc. But using just socketio and running programatically with uvicorn suits my needs well except for this one point.

Logs
I guess this isn't so relevant as its expected but I get the following error when i try to access my static files at localhost:5000/

IsADirectoryError: [Errno 21] Is a directory: '/home/.../build'
INFO:     127.0.0.1:51540 - "GET / HTTP/1.1" 500 Internal Server Error
@miguelgrinberg
Copy link
Owner

Do you get an error stack trace? The 500 error means that something crashed. It would be useful to see this stacktrace if you have it.

@shot-codes
Copy link
Author

yes ofc, sorry for not providing it in the original post.

ERROR:    Exception in ASGI application
Traceback (most recent call last):
  File "/home/.../.venv/lib/python3.12/site-packages/uvicorn/protocols/http/h11_impl.py", line 403, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/.../.venv/lib/python3.12/site-packages/uvicorn/middleware/proxy_headers.py", line 60, in __call__
    return await self.app(scope, receive, send)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/.../.venv/lib/python3.12/site-packages/engineio/async_drivers/asgi.py", line 73, in __call__
    await self.serve_static_file(static_file, receive, send)
  File "/home/.../.venv/lib/python3.12/site-packages/engineio/async_drivers/asgi.py", line 83, in serve_static_file
    with open(static_file['filename'], 'rb') as f:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
IsADirectoryError: [Errno 21] Is a directory: '/home/.../build'
INFO:     127.0.0.1:49882 - "GET / HTTP/1.1" 500 Internal Server Error

@miguelgrinberg
Copy link
Owner

Thanks. I believe the problem is that you are mapping / to /home/.../build, which looks like a file. To indicate that you want folder behavior (which includes the attachment of a default index.html) you have to end on a slash:

    static_files={
        "/":  "/home/.../build/",
    },

Give that a try and let me know if that solves the problem.

@shot-codes
Copy link
Author

You are absolutely right Miguel, adding the trailing slash solves my problem. Sorry for wasting your time with such an oversight on my end. Appreciate the help :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants