Skip to content

Commit 5aba90a

Browse files
committed
create routes
1 parent 91a883f commit 5aba90a

File tree

3 files changed

+121
-11
lines changed

3 files changed

+121
-11
lines changed

Cargo.lock

Lines changed: 67 additions & 0 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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ proxy = true
2929
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
3030

3131
[dependencies]
32+
url = "2.5.2"
3233
wasi = "0.13.2"
3334
wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }

src/lib.rs

Lines changed: 53 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use url::Url;
12
pub use wasi::http::types::{
23
Fields, IncomingRequest, OutgoingBody, OutgoingResponse, ResponseOutparam,
34
};
@@ -6,18 +7,59 @@ struct Component;
67
wasi::http::proxy::export!(Component);
78

89
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");
10+
fn handle(req: IncomingRequest, outparam: ResponseOutparam) {
11+
let url = req.path_with_query().unwrap();
12+
let url = Url::parse(&url).expect("could not parse the URL");
13+
http_home(req, outparam);
14+
// dbg!(&url);
15+
// match url.path() {
16+
// "/wait" => http_wait(req, outparam),
17+
// // "/echo" => {} // TODO
18+
// // "/host" => {} // TODO
19+
// "/" | _ => http_home(req, outparam),
20+
// }
21+
}
22+
}
1323

14-
ResponseOutparam::set(outparam, Ok(resp));
24+
fn http_home(_req: IncomingRequest, outparam: ResponseOutparam) {
25+
let headers = Fields::new();
26+
let res = OutgoingResponse::new(headers);
27+
let body = res.body().expect("outgoing response");
1528

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");
29+
ResponseOutparam::set(outparam, Ok(res));
1930

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

0 commit comments

Comments
 (0)