jshttp

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2022 License: MIT Imports: 1 Imported by: 0

README

Go Reference

JSHTTP

Go HTTP Adapter for WebAssembly

Download and install

go get -u v8.run/go/jshttp

Usage

package main

import (
	"net/http"

	"v8.run/go/jshttp"
)

func main() {
	mux := http.NewServeMux()
	mux.HandleFunc("/", func(w http.ResponseWriter, _ *http.Request) {
		w.Write([]byte("Hello, World!"))
	})
	jshttp.Serve(mux)
}

Using JSHTTP With Deno

import { serve } from "https://deno.land/std@0.154.0/http/mod.ts";
import * as _ from "https://cdn.jsdelivr.net/gh/golang/go@go1.19/misc/wasm/wasm_exec.js";

const wasm_file = await Deno.readFile("./main.wasm");
const go = new window.Go();
const instance = await WebAssembly.instantiate(wasm_file, go.importObject);

go.argv = Deno.args.slice(2);
if ((await Deno.permissions.query({ name: "env" })).state == "granted") {
    const env = Deno.env.toObject();
    go.env = env;
}
go.exit = Deno.exit;
go.run(instance.instance);

await serve(async (_req) => {
    try {
        const _resp = await window.__go_jshttp(_req);
        return _resp;
    } catch (e) {
        console.error(e);
        return new Response("Internal Server Error", { status: 500 });
    }
});

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Serve

func Serve(h http.Handler)

Types

This section is empty.

Directories

Path Synopsis
examples
helloworld command

Jump to

Keyboard shortcuts

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