Skip to content

Commit

Permalink
add router
Browse files Browse the repository at this point in the history
  • Loading branch information
StuartHarris committed Aug 15, 2024
1 parent 414914b commit 91d7ae3
Show file tree
Hide file tree
Showing 5 changed files with 196 additions and 172 deletions.
4 changes: 4 additions & 0 deletions platform-wasmcloud/restart.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env fish

./stop.fish
./start.fish
43 changes: 43 additions & 0 deletions wasm-components/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions wasm-components/rust/http-controller/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ crate-type = ["cdylib"]
anyhow = "1.0.40"
common = { path = "../common" }
form_urlencoded = "1.2.1"
routefinder = "0.5.4"
serde.workspace = true
serde_json.workspace = true
wit-bindgen.workspace = true
31 changes: 10 additions & 21 deletions wasm-components/rust/http-controller/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ impl ResponseOutparam {
}

impl IncomingRequest {
pub fn parts(&self) -> (Vec<String>, Option<String>) {
parse_path_and_query(self.path_with_query().unwrap())
}

pub fn read_body(self) -> Result<Vec<u8>> {
let incoming_req_body = self
.consume()
Expand All @@ -58,22 +54,15 @@ impl IncomingRequest {
}
}

fn parse_path_and_query(path_and_query: String) -> (Vec<String>, Option<String>) {
pub fn path_and_query(path_with_query: &str) -> (&str, Option<&str>) {
let (path, query) =
path_and_query.split_at(path_and_query.find('?').unwrap_or(path_and_query.len()));
path_with_query.split_at(path_with_query.find('?').unwrap_or(path_with_query.len()));
let query = if query.is_empty() {
None
} else {
Some(query.trim_start_matches("?").to_string())
Some(query.trim_start_matches("?"))
};

let path_parts: Vec<String> = path
.strip_prefix('/')
.map(|remainder| remainder.split('/'))
.map(|c| c.map(|s| s.to_string()).collect())
.unwrap_or_default();

(path_parts, query)
(path, query)
}

#[cfg(test)]
Expand All @@ -82,11 +71,11 @@ mod test {

#[test]
fn test_parse_path_and_query() {
let path = "/1/products?skus=sku1,sku2";

let (parts, query) = parse_path_and_query(path.to_string());

assert_eq!(parts, ["1", "products"]);
assert_eq!(query, Some("skus=sku1,sku2".to_string()));
assert_eq!(
path_and_query("/1/products?skus=sku1,sku2"),
("/1/products", Some("skus=sku1,sku2"))
);
assert_eq!(path_and_query("/products"), ("/products", None));
assert_eq!(path_and_query(""), ("", None));
}
}
Loading

0 comments on commit 91d7ae3

Please sign in to comment.