github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/examples/wasm/server.go (about)

     1  package main
     2  
     3  import (
     4  	"log"
     5  	"net/http"
     6  	"strings"
     7  )
     8  
     9  const dir = "./html"
    10  
    11  func main() {
    12  	fs := http.FileServer(http.Dir(dir))
    13  	log.Print("Serving " + dir + " on http://localhost:8080")
    14  	http.ListenAndServe(":8080", http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) {
    15  		resp.Header().Add("Cache-Control", "no-cache")
    16  		if strings.HasSuffix(req.URL.Path, ".wasm") {
    17  			resp.Header().Set("content-type", "application/wasm")
    18  		}
    19  		fs.ServeHTTP(resp, req)
    20  	}))
    21  }