wasm

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2023 License: BSD-3-Clause Imports: 0 Imported by: 0

README

go-whosonfirst-export-wasm

Go package for compiling the Export method of the go-whosonfirst-export package to a JavaScript-compatible WebAssembly (wasm) binary. It also provides a net/http middleware packages for appending the necessary static assets and HTML resources to use the wasm binary in web applications.

Documentation

Go Reference

Build

To build the export_feature WebAssembly binary for use in your applications run the following command:

GOOS=js GOARCH=wasm go build -mod vendor -o export_feature.wasm cmd/export-feature/main.go

Use

To use the export_feature WebAssembly binary in your applications a JSON-encoded GeoJSON string to the export_feature function.

The function returns a JavaScript Promise that will return a JSON-encoded Who's On First (WOF) GeoJSON string on success or an error message if there was a problem.

	var str_f = '{"type": "Feature" ... }'	// A valid GeoJSON Feature
	
	export_feature(str_f).then(rsp => {
	    console.log("WOF-exported feature is ", rsp);
	}).catch(err => {
	    console.log("Failed to export feature: ", err);
	});

In order to load the export_feature function you will need to include the wasm_exec.js and whosonfirst.export.feature.js JavaScript files, or functional equivalents. Both scripts are bundled with this package in the static/javascript folder.

Middleware

The go-whosonfirst-export-wasm/http package provides methods for appending static assets and HTML resources to existing web applications to facilitate the use of the export_feature WebAssembly binary. For example:

package main

import (
	"embed"
	wasm "github.com/whosonfirst/go-whosonfirst-export-wasm/http"
	"log"
	"net/http"
)

//go:embed index.html example.*
var FS embed.FS

func main() {

	mux := http.NewServeMux()

	wasm.AppendAssetHandlers(mux)

	http_fs := http.FS(FS)
	example_handler := http.FileServer(http_fs)

	wasm_opts := wasm.DefaultWASMOptions()
	wasm_opts.EnableWASMExec()

	example_handler = wasm.AppendResourcesHandler(example_handler, wasm_opts)

	mux.Handle("/", example_handler)

	addr := "localhost:8080"
	log.Printf("Listening for requests on %s\n", addr)

	http.ListenAndServe(addr, mux)
}

Error handling omitted for brevity.

There is a full working example of this application in the cmd/example folder. To run this application type the following command:

$> go run -mod vendor cmd/example/main.go

Then if you visit http://localhost:8080 in your web browser you'll see something like this:

And when you click the Export button you'll see something like this:

The logic for the example application is defined in cmd/example/example.init.js.

See also

Documentation

Overview

This package provides tools for compiling the `Export` method of the go-whosonfirst-export package to a JavaScript-compatible WebAssembly (wasm) binary.

It also provides a net/http middleware packages for appending the necessary static assets and HTML resources to use the wasm binary in web applications.

To build the WebAssembly binary run the following command:

GOOS=js GOARCH=wasm go build -mod vendor -o export_feature.wasm cmd/export-feature/main.go

Directories

Path Synopsis
cmd
package http provides HTTP middleware methods for appending the necessary static assets and HTML resources to use the wasm binary in web applications.
package http provides HTTP middleware methods for appending the necessary static assets and HTML resources to use the wasm binary in web applications.
package static provides an `embed.FS` containing JavaScript and WebAssembly binaries used by the go-whosonfirst-export-wasm tools and methods.
package static provides an `embed.FS` containing JavaScript and WebAssembly binaries used by the go-whosonfirst-export-wasm tools and methods.

Jump to

Keyboard shortcuts

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