diff --git a/resources/static-folder/README.md b/resources/static-folder/README.md index ae28f8e76..7511c43f0 100644 --- a/resources/static-folder/README.md +++ b/resources/static-folder/README.md @@ -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, +) -> __ { ... } +``` diff --git a/resources/static-folder/src/lib.rs b/resources/static-folder/src/lib.rs index 6cc620c04..435a6e9d7 100644 --- a/resources/static-folder/src/lib.rs +++ b/resources/static-folder/src/lib.rs @@ -3,12 +3,23 @@ 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 for StaticFolder { +impl<'a> ResourceBuilder for StaticFolder<'a> { fn new() -> Self { - Self {} + Self { folder: "static" } } async fn build( @@ -16,8 +27,8 @@ impl ResourceBuilder for StaticFolder { factory: &mut dyn Factory, _runtime: &Runtime, ) -> Result { - 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())?; @@ -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();