Skip to content

Commit

Permalink
feat: make the folder configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
chesedo committed Dec 2, 2022
1 parent 961964a commit 1c75dfd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 6 deletions.
22 changes: 22 additions & 0 deletions resources/static-folder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,25 @@ This plugin allows services to get the path to a static folder at runtime
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.

An example using the Axum framework can be found on [GitHub](https://github.com/shuttle-hq/examples/tree/main/axum/websocket)

``` rust
#[shuttle_service::main]
async fn main(
#[shuttle_static_folder::StaticFolder] static_folder: PathBuf,
) -> __ { ... }
```

### Parameters
| Parameter | Type | Default | Description |
|-----------|------|----------|--------------------------------------------------------------------|
| folder | str | `static` | The folder relative to the crate root to make a static folder for. |

### Example: Using the public folder instead
Since this plugin defaults to the `static` folder, the arguments can be used to use the `public` folder instead.

``` rust
#[shuttle_service::main]
async fn main(
#[shuttle_static_folder::StaticFolder(folder = "public")] public_folder: PathBuf,
) -> __ { ... }
```
23 changes: 17 additions & 6 deletions resources/static-folder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,32 @@ use shuttle_service::{Factory, ResourceBuilder};
use std::{fs::rename, path::PathBuf};
use tokio::runtime::Runtime;

pub struct StaticFolder;
pub struct StaticFolder<'a> {
/// The folder to reach at runtime. Defaults to `static`
folder: &'a str,
}

impl<'a> StaticFolder<'a> {
pub fn folder(mut self, folder: &'a str) -> Self {
self.folder = folder;

self
}
}

#[async_trait]
impl ResourceBuilder<PathBuf> for StaticFolder {
impl<'a> ResourceBuilder<PathBuf> for StaticFolder<'a> {
fn new() -> Self {
Self {}
Self { folder: "static" }
}

async fn build(
self,
factory: &mut dyn Factory,
_runtime: &Runtime,
) -> Result<PathBuf, shuttle_service::Error> {
let input_dir = factory.get_build_path()?.join("static");
let output_dir = factory.get_storage_path()?.join("static");
let input_dir = factory.get_build_path()?.join(self.folder);
let output_dir = factory.get_storage_path()?.join(self.folder);

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

Expand Down Expand Up @@ -89,7 +100,7 @@ mod tests {
assert!(!expected_file.exists(), "input file should not exist yet");

// Call plugin
let static_folder = StaticFolder;
let static_folder = StaticFolder::new();

let runtime = tokio::runtime::Runtime::new().unwrap();
let actual_folder = static_folder.build(&mut factory, &runtime).await.unwrap();
Expand Down

0 comments on commit 1c75dfd

Please sign in to comment.