Skip to content

HTTP Middleware library for TinyGo used to compile WebAssembly Guest modules

License

Notifications You must be signed in to change notification settings

http-wasm/http-wasm-guest-tinygo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Oct 31, 2023
e57ec90 · Oct 31, 2023

History

27 Commits
Oct 31, 2023
Jun 12, 2023
Oct 31, 2023
Jun 12, 2023
Sep 22, 2022
Sep 22, 2022
Sep 22, 2022
Jun 5, 2023
Sep 22, 2022
Oct 31, 2023
Jun 12, 2023
Jun 12, 2023
Oct 20, 2022

Repository files navigation

Build Go Report Card License

http-wasm Guest Library for TinyGo

http-wasm is HTTP client middleware implemented in WebAssembly. This is a TinyGo WASI library that implements the Guest ABI.

Example

The following is an example of routing middleware:

package main

import (
	"strings"

	"github.com/http-wasm/http-wasm-guest-tinygo/handler"
	"github.com/http-wasm/http-wasm-guest-tinygo/handler/api"
)

func main() {
	handler.HandleRequestFn = handleRequest
}

// handle implements a simple HTTP router.
func handleRequest(req api.Request, resp api.Response) (next bool, reqCtx uint32) {
	// If the URI starts with /host, trim it and dispatch to the next handler.
	if uri := req.GetURI(); strings.HasPrefix(uri, "/host") {
		req.SetURI(uri[5:])
		next = true // proceed to the next handler on the host.
		return
	}

	// Serve a static response
	resp.Headers().Set("Content-Type", "text/plain")
	resp.Body().WriteString("hello")
	return // skip any handlers as the response is written.
}

If you make changes, you can rebuild this with TinyGo v0.28 or higher like so:

tinygo build -o examples/router/main.wasm -scheduler=none --no-debug -target=wasi examples/router/main.go

There are also more examples you may wish to try out!

WARNING: This is an early draft

The current maturity phase is early draft. Once this is integrated with coraza and dapr, we can begin discussions about compatability.