DEV Community

Cover image for Emulate a Go HTTP server in your browser
Nicolas Lepage
Nicolas Lepage

Posted on

Emulate a Go HTTP server in your browser

Hi 👋 I'd like to share github.com/nlepage/go-wasm-http-server

GitHub logo nlepage / go-wasm-http-server

WebAssembly Go HTTP Server embedded in a ServiceWorker

Welcome to go-wasm-http-server 👋

Go Reference License: Apache 2.0 Twitter: njblepage

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

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())…




This project allows you to build Go HTTP handlers to WebAssembly and embed it in a ServiceWorker to emulate an HTTP server!

I'd really appreciate to have feedback from people who would give it a try 🙂

The readme has a usage section and some examples to guide you.

Here is a video from FOSDEM 21 explaining how this works:

Contributions on the project are more than welcome.

Thanks!

Top comments (1)

Collapse
 
thomaspeugeot profile image
Thomas Peugeot

Hi,

Great work! I work on a fork of the repo. I have a question regarding porting a websocket connexion to this "in browser connexion". How can I contact you ?

BR,

Thomas