From 1c75dfdbd6b2e34caa646c8d1974264693d820f7 Mon Sep 17 00:00:00 2001 From: chesedo Date: Fri, 2 Dec 2022 10:40:18 +0200 Subject: [PATCH 1/2] feat: make the folder configurable --- resources/static-folder/README.md | 22 ++++++++++++++++++++++ resources/static-folder/src/lib.rs | 23 +++++++++++++++++------ 2 files changed, 39 insertions(+), 6 deletions(-) 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(); From e76543a02fe8d339832c38a61234c6606b315952 Mon Sep 17 00:00:00 2001 From: Pieter Date: Mon, 5 Dec 2022 11:16:16 +0200 Subject: [PATCH 2/2] Update resources/static-folder/README.md Co-authored-by: Damien --- resources/static-folder/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resources/static-folder/README.md b/resources/static-folder/README.md index 7511c43f0..48e27ba32 100644 --- a/resources/static-folder/README.md +++ b/resources/static-folder/README.md @@ -16,7 +16,7 @@ async fn main( ### Parameters | Parameter | Type | Default | Description | |-----------|------|----------|--------------------------------------------------------------------| -| folder | str | `static` | The folder relative to the crate root to make a static folder for. | +| 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.