Coder Social home page Coder Social logo

izouxv / go-wasm-http-server Goto Github PK

View Code? Open in Web Editor NEW

This project forked from nlepage/go-wasm-http-server

0.0 0.0 0.0 22.05 MB

Embed your Go HTTP handlers in a ServiceWorker and emulate an HTTP server!

License: Apache License 2.0

JavaScript 16.34% Go 83.66%

go-wasm-http-server's Introduction

Welcome to go-wasm-http-server ๐Ÿ‘‹

Go Reference License: Apache 2.0 Twitter: njblepage

Embed your Go HTTP handlers in a ServiceWorker (using WebAssembly) and emulate an HTTP server!

Examples

How?

Talk given at the Go devroom of FOSDEM 2021 explaining how go-wasm-http-server works:

Deploy a Go HTTP server in your browser Youtube link

The slides are available here.

Why?

go-wasm-http-server can help you put up a demonstration for a project without actually running a Go HTTP server.

Requirements

go-wasm-http-server requires you to build your Go application to WebAssembly, so you need to make sure your code is compatible:

  • no C bindings
  • no System dependencies such as file system or network (database server for example)

Usage

Step 1: Build to js/wasm

In your Go code, replace http.ListenAndServe() (or net.Listen() + http.Serve()) by wasmhttp.Serve():

๐Ÿ“„ server.go

// +build !js,!wasm

package main

import (
    "net/http"
)

func main() {
    // Define handlers...

    http.ListenAndServe(":8080", nil)
}

becomes:

๐Ÿ“„ server_js_wasm.go

// +build js,wasm

package main

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

func main() {
    // Define handlers...

    wasmhttp.Serve(nil)
}

You may want to use build tags as shown above (or file name suffixes) in order to be able to build both to WebAssembly and other targets.

Then build your WebAssembly binary:

GOOS=js GOARCH=wasm go build -o server.wasm .

Step 2: Create ServiceWorker file

Create a ServiceWorker file with the following code:

๐Ÿ“„ sw.js

importScripts('https://cdn.jsdelivr.net/gh/nlepage/[email protected]/sw.js')

registerWasmHTTPListener('path/to/server.wasm')

By default the server will deploy at the ServiceWorker's scope root, check registerWasmHTTPListener()'s API for more information.

You may want to add these additional event listeners in your ServiceWorker:

// Skip installed stage and jump to activating stage
addEventListener('install', (event) => {
  event.waitUntil(skipWaiting())
})

// Start controlling clients as soon as the SW is activated
addEventListener('activate', event => {
  event.waitUntil(clients.claim())
})

Step 3: Register the ServiceWorker

In your web page(s), register the ServiceWorker:

<script>
  // By default the ServiceWorker's scope will be "server/"
  navigator.serviceWorker.register('server/sw.js')
</script>

Now your web page(s) may start fetching from the server:

// The server will receive a request for "/path/to/resource"
fetch('server/path/to/resource').then(res => {
  // use response...
})

API

For Go API see pkg.go.dev/github.com/nlepage/go-wasm-http-server

JavaScript API

registerWasmHTTPListener(wasmUrl, options)

Instantiates and runs the WebAssembly module at wasmUrl, and registers a fetch listener forwarding requests to the WebAssembly module's server.

โš  This function must be called only once in a ServiceWorker, if you want to register several servers you must use several ServiceWorkers.

The server will be "deployed" at the root of the ServiceWorker's scope by default, base may be used to deploy the server at a subpath of the scope.

See ServiceWorkerContainer.register() for more information about the scope of a ServiceWorker.

wasmUrl

URL string of the WebAssembly module, example: "path/to/my-module.wasm".

options

An optional object containing:

  • base (string): Base path of the server, relative to the ServiceWorker's scope.
  • args (string[]): Arguments for the WebAssembly module.

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

go-wasm-http-server's People

Contributors

nlepage avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.