Skip to content

Add instructions for testing the endpoints. #6

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 1 commit into from
Apr 29, 2025
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
35 changes: 25 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,58 @@ The following HTTP routes are available from the component:
/sleep # Sleep for {ms} milliseconds
/echo # Echo the HTTP body
/echo-headers # Echo the HTTP headers
/echo-trailers # Echo the HTTP trailers
/upload # Echo uploaded blob
/upload # Echo uploaded blob
```

Testing routes:

```bash
# Hello world
$ curl localhost:8080
# Sleep for {ms} milliseconds
$ curl localhost:8080/sleep/2000
# Echo the HTTP body
$ curl -d "Test echo body" localhost:8080/echo
# Echo the HTTP headers
$ curl -H "X-Test-Header: 123" localhost:8080/echo-headers
# Echo uploaded blob
$ echo "Hello World!" > test_file.txt
$ curl -H "Content-Type: text/plain" --data-binary @test_file.txt http://localhost:8080/upload
```

## Quick Start
The project uses [`Wasmtime`][wasmtime] as its runtime. However, if needed, it
can easily be adjusted to use [`jco`][jco] instead. For `wasmtime` installation,
The project uses [`Wasmtime`][wasmtime] as its runtime. However, if needed, it
can easily be adjusted to use [`jco`][jco] instead. For `wasmtime` installation,
simply run:

```bash
$ curl https://wasmtime.dev/install.sh -sSf | bash
```

The quickest way to start is by using [`just`][just].
The quickest way to start is by using [`just`][just].
```bash
$ just serve # to build and serve the wasm component on `localhost:8080`
$ curl 127.0.0.1:8080 # to send requests to component.
$ curl localhost:8080 # to send requests to component.
```

Alternatively, run:

```bash
$ npm install
$ npm build
$ npm run build
$ wasmtime serve -S common dist/server.component.wasm
```

## See Also

- [sample-wasi-http-rust](https://github.com/bytecodealliance/sample-wasi-http-rust) An example `wasi:http` server component written in Rust.
- [sample-wasi-http-rust][rust-sample] An example `wasi:http` server component written in Rust.

## License

Apache-2.0 with LLVM Exception

[jco]: https://github.com/bytecodealliance/jco
[just]: https://github.com/casey/just
[rust-sample]: https://github.com/bytecodealliance/sample-wasi-http-rust
[rust-sample]: https://github.com/bytecodealliance/sample-wasi-http-rust
[wasi-http]: https://github.com/WebAssembly/wasi-http
[wasmtime]: https://wasmtime.dev/
[wasmtime]: https://wasmtime.dev/
21 changes: 1 addition & 20 deletions src/endpoints/echo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { echo, echoHeaders, echoTrailers };
export { echo, echoHeaders };

// Echo the body of the request in the response
const echo = async (req) => {
Expand All @@ -22,22 +22,3 @@ const echoHeaders = (req) => {
},
});
};

// Echo all trailers from the request in JSON format
const echoTrailers = async (req) => {
let trailers;
try {
trailers = req.trailers ? await req.trailers : new Headers();
} catch (e) {
console.warning("Error fetching trailers:", e);
trailers = new Headers();
}

const trailersObj = Object.fromEntries(trailers.entries());
return new Response(JSON.stringify(trailersObj, null, 2), {
status: 200,
headers: {
"Content-Type": "application/json",
},
});
};
3 changes: 1 addition & 2 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import { AutoRouter } from "itty-router";

import { sleep } from "./endpoints/sleep.js";
import { upload } from "./endpoints/upload.js";
import { echo, echoHeaders, echoTrailers } from "./endpoints/echo.js";
import { echo, echoHeaders } from "./endpoints/echo.js";

let router = AutoRouter();

router
.get("/", () => new Response("Hello, World!"))
.get("/echo-headers", (req) => echoHeaders(req))
.get("/echo-trailers", (req) => echoTrailers(req))
.get("/sleep/:ms", async ({ ms }) => await sleep(ms))
.post("/echo", (req) => echo(req))
.post("/upload", async (req) => await upload(req));
Expand Down