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

Start static directory support #1024

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft

Conversation

dave42w
Copy link
Contributor

@dave42w dave42w commented Nov 11, 2024

Description

This PR fixes #1005

Work In Progress

Summary

This PR provides a simple way to add a static directory for the Robyn app and each SubRouter. It calls serve_directory but with fewer arguments and simple defaults. The goal will be to make it a one line addition eg

change from (in the initial example at https://robyn.tech/documentation/example_app/modeling_routes )

app = Robyn(__file__)

@app.post("/crimes")

to

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

@app.post("/crimes")

For the subrouter the same ( https://robyn.tech/documentation/example_app/subrouters_and_views )
'''
frontend = SubRouter(name, prefix="/frontend")
frontend.set_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 are these routes /static/crimes.css and '/frontend/static/fronts.css' (where frontend is the object name not the route prefix)

Then we can add support to templating allowing us simple access to static files in templates which won't change when the route prefix for frontend changes

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

PR Checklist

Please ensure that:

  • The PR contains a descriptive title
  • The PR contains a descriptive summary of the changes
  • You build and test your changes before submitting a PR.
  • You have added relevant documentation
  • You have added relevant tests. We prefer integration tests wherever possible

Pre-Commit Instructions:

Copy link

vercel bot commented Nov 11, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
robyn ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 22, 2024 11:01pm

@dave42w
Copy link
Contributor Author

dave42w commented Nov 17, 2024

Hi @sansyrox
I just wanted to check if you would accept this PR if I complete the code (obviously in an acceptable way)
Thanks
Dave

Copy link

codspeed-hq bot commented Nov 20, 2024

CodSpeed Performance Report

Merging #1024 will not alter performance

Comparing dave42w:static_files (adeba3c) with main (9a95611)

Summary

✅ 146 untouched benchmarks

@dave42w
Copy link
Contributor Author

dave42w commented Nov 21, 2024

I've decided that set_static_files_directory is better as it does not imply you can/should add multiple static directories.

Also if the directory does not exist then throw an assertion is going to help resolve deployment issues quicker especially as it shows the exact path it's trying to use.

@sansyrox
Copy link
Member

Hey @dave42w

Hi @sansyrox
I just wanted to check if you would accept this PR if I complete the code (obviously in an acceptable way)
Thanks
Dave

Apologies for missing this PR. Having a review now 😄

@sansyrox
Copy link
Member

Alright, one question

What is the concern with

app.serve_directory(
    route="/test_dir",
    directory_path=os.path.join(current_file_path, "build"),
    index_file="index.html",
)

Apart from it not coming up in the search?

@dave42w
Copy link
Contributor Author

dave42w commented Nov 21, 2024

Alright, one question

What is the concern with

app.serve_directory(
    route="/test_dir",
    directory_path=os.path.join(current_file_path, "build"),
    index_file="index.html",
)

Apart from it not coming up in the search?

for me:

  • not showing clearly enough in documentation
  • not making it easy to have separate static directories for each subrouter
  • clear, sane defaults that mean we can expect to have a static route and a static directory in an app reduces learning time

@sansyrox
Copy link
Member

sansyrox commented Nov 21, 2024

not showing clearly enough in documentation

Yes, this is very bad, and we should fix this!

not making it easy to have separate static directories for each subrouter

you can do subrouter.serve_directory(..) . But yes, we should update the docs again

clear, sane defaults that mean we can expect to have a static route and a static directory in an app reduces learning time

Maybe it does. But it also imposes opinions and structure on the developers. Robyn is made to be extensible. We can maybe improve the implementation of serve_directory to make it simpler but I am not super keen on fixing defaults until absolutely necessary.

@dave42w
Copy link
Contributor Author

dave42w commented Nov 21, 2024

not showing clearly enough in documentation

Yes, this is very bad, and we should fix this!

I tried and npm told me that it hated me and I should run away :-)

not making it easy to have separate static directories for each subrouter

you can do subrouter.serve_directory(..) . But yes, we should update the docs again

You can but it doesn't help you setup a sane way of doing this. So you have to dive much deeper.

clear, sane defaults that mean we can expect to have a static route and a static directory in an app reduces learning time

Maybe it does. But it also imposes opinions and structure on the developers. Robyn is made to be extensible. We can maybe improve the implementation of serve_directory to make it simpler but I am not super keen on fixing defaults until absolutely necessary.

I'm not sure that offering defaults counts as imposing :-)

@sansyrox
Copy link
Member

I tried and npm told me that it hated me and I should run away :-)

I will do it then. Let me make an issue :D

I'm not sure that offering defaults counts as imposing :-)

Not always but here, we will be hardcoding a static folder path, no?

@dave42w
Copy link
Contributor Author

dave42w commented Nov 21, 2024

I tried and npm told me that it hated me and I should run away :-)

I will do it then. Let me make an issue :D

Thanks, seen it :-)

I'm not sure that offering defaults counts as imposing :-)

Not always but here, we will be hardcoding a static folder path, no?

Only hardcoded as a default argument.

I'm thinking that if you go for the defaults then the route is static within Robyn and prefix/static for subroutes

default directories to be something like

./static/robyn      # for the main app
./static/prefix      # one for each subroute

when we start supporting nested subroutes we would change to something like

./static/robyn                              # for the main app static files
./static/prefix1/robyn                    # for a subroute static files
./static/prefix1/prefix2/robyn      # for a nested subroute static files

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.

Serving static files
2 participants