Skip to content

Commit

Permalink
Merge pull request #1803 from lann/http-base-default
Browse files Browse the repository at this point in the history
http: Make `base = "/"` the default
  • Loading branch information
lann authored Sep 25, 2023
2 parents 8858603 + d041cc0 commit 76636a8
Show file tree
Hide file tree
Showing 60 changed files with 69 additions and 91 deletions.
27 changes: 3 additions & 24 deletions crates/doctor/src/manifest/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ pub enum TriggerDiagnosis {
MissingAppTrigger,
/// Invalid app trigger config
InvalidAppTrigger(&'static str),
/// HTTP trigger missing base field
HttpAppTriggerMissingBase,
/// HTTP component trigger missing route field
HttpComponentTriggerMissingRoute(String, bool),
/// Invalid HTTP component trigger config
Expand All @@ -76,12 +74,9 @@ impl TriggerDiagnosis {
let Some(trigger_type) = trigger.get("type") else {
return Some(Self::InvalidAppTrigger("trigger table missing type"));
};
let Some(trigger_type) = trigger_type.as_str() else {
let Some(_) = trigger_type.as_str() else {
return Some(Self::InvalidAppTrigger("type must be a string"));
};
if trigger_type == "http" && trigger.get("base").is_none() {
return Some(Self::HttpAppTriggerMissingBase);
}
None
}

Expand Down Expand Up @@ -116,7 +111,6 @@ impl Diagnosis for TriggerDiagnosis {
Self::InvalidAppTrigger(msg) => {
format!("Invalid app trigger config: {msg}")
}
Self::HttpAppTriggerMissingBase => "http trigger config missing base".into(),
Self::HttpComponentTriggerMissingRoute(id, _) => {
format!("HTTP component {id:?} missing trigger.route")
}
Expand All @@ -128,7 +122,7 @@ impl Diagnosis for TriggerDiagnosis {

fn treatment(&self) -> Option<&dyn Treatment> {
match self {
Self::MissingAppTrigger | Self::HttpAppTriggerMissingBase => Some(self),
Self::MissingAppTrigger => Some(self),
// We can reasonably fill in default "route" iff there is only one component
Self::HttpComponentTriggerMissingRoute(_, single_component) if *single_component => {
Some(self)
Expand All @@ -143,9 +137,6 @@ impl ManifestTreatment for TriggerDiagnosis {
fn summary(&self) -> String {
match self {
TriggerDiagnosis::MissingAppTrigger => "Add default HTTP trigger config".into(),
TriggerDiagnosis::HttpAppTriggerMissingBase => {
"Set default HTTP trigger base '/'".into()
}
TriggerDiagnosis::HttpComponentTriggerMissingRoute(id, _) => {
format!("Set trigger.route '/...' for component {id:?}")
}
Expand All @@ -155,7 +146,7 @@ impl ManifestTreatment for TriggerDiagnosis {

async fn treat_manifest(&self, doc: &mut Document) -> anyhow::Result<()> {
match self {
Self::MissingAppTrigger | Self::HttpAppTriggerMissingBase => {
Self::MissingAppTrigger => {
// Get or insert missing trigger config
if doc.get("trigger").is_none() {
doc.insert("trigger", Item::Value(InlineTable::new().into()));
Expand All @@ -175,8 +166,6 @@ impl ManifestTreatment for TriggerDiagnosis {
decor.set_suffix(suffix.to_string().trim());
}
}
// Set missing "base"
trigger.entry("base").or_insert(Item::Value("/".into()));
}
}
Self::HttpComponentTriggerMissingRoute(_, true) => {
Expand Down Expand Up @@ -230,16 +219,6 @@ mod tests {
assert!(matches!(diag, TriggerDiagnosis::MissingAppTrigger));
}

#[tokio::test]
async fn test_http_app_trigger_missing_base() {
let diag = run_broken_test::<TriggerDiagnostic>(
"manifest_trigger",
"http_app_trigger_missing_base",
)
.await;
assert!(matches!(diag, TriggerDiagnosis::HttpAppTriggerMissingBase));
}

#[tokio::test]
async fn test_http_component_trigger_missing_route() {
let diag = run_broken_test::<TriggerDiagnostic>(
Expand Down
2 changes: 1 addition & 1 deletion crates/doctor/tests/data/manifest_trigger_correct.toml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
trigger = { type = "http", base = "/" }
trigger = { type = "http" }

[[component]]
id = "http-component"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
trigger = { type = "http", base = "/" }
trigger = { type = "http" }

[[component]]
id = "http-component"
5 changes: 5 additions & 0 deletions crates/http/src/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,10 @@ pub struct Metadata {
// The type of trigger which should always been "http" in this case
pub r#type: String,
// The based url
#[serde(default = "default_base")]
pub base: String,
}

pub fn default_base() -> String {
"/".into()
}
2 changes: 1 addition & 1 deletion crates/loader/src/local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ source = "nonexistent.wasm"
r#"
spin_version = "1"
name = "test"
trigger = {{ type = "http", base = "/" }}
trigger = {{ type = "http" }}
version = "0.0.1"
[variables]
Expand Down
6 changes: 3 additions & 3 deletions crates/loader/src/local/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,14 @@ fn test_unknown_version_is_rejected() {
}

#[tokio::test]
async fn gives_correct_error_when_missing_app_trigger_field() -> Result<()> {
const MANIFEST: &str = "tests/missing-http-base.toml";
async fn gives_correct_error_for_invalid_app_trigger_field() -> Result<()> {
const MANIFEST: &str = "tests/invalid-http-base.toml";

let app = raw_manifest_from_file(&PathBuf::from(MANIFEST)).await;

let e = format!("{:#}", app.unwrap_err());
assert!(
e.contains("missing field `base`"),
e.contains("expected a string for key `base`"),
"Expected error to contain trigger field information but was '{e}'"
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spin_version = "1"
name = "spin-hello-world-duplicate"
version = "1.0.0"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }

[[component]]
id = "hello"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
spin_version = "1"
authors = ["Gul Madred", "Edward Jellico", "JL"]
description = "A HTTP application without a base"
name = "missing-http-base"
trigger = {type = "http"}
description = "A HTTP application with an invalid base"
name = "invalid-http-base"
trigger = { type = "http", base = 7 }
version = "6.11.2"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion crates/loader/tests/invalid-manifest-duplicate-id.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spin_version = "1"
name = "spin-hello-world-duplicate"
version = "1.0.0"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }

[[component]]
id = "hello"
Expand Down
2 changes: 1 addition & 1 deletion crates/loader/tests/invalid-manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ spin_version = "1"
authors = ["Gul Madred", "Edward Jellico", "JL"]
description = "A simple application that returns the number of lights"
name = "chain-of-command"
trigger = {type = "http", base = "/"}
trigger = {type = "http"}
ver
2 changes: 1 addition & 1 deletion crates/loader/tests/invalid-url-in-allowed-http-hosts.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spin_version = "1"
name = "spin-hello-world-duplicate"
version = "1.0.0"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }

[[component]]
id = "hello"
Expand Down
2 changes: 1 addition & 1 deletion crates/loader/tests/invalid-version.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_version = "77.301.12"
authors = ["Gul Madred", "Edward Jellico", "JL"]
description = "Because if we get to API version 77.301.12 then we have bigger problems"
name = "chain-of-command"
trigger = {type = "http", base = "/"}
trigger = {type = "http"}
version = "6.11.2"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion crates/loader/tests/valid-manifest.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_version = "1"
authors = ["Gul Madred", "Edward Jellico", "JL"]
description = "A simple application that returns the number of lights"
name = "chain-of-command"
trigger = {type = "http", base = "/"}
trigger = {type = "http"}
version = "6.11.2"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion crates/loader/tests/valid-with-files/spin.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spin_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
name = "spin-local-source-test"
trigger = {type = "http", base = "/"}
trigger = {type = "http"}
version = "1.0.0"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion crates/loader/tests/wagi-custom-entrypoint.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name = "spin-wagi-custom-entrypoint"
spin_version = "1"
version = "1.0.0"
authors = [ "Fermyon Engineering <[email protected]>" ]
trigger = { type = "http", base = "/" }
trigger = { type = "http" }

[[component]]
source = "spin-fs.wasm"
Expand Down
1 change: 1 addition & 0 deletions crates/manifest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ impl From<ApplicationTrigger> for ApplicationTriggerSerialised {

/// HTTP trigger configuration.
#[derive(Clone, Debug, Deserialize, PartialEq, Eq, Serialize)]
#[serde(default)]
pub struct HttpTriggerConfiguration {
/// Base path for the HTTP application.
pub base: String,
Expand Down
2 changes: 1 addition & 1 deletion crates/trigger/src/locked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ mod tests {
spin_version = "1"
name = "test-app"
version = "0.0.1"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
[variables]
test_var = { default = "test-val" }
Expand Down
2 changes: 1 addition & 1 deletion docs/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name = "spin-docs"
version = "0.1.0"
description = "The Spin documentation website running on... Spin."
authors = [ "Fermyon Engineering <[email protected]>" ]
trigger = { type = "http", base = "/" }
trigger = { type = "http" }

[[component]]
id = "redirect-download"
Expand Down
2 changes: 1 addition & 1 deletion examples/config-rust/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_manifest_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
description = "A Spin Rust application demonstrating the config sdk."
name = "spin-config-rust"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "0.1.0"

[variables]
Expand Down
2 changes: 1 addition & 1 deletion examples/config-tinygo/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_manifest_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
description = "A simple Spin application written in (Tiny)Go."
name = "spin-config-tinygo"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "1.0.0"

[variables]
Expand Down
2 changes: 1 addition & 1 deletion examples/http-cpp/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_manifest_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
description = "A simple application that returns hello."
name = "spin-hello-world"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "1.0.0"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion examples/http-rust-outbound-http/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_manifest_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
description = "Demonstrates outbound HTTP calls"
name = "spin-outbound-http"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "1.0.0"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion examples/http-rust-router-macro/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_manifest_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
description = "An application that demonstrates HTTP routing."
name = "spin-rust-router"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "1.0.0"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion examples/http-rust-router/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_manifest_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
description = "An application that demonstrates HTTP routing."
name = "spin-rust-router"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "1.0.0"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion examples/http-rust/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_manifest_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
description = "A simple application that returns hello."
name = "spin-hello-world"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "1.0.0"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion examples/http-tinygo-outbound-http/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_manifest_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
description = "A simple Spin application written in (Tiny)Go that performs outbound HTTP requests."
name = "spin-tinygo-outbound-http"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "1.0.0"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion examples/http-tinygo-router/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ apiVersion = "0.1.0"
authors = ["Fermyon Engineering <[email protected]>"]
description = "A simple Spin application written in (Tiny)Go."
name = "spin-hello-world"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "1.0.0"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion examples/http-tinygo-router/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_manifest_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
description = "A simple Spin application written in (Tiny)Go."
name = "spin-hello-tinygo"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "1.0.0"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion examples/http-tinygo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ apiVersion = "0.1.0"
authors = ["Fermyon Engineering <[email protected]>"]
description = "A simple Spin application written in (Tiny)Go."
name = "spin-hello-world"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "1.0.0"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion examples/http-tinygo/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_manifest_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
description = "A simple Spin application written in (Tiny)Go."
name = "spin-hello-tinygo"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "1.0.0"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion examples/rust-key-value/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_manifest_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
description = "A simple application that exercises key-value storage."
name = "spin-key-value"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "1.0.0"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion examples/rust-outbound-mysql/spin.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ spin_manifest_version = "1"
authors = ["itowlson <[email protected]>"]
description = "Demo of calling MySQL from a Spin application"
name = "rust-outbound-mysql"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "0.1.0"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion examples/rust-outbound-pg/spin.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spin_manifest_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
name = "rust-outbound-pg-example"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "0.1.0"

[[component]]
Expand Down
2 changes: 1 addition & 1 deletion examples/rust-outbound-redis/spin.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
spin_manifest_version = "1"
authors = ["Fermyon Engineering <[email protected]>"]
name = "rust-outbound-redis-example"
trigger = { type = "http", base = "/" }
trigger = { type = "http" }
version = "0.1.0"

[[component]]
Expand Down
Loading

0 comments on commit 76636a8

Please sign in to comment.