gwasm

package module
v0.0.11 Latest Latest
Warning

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

Go to latest
Published: May 7, 2022 License: MIT Imports: 11 Imported by: 12

README

gwasm

Common utilities I find myself rewriting too often across Go WASM projects.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WASMHandler added in v0.0.10

type WASMHandler struct {
	// Compiler is the tool used to compile the WASM binary. Can be "go", "tinygo".
	Compiler string
	// IndexHTML is the html served to the user when loading the application. Should contain
	// the WASM bootstrap javascript to correctly load the WASM binary.
	IndexHTML string
	// WASMReload set to true recompiles the WASM application on every request.
	WASMReload bool
	// WASMDir points to the directory with the package/module with the Go WASM application.
	// If WASMDir is the empty string, it will serve the WASMApplication without compiling.
	WASMDir string
	// WASMApplication is the compiled WASM binary data as output by the go tool.
	WASMApplication []byte
	// Bootloader script. Filename can be found in a go installation by running
	//  out, err := exec.Command(wsm.Compiler, "env", "GOROOT").Output()
	//  if err != nil {
	//  	return nil, fmt.Errorf("%w: %s", err, string(out))
	//  }
	//  filename := filepath.Join(strings.TrimSpace(string(out)), "misc", "wasm", "wasm_exec.js")
	WASMExecContent []byte
	// contains filtered or unexported fields
}

WASMHandler serves a WASM application. Implements http.Handler interface.

func NewWASMHandler added in v0.0.10

func NewWASMHandler(wasmDir string, subHandler http.HandlerFunc) (*WASMHandler, error)

NewWASMHandler returns a handler which does basically the same thing as https://github.com/hajimehoshi/wasmserve.

Example of usage which does most of what `wasmserve` does:

wsm, err := gwasm.NewWASMHandler("app", nil)
if err != nil {
	log.Fatal(err)
}
wsm.WASMReload = true
wsm.SetOutput(os.Stdout)
http.Handle("/", wsm)
log.Fatal(http.ListenAndServe(":8080", nil))

func (*WASMHandler) ServeHTTP added in v0.0.10

func (wsm *WASMHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP implements http.Handler interface. For use with http.Handle

func (*WASMHandler) SetOutput added in v0.0.10

func (wsm *WASMHandler) SetOutput(w io.Writer)

SetOutput sets logging write output to visualize compile-time errors and bad requests.

Jump to

Keyboard shortcuts

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