wasmhttp

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2021 License: Apache-2.0 Imports: 7 Imported by: 2

README

Welcome to go-wasm-http-server 👋

Go Reference

Build your Go HTTP Server to WebAssembly and embed it in a ServiceWorker!

Demos

Install

TODO

Usage

TODO

Why?

TODO

How?

TODO

Author

👤 Nicolas Lepage

🤝 Contributing

Contributions, issues and feature requests are welcome!
Feel free to check issues page.

Show your support

Give a ⭐️ if this project helped you!

📝 License

Copyright © 2021 Nicolas Lepage.
This project is Apache 2.0 licensed.


This README was generated with ❤️ by readme-md-generator

Documentation

Overview

Package wasmhttp (github.com/nlepage/go-wasm-http-server) allows to create a WebAssembly Go HTTP Server embedded in a ServiceWorker.

It is a subset of the full solution, a full usage is available on the github repository: https://github.com/nlepage/go-wasm-http-server

Example (Json)

Demonstrates a simple hello JSON service.

package main

import (
	"encoding/json"
	"fmt"
	"net/http"

	wasmhttp "github.com/nlepage/go-wasm-http-server"
)

func main() {
	http.HandleFunc("/hello", func(res http.ResponseWriter, req *http.Request) {
		params := make(map[string]string)
		if err := json.NewDecoder(req.Body).Decode(&params); err != nil {
			panic(err)
		}

		if err := json.NewEncoder(res).Encode(map[string]string{
			"message": fmt.Sprintf("Hello %s!", params["name"]),
		}); err != nil {
			panic(err)
		}
	})

	defer wasmhttp.Serve(nil)()

	// Wait for webpage event or use empty select{}
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Await

func Await(p js.Value) (js.Value, error)

Await waits for the Promise to be resolved and returns the value

func NewPromise

func NewPromise() (p js.Value, resolve func(interface{}), reject func(interface{}))

NewPromise creates a new JavaScript Promise

func Request

func Request(r js.Value) *http.Request

Request builds and returns the equivalent http.Request

func Serve

func Serve(handler http.Handler) func()

Serve serves HTTP requests using handler or http.DefaultServeMux if handler is nil.

Types

type ResponseRecorder

type ResponseRecorder struct {
	*httptest.ResponseRecorder
}

ResponseRecorder extends httptest.ResponseRecorder and implements js.Wrapper

func NewResponseRecorder

func NewResponseRecorder() ResponseRecorder

NewResponseRecorder returns a new ResponseRecorder

func (ResponseRecorder) JSValue

func (rr ResponseRecorder) JSValue() js.Value

JSValue builds and returns the equivalent JS Response (implementing js.Wrapper)

Directories

Path Synopsis
docs
hello command
hello-state command

Jump to

Keyboard shortcuts

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