Skip to content
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

feat(http-server): Add WASI reactor support to http-server cap #359

Merged
merged 7 commits into from
Mar 15, 2023
Merged

Conversation

Mossaka
Copy link
Member

@Mossaka Mossaka commented Mar 14, 2023

This PR switches the WASI command model to reactor model for wasi-http. The reason for that is in the command model, each exported HTTP handler function would not be able to initialize properly with the WASI stdout because the host will not invoke _start. The reactor solves this issue.

Breaking changes

  1. rust slight applications that use http-server must use cargo new --lib to create the rust program, instead of --bin. This will create a rust library. In Cargo.toml, the following attribute is needed:
[lib]
crate-type = ["cdylib"]
  1. Use slight provided slight_http_server_macro::on_server_init macro on top of the router and server setup function.

A rust slight program that uses http-server capability would end up looking like

use http_server::*;
use slight_http_handler_macro::register_handler;
use slight_http_server_macro::on_server_init;

wit_bindgen_rust::import!("../../wit/http-server.wit");
wit_bindgen_rust::export!("../../wit/http-server-export.wit");
wit_bindgen_rust::import!("../../wit/http-client.wit");
wit_error_rs::impl_error!(http_server::HttpRouterError);
wit_error_rs::impl_error!(http_client::HttpError);

#[on_server_init]
fn main() -> Result<()> {
    let router = Router::new()?;
    let router_with_route = router
        .get("/hello", "handle_hello")?
        .get("/person/:name", "handle_person_with_name")?
        .get("/foo", "handle_foo")?
        .put("/bar", "handle_bar")?
        .post("/upload", "upload")?
        .delete("/delete-file", "delete_file_handler")?
        .get("/request", "handle_request")?;

    println!("guest starting server");
    let _ = Server::serve("0.0.0.0:3000", &router_with_route)?;
    // server.stop().unwrap();
    println!("guest moving on");

    Ok(())
}

A Note to Reviewers

I've tested that the http handler would work with keyvalue capability correctly. I still need to add more docs to this newly added macro and changes to the slight runtime library, but other than that, this PR is ready to review.

@Mossaka Mossaka added this to the [KUBECON] March Milestone milestone Mar 14, 2023
@Mossaka Mossaka added the ✨ feature New feature or feature request label Mar 14, 2023
Mossaka and others added 4 commits March 14, 2023 10:46
Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>
Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>
Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>
Signed-off-by: David Justice <david@devigned.com>
Mossaka and others added 3 commits March 14, 2023 08:44
Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>
Signed-off-by: Jiaxiao Zhou <jiazho@microsoft.com>
Signed-off-by: David Justice <david@devigned.com>
Copy link
Member

@devigned devigned left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm! I've tested it in deislabs/containerd-wasm-shims#80 and pods are displaying log messages from the HTTP handlers.

I'm good with pulling this in and having a follow up PR for docs. Up to you, @Mossaka.

@Mossaka Mossaka merged commit 74f0009 into main Mar 15, 2023
@Mossaka Mossaka deleted the reactor branch March 15, 2023 14:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ feature New feature or feature request
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants