Add workaround for the new path of wasm_exec.js

This commit is contained in:
David Vogel 2025-08-30 19:04:16 +02:00
parent 2619cb9e06
commit bdbdacd2a3
3 changed files with 47 additions and 1 deletions

2
.vscode/launch.json vendored
View File

@ -9,7 +9,7 @@
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/scripts/dev-server/devserver.go",
"program": "${workspaceFolder}/scripts/dev-server/",
"env": {
"GOOS": "windows",
"GOARCH": "amd64"

View File

@ -16,11 +16,14 @@ func main() {
simplehttp.DefaultStaticData["Title"] = "D3vugu-components examples"
simplehttp.DefaultStaticData["CSSFiles"] = []string{"/static/css/d3c-theme-base.css"}
var wasmHandler WASMExecHandler
wd, _ := os.Getwd()
uiDir := filepath.Join(wd)
l := ":8875"
log.Printf("Starting HTTP Server at %q", l)
h := simplehttp.New(uiDir, true)
http.Handle("/wasm_exec.js", &wasmHandler) // A hack to circumvent the old hardcoded path in simplehttp. TODO: Seek alternative to the simplehttp dev server
http.Handle("/", h)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir(filepath.Join(uiDir, "static")))))

View File

@ -0,0 +1,43 @@
package main
import (
"bytes"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"sync"
"time"
)
type WASMExecHandler struct {
wasmExecJsOnce sync.Once
wasmExecJsContent []byte
wasmExecJsTs time.Time
}
func (h *WASMExecHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
b, err := exec.Command("go", "env", "GOROOT").CombinedOutput()
if err != nil {
http.Error(w, "failed to run `go env GOROOT`: "+err.Error(), 500)
return
}
h.wasmExecJsOnce.Do(func() {
h.wasmExecJsContent, err = os.ReadFile(filepath.Join(strings.TrimSpace(string(b)), "lib/wasm/wasm_exec.js"))
if err != nil {
http.Error(w, "failed to run `go env GOROOT`: "+err.Error(), 500)
return
}
h.wasmExecJsTs = time.Now() // hack but whatever for now
})
if len(h.wasmExecJsContent) == 0 {
http.Error(w, "failed to read wasm_exec.js from local Go environment", 500)
return
}
w.Header().Set("Content-Type", "text/javascript")
http.ServeContent(w, r, "/wasm_exec.js", h.wasmExecJsTs, bytes.NewReader(h.wasmExecJsContent))
}