handler

package
v0.0.0-...-730272d Latest Latest
Warning

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

Go to latest
Published: Jun 2, 2019 License: MIT Imports: 14 Imported by: 2

Documentation

Overview

Package handler implements a dynamic wasm building http.Handler.

Example (Hello)
//go:build js
// +build js

package main

import "syscall/js"

var (
	window   = js.Global().Get("window")
	document = js.Global().Get("document")
)

func main() {
	window.Call("addEventListener", "DOMContentLoaded",
		js.FuncOf(func(this js.Value, args []js.Value) interface{} {
			document.Get("body").Set("innerText", "hello world")
		}))
}
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// IndexHandler provides a sensible default index.html
	IndexHandler http.Handler // index.html

	// RunHandler provides a wrapper script that eases integrating wasm_exec.js
	// and a compiled wasm endpoint.
	RunHandler http.Handler // index.js
)

Functions

This section is empty.

Types

type WASMHandler

type WASMHandler struct {
	// contains filtered or unexported fields
}

WASMHandler implements an http.Handler that serves a dynamically built wasm binary from a Go "main" package.

The target package must be a normal main package with a func main() entry point and should contain a js build tag. See the package examples for detail.

func Handle

func Handle(prefix, srcDir, path string) (*WASMHandler, error)

Handle mounts a new WASMHandler at the given prefix onto the http.DefaultServeMux. The caller should defer a call WASMHandler.Close() to ensure temporary file deletion.

func NewWASMHandler

func NewWASMHandler(srcDir, path string) (*WASMHandler, error)

NewWASMHandler creates a WASMHandler for a given package path and source directory.

func (*WASMHandler) Close

func (wh *WASMHandler) Close() error

Close removes any temporary built wasm binary.

func (*WASMHandler) ExecHandler

func (wh *WASMHandler) ExecHandler() http.Handler

ExecHandler returns an http handler that will serve the appropriate wasm_exec.js stub from $GOROOT.

func (*WASMHandler) IndexHandler

func (wh *WASMHandler) IndexHandler() http.Handler

IndexHandler returns an http.Handler either backed by the package's directory if it contains an index.html file, or a default one otherwise.

func (*WASMHandler) Mount

func (wh *WASMHandler) Mount(prefix string, mux *http.ServeMux)

Mount mounts the IndexHandler() at /, the RunHandler at /index.js, the ExecHandler() at /wasm_exec.js, and finally the WASMHandler itself at /main.wasm.

func (*WASMHandler) ServeHTTP

func (wh *WASMHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP dispatches the request dynamically.

It serves a text build log if the "log" form value is set.

It serves a json build config if the "build" form value is set.

It builds a wasm binary if none has been built before or if the "force" form value is set.

It serves the built wasm binary, or redirects to the build log if the build fails.

func (*WASMHandler) String

func (wh *WASMHandler) String() string

Jump to

Keyboard shortcuts

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