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

feat: make the folder configurable #508

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 relative path, from the crate root, to the directory containing static files to deploy |

### 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