Skip to content
Draft
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
9 changes: 9 additions & 0 deletions templates/http-rust/content/Cargo.toml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ crate-type = ["cdylib"]

[dependencies]
anyhow = "1"
{%- case http-router -%}
{%- when "spin" %}
spin-sdk = { version = "6.0.0", features = ["router"] }
{%- when "axum" %}
axum = { version = "0.8.6", default-features = false, features = ["json", "macros"] }
spin-sdk = "6.0.0"
tower-service = "0.3.3"
{%- else %}
spin-sdk = "6.0.0"
{%- endcase %}

[workspace]
58 changes: 58 additions & 0 deletions templates/http-rust/content/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,60 @@
{%- case http-router -%}
{%- when "spin" -%}
// For the built-in router documentation refer to
// https://spinframework.dev/rust-components#the-spin-http-router
use spin_sdk::http::router::{Params, Router};
use spin_sdk::http::{IntoResponse, Request, StatusCode};
use spin_sdk::http_service;

/// A simple Spin HTTP component using the built-in router.
#[http_service]
async fn handle_{{project-name | snake_case}}(req: Request) -> impl IntoResponse {
let mut router = Router::new();
router.get("/", index);
router.get("/hello/:name", hello);
router.any("/*", not_found);
router.handle(req).await
}

async fn index(_req: Request, _params: Params) -> impl IntoResponse {
(StatusCode::OK, "Hello, Spin!")
}

async fn hello(_req: Request, params: Params) -> impl IntoResponse {
let name = params.get("name").unwrap_or("world").to_owned();
(StatusCode::OK, format!("Hello, {name}!"))
}

async fn not_found(_req: Request, _params: Params) -> impl IntoResponse {
(StatusCode::NOT_FOUND, "Not Found")
}
{%- when "axum" -%}
// For Axum documentation refer to https://docs.rs/axum/latest/axum/
use axum::extract::Path;
use axum::routing::get;
use axum::Router;
use spin_sdk::http::{IntoResponse, Request};
use spin_sdk::http_service;
use tower_service::Service;

/// A simple Spin HTTP component using the Axum router.
#[http_service]
async fn handle_{{project-name | snake_case}}(req: Request) -> impl IntoResponse {
Router::new()
.route("/", get(index))
.route("/hello/{name}", get(hello))
.call(req)
.await
}

async fn index() -> &'static str {
"Hello, Spin!"
}

async fn hello(Path(name): Path<String>) -> String {
format!("Hello, {name}!")
}
{%- else -%}
use spin_sdk::http::{IntoResponse, Request, Response};
use spin_sdk::http_service;

Expand All @@ -10,3 +67,4 @@ async fn handle_{{project-name | snake_case}}(req: Request) -> anyhow::Result<im
.header("content-type", "text/plain")
.body("Hello World!".to_string()))
}
{%- endcase %}
7 changes: 6 additions & 1 deletion templates/http-rust/metadata/spin-template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,9 @@ component = "component.txt"

[parameters]
project-description = { type = "string", prompt = "Description", default = "" }
http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" }
http-path = { type = "string", prompt = "HTTP path", default = "/...", pattern = "^/\\S*$" }
http-router = { type = "string", prompt = "HTTP Router", default = "none", allowed_values = [
"spin",
"axum",
"none",
] }
Loading