Skip to content

Commit 229c6d1

Browse files
committed
feat: make the folder configurable
1 parent 961964a commit 229c6d1

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

resources/static-folder/README.md

+22
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,25 @@ This plugin allows services to get the path to a static folder at runtime
55
Add `shuttle-static-folder` to the dependencies for your service. This resource can be using by the `shuttle_static_folder::StaticFolder` attribute to get a `PathBuf` with the location of the static folder.
66

77
An example using the Axum framework can be found on [GitHub](https://github.com/shuttle-hq/examples/tree/main/axum/websocket)
8+
9+
``` rust
10+
#[shuttle_service::main]
11+
async fn main(
12+
#[shuttle_static_folder::StaticFolder] static_folder: PathBuf,
13+
) -> __ { ... }
14+
```
15+
16+
### Parameters
17+
| Parameter | Type | Default | Description |
18+
|-----------|------|----------|--------------------------------------------------------------------|
19+
| folder | str | `static` | The folder relative to the crate root to make a static folder for. |
20+
21+
### Example: Using the public folder instead
22+
Since this plugin defaults to the `static` folder, the arguments can be used to use the `public` folder instead.
23+
24+
``` rust
25+
#[shuttle_service::main]
26+
async fn main(
27+
#[shuttle_static_folder::StaticFolder(folder = "public")] public_folder: PathBuf,
28+
) -> __ { ... }
29+
```

resources/static-folder/src/lib.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,32 @@ use shuttle_service::{Factory, ResourceBuilder};
33
use std::{fs::rename, path::PathBuf};
44
use tokio::runtime::Runtime;
55

6-
pub struct StaticFolder;
6+
pub struct StaticFolder<'a> {
7+
/// The folder to reach at runtime. Defaults to `static`
8+
folder: &'a str,
9+
}
10+
11+
impl<'a> StaticFolder<'a> {
12+
pub fn folder(mut self, folder: &'a str) -> Self {
13+
self.folder = folder;
14+
15+
self
16+
}
17+
}
718

819
#[async_trait]
9-
impl ResourceBuilder<PathBuf> for StaticFolder {
20+
impl<'a> ResourceBuilder<PathBuf> for StaticFolder<'a> {
1021
fn new() -> Self {
11-
Self {}
22+
Self { folder: "static" }
1223
}
1324

1425
async fn build(
1526
self,
1627
factory: &mut dyn Factory,
1728
_runtime: &Runtime,
1829
) -> Result<PathBuf, shuttle_service::Error> {
19-
let input_dir = factory.get_build_path()?.join("static");
20-
let output_dir = factory.get_storage_path()?.join("static");
30+
let input_dir = factory.get_build_path()?.join(self.folder);
31+
let output_dir = factory.get_storage_path()?.join(self.folder);
2132

2233
rename(input_dir, output_dir.clone())?;
2334

0 commit comments

Comments
 (0)