Skip to content

Routes #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 16, 2024
Merged
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
10 changes: 5 additions & 5 deletions Cargo.lock

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

17 changes: 1 addition & 16 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,6 @@ package = "component:rust-wasi-hello"
# `get-environment` or other things not present in the proxy world.
proxy = true

[package.metadata.component.dependencies]

[package.metadata.component.target]

[package.metadata.component.target.dependencies]
"wasi:http" = { path = "wit/deps/http" }
"wasi:clocks" = { path = "wit/deps/clocks" }
"wasi:io" = { path = "wit/deps/io" }
"wasi:random" = { path = "wit/deps/random" }
"wasi:cli" = { path = "wit/deps/cli" }
"wasi:filesystem" = { path = "wit/deps/filesystem" }
"wasi:sockets" = { path = "wit/deps/sockets" }

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
wasi = "0.13.2"
wit-bindgen-rt = { version = "0.24.0", features = ["bitflags"] }
wit-bindgen-rt = { version = "0.30.0", features = ["bitflags"] }
59 changes: 48 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,55 @@ struct Component;
wasi::http::proxy::export!(Component);

impl wasi::exports::http::incoming_handler::Guest for Component {
fn handle(_request: IncomingRequest, outparam: ResponseOutparam) {
let hdrs = Fields::new();
let resp = OutgoingResponse::new(hdrs);
let body = resp.body().expect("outgoing response");
fn handle(req: IncomingRequest, outparam: ResponseOutparam) {
match req.path_with_query().unwrap().as_str() {
"/wait" => http_wait(req, outparam),
// "/echo" => {} // TODO
// "/host" => {} // TODO
"/" | _ => http_home(req, outparam),
}
}
}

ResponseOutparam::set(outparam, Ok(resp));
fn http_home(_req: IncomingRequest, outparam: ResponseOutparam) {
let headers = Fields::new();
let res = OutgoingResponse::new(headers);
let body = res.body().expect("outgoing response");

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

drop(out);
OutgoingBody::finish(body, None).unwrap();
}
let out = body.write().expect("outgoing stream");
out.blocking_write_and_flush(b"Hello, wasi:http/proxy world!\n")
.expect("writing response");

drop(out);
OutgoingBody::finish(body, None).unwrap();
}

fn http_wait(_req: IncomingRequest, outparam: ResponseOutparam) {
// Get the time now
let now = wasi::clocks::monotonic_clock::now();

// Sleep for 1 second
let nanos = 1_000_000_000;
let pollable = wasi::clocks::monotonic_clock::subscribe_duration(nanos);
pollable.block();

// Compute how long we slept for.
let elapsed = wasi::clocks::monotonic_clock::now() - now;
let elapsed = elapsed / 1_000_000; // change to millis

let headers = Fields::new();
let res = OutgoingResponse::new(headers);
let body = res.body().expect("outgoing response");

ResponseOutparam::set(outparam, Ok(res));

let out = body.write().expect("outgoing stream");
let msg = format!("slept for {elapsed} millis\n");
out.blocking_write_and_flush(msg.as_bytes())
.expect("writing response");

drop(out);
OutgoingBody::finish(body, None).unwrap();
}
Loading