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

traefik/http-wasm-guest-tinygo

 
 

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.

About

HTTP Middleware library for TinyGo used to compile WebAssembly Guest modules

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 96.7%
  • Makefile 3.3%