Skip to content

Commit ecef0ae

Browse files
authored
Merge pull request #11 from yoshuawuyts/routes
Routes
2 parents 91a883f + 194b1a2 commit ecef0ae

File tree

3 files changed

+54
-32
lines changed

3 files changed

+54
-32
lines changed

Cargo.lock

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,6 @@ package = "component:rust-wasi-hello"
1313
# `get-environment` or other things not present in the proxy world.
1414
proxy = true
1515

16-
[package.metadata.component.dependencies]
17-
18-
[package.metadata.component.target]
19-
20-
[package.metadata.component.target.dependencies]
21-
"wasi:http" = { path = "wit/deps/http" }
22-
"wasi:clocks" = { path = "wit/deps/clocks" }
23-
"wasi:io" = { path = "wit/deps/io" }
24-
"wasi:random" = { path = "wit/deps/random" }
25-
"wasi:cli" = { path = "wit/deps/cli" }
26-
"wasi:filesystem" = { path = "wit/deps/filesystem" }
27-
"wasi:sockets" = { path = "wit/deps/sockets" }
28-
29-
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
30-
3116
[dependencies]
3217
wasi = "0.13.2"
33-
wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
18+
wit-bindgen-rt = { version = "0.30.0", features = ["bitflags"] }

src/lib.rs

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,55 @@ struct Component;
66
wasi::http::proxy::export!(Component);
77

88
impl wasi::exports::http::incoming_handler::Guest for Component {
9-
fn handle(_request: IncomingRequest, outparam: ResponseOutparam) {
10-
let hdrs = Fields::new();
11-
let resp = OutgoingResponse::new(hdrs);
12-
let body = resp.body().expect("outgoing response");
9+
fn handle(req: IncomingRequest, outparam: ResponseOutparam) {
10+
match req.path_with_query().unwrap().as_str() {
11+
"/wait" => http_wait(req, outparam),
12+
// "/echo" => {} // TODO
13+
// "/host" => {} // TODO
14+
"/" | _ => http_home(req, outparam),
15+
}
16+
}
17+
}
1318

14-
ResponseOutparam::set(outparam, Ok(resp));
19+
fn http_home(_req: IncomingRequest, outparam: ResponseOutparam) {
20+
let headers = Fields::new();
21+
let res = OutgoingResponse::new(headers);
22+
let body = res.body().expect("outgoing response");
1523

16-
let out = body.write().expect("outgoing stream");
17-
out.blocking_write_and_flush(b"Hello, wasi:http/proxy world!\n")
18-
.expect("writing response");
24+
ResponseOutparam::set(outparam, Ok(res));
1925

20-
drop(out);
21-
OutgoingBody::finish(body, None).unwrap();
22-
}
26+
let out = body.write().expect("outgoing stream");
27+
out.blocking_write_and_flush(b"Hello, wasi:http/proxy world!\n")
28+
.expect("writing response");
29+
30+
drop(out);
31+
OutgoingBody::finish(body, None).unwrap();
32+
}
33+
34+
fn http_wait(_req: IncomingRequest, outparam: ResponseOutparam) {
35+
// Get the time now
36+
let now = wasi::clocks::monotonic_clock::now();
37+
38+
// Sleep for 1 second
39+
let nanos = 1_000_000_000;
40+
let pollable = wasi::clocks::monotonic_clock::subscribe_duration(nanos);
41+
pollable.block();
42+
43+
// Compute how long we slept for.
44+
let elapsed = wasi::clocks::monotonic_clock::now() - now;
45+
let elapsed = elapsed / 1_000_000; // change to millis
46+
47+
let headers = Fields::new();
48+
let res = OutgoingResponse::new(headers);
49+
let body = res.body().expect("outgoing response");
50+
51+
ResponseOutparam::set(outparam, Ok(res));
52+
53+
let out = body.write().expect("outgoing stream");
54+
let msg = format!("slept for {elapsed} millis\n");
55+
out.blocking_write_and_flush(msg.as_bytes())
56+
.expect("writing response");
57+
58+
drop(out);
59+
OutgoingBody::finish(body, None).unwrap();
2360
}

0 commit comments

Comments
 (0)