[BUG] Static files broken
See original GitHub issueDescribe the bug
I want to expose api endpoint from which I can download static
directory. I get 2 unexpected behaviors:
- When I want to reach my endpoint
0.0.0.0/static
I seeNo operations defined in spec!
- From tutorials(mentioned below) I understood it takes file from my app directory? Unless I copy it to root of my OS I get error
raise RuntimeError(f"Directory '{directory}' does not exist")
.
This is really important for my home project to make sense so any input is really appreciated because I’m stuck on this.
To Reproduce Code:
from starlette.staticfiles import StaticFiles
from fastapi import FastAPI
app = FastAPI(title="static_api")
app.mount("/static", StaticFiles(directory="static"))
Dockerfile:
FROM python:3.7
RUN pip install aiofiles fastapi uvicorn python-multipart
EXPOSE 80
COPY ./app /app
COPY ./static /static
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "80"]
I use docker build -t myimage .
and docker run -p 80:80 myimage
.
Expected behavior
Download static directory(or at least any file from this dir) from 0.0.0.0/static
Environment: Docker above.
Already viewed and tried issues/tutorials https://fastapi.tiangolo.com/tutorial/static-files/ https://www.starlette.io/staticfiles/ https://github.com/tiangolo/fastapi/issues/130
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:11 (4 by maintainers)
Top Results From Across the Web
my template is broken and cannot get static files - Stack Overflow
You need to add both debug-time url patterns for static and media: urlpatterns += static(settings.MEDIA_URL, document_root=settings.
Read more >Trouble debugging static files not working with the local server ...
My issue is fairly simple: when doing ./manage.py runserver localhost:8080 none of the files in my static directory are being served, which is ......
Read more >53331 – Broken http response for 200k+ static files
This is ASF Bugzilla: the Apache Software Foundation bug system. In case of problems with the functioning of ASF Bugzilla, please contact ...
Read more >Django can't see my static files - Google Groups
from django.contrib.staticfiles.urls import staticfiles_urlpatterns ... But it is still not working. Do I have a syntax error somewhere?
Read more >Django static files not working - Render community
Edit: i got it to “work”, it shows images or sometimes only the css. But constantly gives 500 server error. Static config: STATIC_URL...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@yaronv you might be able to pass absolute paths without needing to actually hard code an absolute path by doing something like:
Obviously you can reduce the number of lines involved, I just made it more verbose to make it clear what was going on.
Or you could also use an environment variable/config as you noted.
Even if this answer comes a little late, maybe it helps others, because I got into the same problem in the beginning.
I am not sure if this is a bug either? Because it’s called static file and not static directory.
So you won’t be able to request a folder, only a file, which has to be explicit.
0.0.0.0/static/existing_file
if you want to get files as folder you could zip your files to one file
0.0.0.0/static/existing_files.zip
If the error result is that the folder was not found, then a wrong specification must have happened. The following should run, if you want static inside you app-folder:
So you don’t need to copy the static folder in dockerfile additionaly.