Skip to content

Commit

Permalink
wacker: support serving an HTTP WebAssembly module
Browse files Browse the repository at this point in the history
  • Loading branch information
iawia002 committed Feb 2, 2024
1 parent 8b12bd0 commit 2fcb48a
Show file tree
Hide file tree
Showing 14 changed files with 856 additions and 240 deletions.
509 changes: 338 additions & 171 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ license = "Apache-2.0"
[workspace.dependencies]
wacker = { path = "wacker", version = "0.2.1" }

anyhow = "1.0.76"
anyhow = "1.0.77"
clap = { version = "4.4.11", features = ["derive"] }
log = "0.4.20"
tokio = { version = "1.35.1", features = ["rt", "rt-multi-thread", "macros"] }
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,20 @@ $ wacker run time.wasm

Where `hello.wasm` is a simple WASM program that prints out `Hello World!` and exits, and `time.wasm` is a long-running program that constantly prints out the current time.

Serve an HTTP WebAssembly module:

```
$ wacker serve hello_wasi_http.wasm --addr 127.0.0.1:8081
```

List running modules:

```
$ wacker list
ID PATH STATUS
hello-w0AqXnf hello.wasm Finished
time-xhQVmjU time.wasm Running
ID PATH STATUS ADDRESS
hello-w0AqXnf hello.wasm Finished
time-xhQVmjU time.wasm Running
hello_wasi_http-luf1vz6 hello_wasi_http.wasm Running 127.0.0.1:8081
```

Fetch the logs:
Expand Down Expand Up @@ -74,6 +81,7 @@ Usage: wacker <COMMAND>
Commands:
run Runs a WebAssembly module
serve Serves an HTTP WebAssembly module
list Lists running WebAssembly modules [aliases: ps]
stop Stops a WebAssembly module
restart Restarts a WebAssembly module
Expand Down
2 changes: 2 additions & 0 deletions wacker-cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ mod delete;
pub use self::delete::*;
mod logs;
pub use self::logs::*;
mod serve;
pub use self::serve::*;
5 changes: 4 additions & 1 deletion wacker-cli/src/commands/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ struct Module {
path: String,
#[tabled(rename = "STATUS")]
status: &'static str,
#[tabled(rename = "ADDRESS")]
address: String,
}

impl ListCommand {
Expand All @@ -33,6 +35,7 @@ impl ListCommand {
id: res.id,
path: res.path,
status: ModuleStatus::try_from(res.status).unwrap().as_str_name(),
address: res.addr,
})
}

Expand All @@ -41,7 +44,7 @@ impl ListCommand {
.with(Padding::new(0, 2, 0, 0))
.with(Style::blank())
// the PATH column
.with(Modify::new(Columns::single(1)).with(Width::wrap(80).keep_words()));
.with(Modify::new(Columns::single(1)).with(Width::wrap(60).keep_words()));

println!("{table}");

Expand Down
33 changes: 33 additions & 0 deletions wacker-cli/src/commands/serve.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use anyhow::{anyhow, Result};
use clap::Parser;
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use tonic::transport::Channel;
use wacker::{ModulesClient, ServeRequest};

const DEFAULT_ADDR: SocketAddr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), 8080);

#[derive(Parser)]
pub struct ServeCommand {
/// Module file path
#[arg(required = true)]
path: String,
/// Socket address for the web server to bind to
#[arg(long = "addr", default_value_t = DEFAULT_ADDR )]
addr: SocketAddr,
}

impl ServeCommand {
/// Executes the command.
pub async fn execute(self, mut client: ModulesClient<Channel>) -> Result<()> {
match client
.serve(ServeRequest {
path: self.path.to_string(),
addr: self.addr.to_string(),
})
.await
{
Ok(_) => Ok(()),
Err(err) => Err(anyhow!(err.message().to_string())),
}
}
}
3 changes: 3 additions & 0 deletions wacker-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ fn version() -> &'static str {
enum Subcommand {
/// Runs a WebAssembly module
Run(commands::RunCommand),
/// Serves an HTTP WebAssembly module
Serve(commands::ServeCommand),
/// Lists running WebAssembly modules
#[command(visible_alias = "ps")]
List(commands::ListCommand),
Expand All @@ -43,6 +45,7 @@ impl Wacker {

match self.subcommand {
Subcommand::Run(c) => c.execute(client).await,
Subcommand::Serve(c) => c.execute(client).await,
Subcommand::List(c) => c.execute(client).await,
Subcommand::Stop(c) => c.execute(client).await,
Subcommand::Restart(c) => c.execute(client).await,
Expand Down
14 changes: 11 additions & 3 deletions wacker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,24 @@ tokio-stream.workspace = true
tonic.workspace = true

dirs = "5.0.1"
wasi-common = "16.0.0"
wasmtime = "16.0.0"
wasmtime-wasi = { version = "16.0.0", features = ["tokio"] }
wasi-common = "17.0.0"
wasmtime = "17.0.0"
wasmtime-wasi = { version = "17.0.0", features = ["tokio"] }
wasmtime-wasi-http = "17.0.0"
cap-std = "2.0.0"
rand = "0.8.5"
sled = "0.34.7"
tower = "0.4.13"
prost = "0.12.3"
const_format = "0.2.32"
async-stream = "0.3.5"
hyper = "1.1.0"
http = "1.0.0"
http-body-util = "0.1.0"
bytes = "1.5.0"
async-trait = "0.1.75"
serde = { version = "1.0.193", features = ["derive"] }
bincode = "1.3.3"

[build-dependencies]
anyhow.workspace = true
Expand Down
15 changes: 14 additions & 1 deletion wacker/proto/module.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package module;

service Modules {
rpc Run (RunRequest) returns (google.protobuf.Empty);
rpc Serve (ServeRequest) returns (google.protobuf.Empty);
rpc List (google.protobuf.Empty) returns (ListResponse);
rpc Stop (StopRequest) returns (google.protobuf.Empty);
rpc Restart (RestartRequest) returns (google.protobuf.Empty);
Expand All @@ -17,6 +18,16 @@ message RunRequest {
string path = 1;
}

message ServeRequest {
string path = 1;
string addr = 2;
}

enum ModuleType {
WASI = 0;
HTTP = 1;
}

enum ModuleStatus {
Running = 0;
Finished = 1;
Expand All @@ -27,7 +38,9 @@ enum ModuleStatus {
message Module {
string id = 1;
string path = 2;
ModuleStatus status = 3;
ModuleType module_type = 3;
ModuleStatus status = 4;
string addr = 5;
}

message ListResponse {
Expand Down
2 changes: 1 addition & 1 deletion wacker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use tower::service_fn;
pub use self::config::*;
pub use self::module::{
modules_client::ModulesClient, modules_server::ModulesServer, DeleteRequest, ListResponse, LogRequest, LogResponse,
Module, ModuleStatus, RestartRequest, RunRequest, StopRequest,
Module, ModuleStatus, ModuleType, RestartRequest, RunRequest, ServeRequest, StopRequest,
};
pub use self::server::*;

Expand Down
Loading

0 comments on commit 2fcb48a

Please sign in to comment.