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

Serving static files #1005

Open
dave42w opened this issue Oct 29, 2024 · 5 comments · May be fixed by #1024
Open

Serving static files #1005

dave42w opened this issue Oct 29, 2024 · 5 comments · May be fixed by #1024
Labels
enhancement New feature or request

Comments

@dave42w
Copy link
Contributor

dave42w commented Oct 29, 2024

Robyn has some powerful features for serving static files. However, there are a couple of issues/

Hidden Documentation

At the moment the docs for serving files are "hidden" in "File Uploads" https://robyn.tech/documentation/api_reference/file-uploads

Can we make this more visible by moving it to the top level of API reference (and preferably renaming it to something like "Serving Static Files")

I tried editing the docs but npm hates me ;-)

Simple default

[Update] this PR starts realm code and I think it is simpler/better #1024

As we can serve directories (for CSS, etc.), providing a default would save people a lot of reading.

I'd like us to add ROBYN_STATIC_FILE_DIRECTORY to the robyn.env file. If this is set then we automatically call app.serve_directory() to provide this static directory under the URL "/static/"

SubRouters

It would also seem sensible be able to add static directories to each SubRouter eg by passing a Bool argument to the function

frontend = SubRouter(name, prefix="/frontend", True) ## make /frontend/static/ serve files
frontend = SubRouter(name, prefix="/frontend", False) ## No support for /frontend/static/
frontend = SubRouter(name, prefix="/frontend") ## Default to False ie No support for /frontend/static/

url_for

The flask version of url_for supports static files but only for a single directory.

I'm thinking we could have url_for('static', 'filename.css') to serve from the application-wide static directory

Then for the subrouters we have url_for('static', 'filename.css', subroute='RouterName')

This way we have minimal impact on our templates if we restructure the project.

@dave42w
Copy link
Contributor Author

dave42w commented Nov 1, 2024

See #996 (comment)

@sansyrox
Copy link
Member

sansyrox commented Nov 2, 2024

@VishnuSanal

@dave42w
Copy link
Contributor Author

dave42w commented Nov 12, 2024

I think that my plan in PR #1024 makes this even simpler.

@sansyrox
Copy link
Member

Hey @dave42w 👋

[Update] this PR starts realm code and I think it is simpler/better #1024

As we can serve directories (for CSS, etc.), providing a default would save people a lot of reading.

I'd like us to add ROBYN_STATIC_FILE_DIRECTORY to the robyn.env file. If this is set then we automatically call app.serve_directory() to provide this static directory under the URL "/static/"

Why can't someone just create a static folder themself? Why do we need a new default?

@dave42w
Copy link
Contributor Author

dave42w commented Nov 16, 2024

Why can't someone just create a static folder themself? Why do we need a new default?

My PR updates this. The reason for it is a) to be simpler than serve_directory is at present and b) to work for subroutes as well

I'm suggesting:

app = Robyn(__file__)
app.add_static_files_directory() #using a sane default directory path and "static" as the route both customisable via arguments

@app.post("/crimes")

frontend = SubRouter(name, prefix="/frontend")
frontend.add_static_files_directory() #using a sane default directory path and "static" as the route both customisable via arguments

@frontend.get("/")

What I want to support from templates is

get_static_url("crimes.css") 
get_static_url("frontend/fronts.css") 

It would even support

get_static_url("frontend/crimes.css") 

there could be a fallback so that if it doesn't find crimes.css in the subrouter static directory it looks in the main one. That could provide a nice fallback support.

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

Successfully merging a pull request may close this issue.

2 participants