diff --git a/crates/templates/src/manager.rs b/crates/templates/src/manager.rs index 1759d8d25d..9af08fbdfd 100644 --- a/crates/templates/src/manager.rs +++ b/crates/templates/src/manager.rs @@ -615,7 +615,7 @@ mod tests { } } - const TPLS_IN_THIS: usize = 12; + const TPLS_IN_THIS: usize = 13; #[tokio::test] async fn can_install_into_new_directory() { diff --git a/templates/http-middleware/content/.gitignore b/templates/http-middleware/content/.gitignore new file mode 100644 index 0000000000..06df055bbf --- /dev/null +++ b/templates/http-middleware/content/.gitignore @@ -0,0 +1,2 @@ +target/ +.spin/ \ No newline at end of file diff --git a/templates/http-middleware/content/Cargo.toml.tmpl b/templates/http-middleware/content/Cargo.toml.tmpl new file mode 100644 index 0000000000..f2eed7b83a --- /dev/null +++ b/templates/http-middleware/content/Cargo.toml.tmpl @@ -0,0 +1,16 @@ +[package] +name = "{{project-name | kebab_case}}" +authors = ["{{authors}}"] +description = "{{project-description}}" +version = "0.1.0" +rust-version = "1.93" +edition = "2024" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +anyhow = "1" +spin-sdk = "6.0.0" + +[workspace] \ No newline at end of file diff --git a/templates/http-middleware/content/spin.toml b/templates/http-middleware/content/spin.toml new file mode 100644 index 0000000000..83453e7d44 --- /dev/null +++ b/templates/http-middleware/content/spin.toml @@ -0,0 +1,14 @@ +spin_manifest_version = 2 + +[application] +name = "{{project-name | kebab_case}}" +version = "0.1.0" +authors = ["{{authors}}"] +description = "{{project-description}}" + +[component.{{project-name | kebab_case}}] +source = "target/wasm32-wasip2/release/{{project-name | snake_case}}.wasm" +allowed_outbound_hosts = [] +[component.{{project-name | kebab_case}}.build] +command = "cargo build --target wasm32-wasip2 --release" +watch = ["src/**/*.rs", "Cargo.toml"] \ No newline at end of file diff --git a/templates/http-middleware/content/src/lib.rs b/templates/http-middleware/content/src/lib.rs new file mode 100644 index 0000000000..3ea4c044a2 --- /dev/null +++ b/templates/http-middleware/content/src/lib.rs @@ -0,0 +1,19 @@ +use spin_sdk::http::{self, HeaderName, HeaderValue, Request, Response}; +use spin_sdk::http_service; + +#[http_service] +async fn handle(mut req: Request) -> http::Result { + // Request runs on the way in, before the next handler. + eprintln!("[middleware] --> {} {}", req.method(), req.uri().path()); + + // MIDDLEWARE LOGIC GOES HERE + + // Forward the (modified) request to the next handler in the chain and wait + // for its response. + let mut resp = http::next(req).await?; + + // Response runs on the way out, after the next handler + eprintln!("[middleware] <-- {}", resp.status()); + + Ok(resp) +} \ No newline at end of file diff --git a/templates/http-middleware/metadata/snippets/component.txt b/templates/http-middleware/metadata/snippets/component.txt new file mode 100644 index 0000000000..fa8728b642 --- /dev/null +++ b/templates/http-middleware/metadata/snippets/component.txt @@ -0,0 +1,7 @@ +[component.{{project-name | kebab_case}}] +source = "{{ output-path }}/target/wasm32-wasip2/release/{{project-name | snake_case}}.wasm" +allowed_outbound_hosts = [] +[component.{{project-name | kebab_case}}.build] +command = "cargo build --target wasm32-wasip2 --release" +workdir = "{{ output-path }}" +watch = ["src/**/*.rs", "Cargo.toml"] \ No newline at end of file diff --git a/templates/http-middleware/metadata/spin-template.toml b/templates/http-middleware/metadata/spin-template.toml new file mode 100644 index 0000000000..c87e6d8075 --- /dev/null +++ b/templates/http-middleware/metadata/spin-template.toml @@ -0,0 +1,17 @@ +manifest_version = "1" +id = "http-middleware" +description = "http middleware rust template" +tags = [] + +[new_application] +supported = false + +[add_component] +skip_files = ["spin.toml"] +[add_component.snippets] +component = "component.txt" + +[parameters] +# These are placehodlers for the template parameters +# You can add more parameters here, or remove them if you don't need them. +project-description = { type = "string", prompt = "Description", default = "" } \ No newline at end of file