{"id":15401128,"url":"https://github.com/sunfishcode/hello-wasi-http","last_synced_at":"2025-04-07T06:05:56.102Z","repository":{"id":203592424,"uuid":"709971935","full_name":"sunfishcode/hello-wasi-http","owner":"sunfishcode","description":null,"archived":false,"fork":false,"pushed_at":"2025-01-15T02:00:53.000Z","size":186,"stargazers_count":70,"open_issues_count":4,"forks_count":21,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-04-04T06:07:18.828Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sunfishcode.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-25T18:37:58.000Z","updated_at":"2025-03-25T16:57:46.000Z","dependencies_parsed_at":"2025-01-15T02:51:26.566Z","dependency_job_id":"c305e7fb-c14d-4286-8a45-23bba651594c","html_url":"https://github.com/sunfishcode/hello-wasi-http","commit_stats":{"total_commits":35,"total_committers":6,"mean_commits":5.833333333333333,"dds":0.1428571428571429,"last_synced_commit":"29205a0749835bd65be65841640af913be9be794"},"previous_names":["sunfishcode/hello-wasi-http"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunfishcode%2Fhello-wasi-http","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunfishcode%2Fhello-wasi-http/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunfishcode%2Fhello-wasi-http/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sunfishcode%2Fhello-wasi-http/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sunfishcode","download_url":"https://codeload.github.com/sunfishcode/hello-wasi-http/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247601447,"owners_count":20964864,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-01T15:56:33.973Z","updated_at":"2025-04-07T06:05:56.079Z","avatar_url":"https://github.com/sunfishcode.png","language":"Rust","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hello WASI HTTP!\n\nThis is a simple tutorial to get started with WASI HTTP using the `wasmtime serve` command introduced in [Wasmtime] 18.0.\nIt runs an HTTP server and forwards requests to a Wasm component via the [WASI HTTP] API.\n\nOther resources you may find interesting:\n - [This tutorial](https://github.com/yoshuawuyts/sample-wasi-http-rust/), which uses\n   wkg and registries.\n - The [`wstd` crate](https://crates.io/crates/wstd), which provides friendlier APIs\n   on top of the WASI APIs, and includes examples.\n\n[Wasmtime]: https://wasmtime.dev\n[WASI HTTP]: https://github.com/WebAssembly/wasi-http/\n\nThe WASI HTTP API is now stable, and part of WASI 0.2.\n\nSo without further ado...\n\n## Let's go!\n\nFirst, [install `cargo-component`](https://github.com/bytecodealliance/cargo-component#requirements) (version 0.13.2 or later). `cargo-component` is a tool for building Wasm components implemented in Rust. For more\ninformation on building Wasm components from different languages, check [here]!\n\n[here]: https://component-model.bytecodealliance.org/language-support.html\n\nWith that, build the Wasm component from source in this repository:\n```sh\n$ cargo component build\n  Compiling hello-wasi-http v0.0.0 (/home/wasm/hello-wasi-http)\n    Finished `dev` profile [unoptimized + debuginfo] target(s) in 2.01s\n    Creating component target/wasm32-wasip1/debug/hello_wasi_http.wasm\n```\n\nThis builds a Wasm component at `target/wasm32-wasip1/debug/hello_wasi_http.wasm`.\n\nTo run it, we'll need at least Wasmtime `18.0`. Installation instructions are on [wasmtime.dev]. For example, on Linux or macOS, use the command below:\n\n```sh\n$ curl https://wasmtime.dev/install.sh -sSf | bash\n```\n\n[wasmtime.dev]: https://wasmtime.dev/\n\nThen, run in your terminal:\n```sh\n$ cargo component serve\n```\nThis starts up an HTTP server that, by default, listens on `0.0.0.0:8080`.\n\nWith that running, in another window, we can now make requests!\n```sh\n$ curl http://localhost:8080\nHello, wasi:http/proxy world!\n```\n\n## Optimizing!\n\nThe above uses a `debug` build. To make a component that runs faster, build with `cargo component build --release`.\n\nIt's also worth making sure you have a release build of Wasmtime; if you installed it from the instructions above\nwith wasmtime.dev, you're good.\n\nWasmtime has several tuning options that can improve performance in different situations—pass `-O help` for a\nlist. One that's especially useful here is `-O pooling-allocator`.\n\n## Notes\n\n`wasmtime serve` uses the [proxy] world, which is a specialized world just for accepting requests and producing\nresponses. One interesting thing about the proxy world is that it doesn't have a filesystem or network API. If you add\ncode to the example that tries to access files or network sockets, it won't be able to build, because those APIs are\nnot available in this world. This allows proxy components to run in many different places, including specialized\nserverless environments which may not provide traditional filesystem and network access.\n\nBut, what if you do want to have it serve some files? One option will be to use \n[WASI-Virt](https://github.com/bytecodealliance/WASI-Virt), which is a tool that can bundle a filesystem with a \ncomponent.\n\nAnother option is to use a custom `world`. The proxy world is meant to be able to run in many different environments,\nbut if you know your environment, and you know it has a filesystem, you could create your own world, by including both\nthe `\"wasi:http/proxy\"` and `\"wasi:filesystem/types\"` or any other APIs you want the Wasm to be able to access. This\nwould require a custom embedding of Wasmtime, as it wouldn't run under plain `wasmtime serve`, so it's a little more\nwork to set up.\n\nIn the future, we expect to see standard `world`s emerge that combine WASI HTTP with many other APIs, such as\n[wasi-cloud-core].\n\nIf you're interested in tutorials for any of these options, please reach out and say hi!\n\n[proxy]: https://github.com/WebAssembly/wasi-http/blob/main/wit/proxy.wit\n[wasi-cloud-core]: https://github.com/WebAssembly/wasi-cloud-core\n\n## Creating this repo\n\nHere are my notes on how I created this repository, in case you're interested in recreating it.\n\nTo create a new project, run:\n\n```sh\n$ cargo component new --proxy --lib hello-wasi-http\n    Created binary (application) `hello-wasi-http` package\n    Updated manifest of package `hello-wasi-http`\n    Generated source file `src/main.rs`\n$ cd hello-wasi-http\n```\n\nCopy the `wit` directory from your version of  Wasmtime, to ensure that we're using the same version of the API that\nWasmtime is built with (e.g., for Wasmtime 18.0.0: https://github.com/bytecodealliance/wasmtime/tree/release-18.0.0).\n\nThen, I manually trimmed the filesystem and sockets dependencies out.\n\nIn the future, we'll have wit dependencies stored in a registry, which will make this all much easier.\n\nI derived `src/lib.rs` from Wasmtime's `crates/test-programs/src/bin/api_proxy.rs` contents on the `main` branch,\nadapted it to work with cargo component, in particular by adding:\n\n```rust\ncargo_component_bindings::generate!();\n```\n\nThen, I renamed the `T` type to `Component`, which the bindings expect.\n\nFinally, add dependencies:\n```\n$ cargo component add --target --path wit/deps/clocks wasi:clocks\n$ cargo component add --target --path wit/deps/io wasi:io\n$ cargo component add --target --path wit/deps/random wasi:random\n$ cargo component add --target --path wit/deps/cli wasi:cli\n$ cargo component add --target --path wit/deps/logging wasi:logging\n```\n\nThese don't all actually get used in this tutorial, but they're currently needed because of some of the interfaces we\ncopied in from the Wasmtime tree reference them.\n\n\u003e TODO: I should also make a `api_proxy_streaming.rs` version to show streaming.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunfishcode%2Fhello-wasi-http","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsunfishcode%2Fhello-wasi-http","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsunfishcode%2Fhello-wasi-http/lists"}