wasm

package module
v0.0.0-...-65b25bd Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 6, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

Documentation

Overview

Example (Auth)
ctx := context.Background()

// Configure and compile the WebAssembly guest binary. In this case, it is
// an auth interceptor.
mw, err := NewMiddleware(ctx, test.AuthWasm)
if err != nil {
	log.Panicln(err)
}
defer mw.Close(ctx)

// Wrap the real handler with an interceptor implemented in WebAssembly.
wrapped := mw.NewHandler(ctx, serveJson)

// Start the server with the wrapped handler.
ts, url := listenAndServe(wrapped)
defer ts.Shutdown()

// Invoke some requests, only one of which should pass
headers := []http.Header{
	{"NotAuthorization": {"1"}},
	{"Authorization": {""}},
	{"Authorization": {"Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ=="}},
	{"Authorization": {"0"}},
}

for _, header := range headers {
	req, err := http.NewRequest(http.MethodGet, url, nil)
	if err != nil {
		log.Panicln(err)
	}
	req.Header = header

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		log.Panicln(err)
	}
	resp.Body.Close()

	switch resp.StatusCode {
	case http.StatusOK:
		fmt.Println("OK")
	case http.StatusUnauthorized:
		fmt.Println("Unauthorized")
	default:
		log.Panicln("unexpected status code", resp.StatusCode)
	}
}
Output:

Unauthorized
Unauthorized
OK
Unauthorized
Example (Log)
ctx := context.Background()
logger := func(_ context.Context, message string) { fmt.Println(message) }

// Configure and compile the WebAssembly guest binary. In this case, it is
// a logging interceptor.
mw, err := NewMiddleware(ctx, test.LogWasm, httpwasm.Logger(logger))
if err != nil {
	log.Panicln(err)
}
defer mw.Close(ctx)

// Wrap the real handler with an interceptor implemented in WebAssembly.
wrapped := mw.NewHandler(ctx, serveJson)

// Start the server with the wrapped handler.
ts, url := listenAndServe(wrapped)
defer ts.Shutdown()

// Make a client request and print the contents to the same logger
resp, err := http.Get(url)
if err != nil {
	log.Panicln(err)
}
defer resp.Body.Close()
content, _ := io.ReadAll(resp.Body)
logger(ctx, string(content))
Output:

before
after
{"hello": "world"}
Example (Router)
ctx := context.Background()

// Configure and compile the WebAssembly guest binary. In this case, it is
// an example request router.
mw, err := NewMiddleware(ctx, test.RouterWasm)
if err != nil {
	log.Panicln(err)
}
defer mw.Close(ctx)

// Wrap the real handler with an interceptor implemented in WebAssembly.
wrapped := mw.NewHandler(ctx, servePath)

// Start the server with the wrapped handler.
ts, url := listenAndServe(wrapped)
defer ts.Shutdown()

// Invoke some requests, only one of which should pass
paths := []string{
	"/",
	"/nothosst",
	"/host/a",
}

for _, p := range paths {
	resp, err := http.Get(fmt.Sprintf("%s/%s", url, p))
	if err != nil {
		log.Panicln(err)
	}
	defer resp.Body.Close()
	content, _ := io.ReadAll(resp.Body)
	fmt.Println(string(content))
}
Output:

hello world
hello world
/a

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Middleware

func NewMiddleware

func NewMiddleware(ctx context.Context, guest []byte, options ...httpwasm.Option) (Middleware, error)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL