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

Multiple directories in StaticFiles #625

Closed
taoufik07 opened this issue Aug 30, 2019 · 8 comments
Closed

Multiple directories in StaticFiles #625

taoufik07 opened this issue Aug 30, 2019 · 8 comments

Comments

@taoufik07
Copy link
Contributor

taoufik07 commented Aug 30, 2019

The feature request is to be able to serve different directories under the same route.

The StaticFiles app only allows serving from one directory, so we can't have have multiple apps under the same route.

Suggestions :

1- Add an add_directory method to StaticFiles app, to allow adding multiple directories to the the same StaticFiles app and it can be used like so :

static_app = StaticFiles(directory="static")
static_app.add_directory("another_static_dir")

We can also add add_directories which accepts a list of directories

2- Allow passing a list of directories in init, I know it's not a good idea to change the api :/, and to avoid that one way could be adding a kwarg directories: List[str], I'm also aware it's not a good idea :3 (even worse) to expose two ways of doing things directory and directoriesbut the way of implementing it should be discussed, ofc if accepted.

@tomchristie
Copy link
Member

Good summary on this issue, yup!
I'm not really sure what we want the API for this to look like, bit conflicted.

@taoufik07
Copy link
Contributor Author

We can now add the add_directory, and later discuss how to pass multiple directories in the constructor, if we wanna allow it and change the existing API ofc.

@as-dg
Copy link

as-dg commented Feb 17, 2021

Any news on this?

@Kludex
Copy link
Member

Kludex commented Jan 30, 2022

@aminalaee you've been creating some PRs around StaticFiles. Do you think this makes sense?

@aminalaee
Copy link
Member

@Kludex yeah. It does make sense. There is no conclusion about the API for it.

Honestly I don't think this is very nice:

static_app = StaticFiles(directory="static")
static_app.add_directory("another_static_dir")

We did handle multiple directories for third party packages:

StaticFiles(packages=[('bootstrap4', 'static')])

But for local directories I think we can work around it.
Maybe later on we can change the signature to handle directories too.

@Kludex
Copy link
Member

Kludex commented Feb 1, 2022

Could we accept both str and List[str] on directory? Is that bad? Or should we create directories and make it mutual exclusive with directory?

@tomchristie
Copy link
Member

Closing with the same rationale as #1214

There's plenty decent enough ways users can do this if they're really keen. Eg. subclass StaticFiles and override the get_directories method.

It's okay for us to just default to one directory + packages.

@pomarec
Copy link

pomarec commented Apr 20, 2022

fyi, I used the following solution :

import typing

from starlette.staticfiles import PathLike, StaticFiles


class MultiStaticFiles(StaticFiles):
    def __init__(self, directories: typing.List[PathLike] = [], **kwargs) -> None:
        super().__init__(**kwargs)
        self.all_directories = self.all_directories + directories

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

No branches or pull requests

6 participants