Skip to content

Commit

Permalink
document functionality to have multiple pathes / directories for stat…
Browse files Browse the repository at this point in the history
…ic files and fix typings (#446)

- Expose functionality to have multiple pathes/directories
- Update staticfiles docs
- Provide multiple examples
  • Loading branch information
devkral authored Nov 28, 2024
1 parent f2435c0 commit 2c2887f
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 5 deletions.
12 changes: 12 additions & 0 deletions docs/en/docs/configurations/staticfiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,15 @@ but also via settings.
```

This will make sure you keep the settings clean, separated and without a bloated **Esmerald** instance.

## Multiple directories and multiple pathes

Imagine, for example, you have multiple directories you would like to access including a `node_modules/` one.
This is possible do do it by passing multiple `StaticFilesConfig` configurations and shown below:

```python
{!> ../../../docs_src/configurations/staticfiles/example_multiple.py!}
```

!!! Note
The first path match is used and there is currently no fallthrough in case no file is found, so the order is very important.
1 change: 1 addition & 0 deletions docs/en/docs/release-notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ hide:
### Fixed

- Fix cli detection of wrapped esmerald instances or different ASGI servers.
- Allow passing multiple `StaticFilesConfig` configurations in a tuple.

## 3.5.1

Expand Down
13 changes: 13 additions & 0 deletions docs_src/configurations/staticfiles/example_multiple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from pathlib import Path

from esmerald import Esmerald, StaticFilesConfig

static_files_config = StaticFilesConfig(
path="/static", packages=["mypackage"], directory=Path("static")
)

static_files_node_modules_config = StaticFilesConfig(
path="/static/node_modules", directory=Path("node_modules")
)

app = Esmerald(static_files_config=[static_files_node_modules_config, static_files_config])
11 changes: 8 additions & 3 deletions esmerald/applications.py
Original file line number Diff line number Diff line change
Expand Up @@ -720,13 +720,18 @@ def is_valid(number: int) -> bool:
),
] = None,
static_files_config: Annotated[
Optional["StaticFilesConfig"],
Union[
"StaticFilesConfig",
list["StaticFilesConfig"],
tuple["StaticFilesConfig", ...],
None,
],
Doc(
"""
An instance of [StaticFilesConfig](https://esmerald.dev/configurations/staticfiles/).
This configuration is used to enable and serve static files via
Esmerald application.
Esmerald application. You can pass also multiple objects via a list or tuple.
**Example**
Expand Down Expand Up @@ -1683,7 +1688,7 @@ def _configure(self) -> None:
if self.static_files_config:
for config in (
self.static_files_config
if isinstance(self.static_files_config, list)
if isinstance(self.static_files_config, (list, tuple))
else [self.static_files_config]
):
static_route = Include(path=config.path, app=config.to_app())
Expand Down
4 changes: 3 additions & 1 deletion esmerald/conf/global_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -875,7 +875,9 @@ def template_config(self) -> TemplateConfig:
return None

@property
def static_files_config(self) -> Optional[StaticFilesConfig]:
def static_files_config(
self,
) -> Union[StaticFilesConfig, list[StaticFilesConfig], tuple[StaticFilesConfig, ...], None]:
"""
An instance of [StaticFilesConfig](https://esmerald.dev/configurations/staticfiles/).
Expand Down
3 changes: 3 additions & 0 deletions esmerald/config/static_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class StaticFilesConfig(BaseModel):
)
app = Esmerald(static_files_config=static_files_config)
# or multiple in descending priority
app = Esmerald(static_files_config=[static_files_config1, static_files_config2, ...])
```
"""

Expand Down
4 changes: 3 additions & 1 deletion esmerald/testclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,9 @@ def create_client(
openapi_version: Optional[str] = "3.1.0",
raise_server_exceptions: bool = True,
root_path: str = "",
static_files_config: Optional["StaticFilesConfig"] = None,
static_files_config: Union[
"StaticFilesConfig", list["StaticFilesConfig"], tuple["StaticFilesConfig", ...], None
] = None,
template_config: Optional["TemplateConfig"] = None,
lifespan: Optional[Callable[["Esmerald"], "AsyncContextManager"]] = None,
cookies: Optional[CookieTypes] = None,
Expand Down

0 comments on commit 2c2887f

Please sign in to comment.