From dc59aefeb77dadece960159aa251b6621475556b Mon Sep 17 00:00:00 2001 From: xiaoqidun Date: Sat, 17 Oct 2026 19:26:09 +0800 Subject: [PATCH] =?UTF-8?q?chore(=E6=9E=84=E5=BB=BA=E5=8F=98=E5=8A=A8):=20?= =?UTF-8?q?=E9=9A=94=E7=A6=BBWASM=E8=B5=84=E6=BA=90=E5=B9=B6=E5=AE=8C?= =?UTF-8?q?=E5=96=84=E6=9E=84=E5=BB=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitea/workflows/build.sh | 2 +- README.md | 2 +- assets/webui/assets.go | 20 ++------------------ assets/webui/wasm/assets.go | 22 ++++++++++++++++++++++ cmd/webui/webui.go | 25 ++++++++++++++++++++++++- 5 files changed, 50 insertions(+), 21 deletions(-) create mode 100644 assets/webui/wasm/assets.go diff --git a/.gitea/workflows/build.sh b/.gitea/workflows/build.sh index 55118d9..d13449c 100644 --- a/.gitea/workflows/build.sh +++ b/.gitea/workflows/build.sh @@ -1,5 +1,5 @@ #!/bin/bash set -e export TZ=Asia/Shanghai -GOOS=js GOARCH=wasm CGO_ENABLED=0 go build -o assets/webui/ofdgo.wasm -trimpath -ldflags "-s -w -buildid=" ./cmd/webui/wasm.go +GOOS=js GOARCH=wasm CGO_ENABLED=0 go build -o assets/webui/wasm/ofdgo.wasm -trimpath -ldflags "-s -w -buildid=" ./cmd/webui/wasm.go GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o main -trimpath -ldflags "-s -w -buildid=" ./cmd/webui/webui.go \ No newline at end of file diff --git a/README.md b/README.md index 10b93f8..732aac3 100644 --- a/README.md +++ b/README.md @@ -53,7 +53,7 @@ docker run -d -p 80:80 ccr.ccs.tencentyun.com/xiaoqidun/ofdgo:latest :: 1. 编译OFDGo WASM set GOOS=js set GOARCH=wasm -go build -o assets/webui/ofdgo.wasm -trimpath -ldflags "-s -w -buildid=" ./cmd/webui/wasm.go +go build -o assets/webui/wasm/ofdgo.wasm -trimpath -ldflags "-s -w -buildid=" ./cmd/webui/wasm.go :: 2. 编译OFDGo WebUI set GOOS=windows set GOARCH=amd64 diff --git a/assets/webui/assets.go b/assets/webui/assets.go index beeb608..0f2e9a3 100644 --- a/assets/webui/assets.go +++ b/assets/webui/assets.go @@ -14,30 +14,14 @@ package webuiassets -import ( - "crypto/sha256" - "embed" - "encoding/hex" -) +import "embed" // FS WebUI嵌入静态文件 // -//go:embed index.html ofdgo.css ofdgo.ico ofdgo.js ofdgo.json ofdgo.png ofdgo.svg ofdgo.wasm ofdgo_edit.js ofdgo_font.js ofdgo_host.js ofdgo_wasm.js ofdgo_work.js wasm_exec.js +//go:embed index.html ofdgo.css ofdgo.ico ofdgo.js ofdgo.json ofdgo.png ofdgo.svg ofdgo_edit.js ofdgo_font.js ofdgo_host.js ofdgo_wasm.js ofdgo_work.js wasm_exec.js var FS embed.FS // NativeFS WebView嵌入静态文件 // //go:embed index.html ofdgo.css ofdgo.ico ofdgo.js ofdgo.json ofdgo.png ofdgo.svg ofdgo_edit.js ofdgo_font.js ofdgo_host.js var NativeFS embed.FS - -// Checksum 计算WebUI嵌入资源校验值 -// 返回: string SHA-256十六进制校验值 -func Checksum() string { - hash := sha256.New() - entries, _ := FS.ReadDir(".") - for _, entry := range entries { - data, _ := FS.ReadFile(entry.Name()) - hash.Write(data) - } - return hex.EncodeToString(hash.Sum(nil)) -} diff --git a/assets/webui/wasm/assets.go b/assets/webui/wasm/assets.go new file mode 100644 index 0000000..0a05e3b --- /dev/null +++ b/assets/webui/wasm/assets.go @@ -0,0 +1,22 @@ +// Copyright 2025-2026 肖其顿 (XIAO QI DUN) +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package wasmassets + +import "embed" + +// FS WebUI嵌入WASM引擎 +// +//go:embed ofdgo.wasm +var FS embed.FS diff --git a/cmd/webui/webui.go b/cmd/webui/webui.go index 3b3795a..082df04 100644 --- a/cmd/webui/webui.go +++ b/cmd/webui/webui.go @@ -18,6 +18,9 @@ package main import ( "bytes" + "crypto/sha256" + "embed" + "encoding/hex" "flag" "fmt" "net/http" @@ -25,6 +28,7 @@ import ( "time" "github.com/xiaoqidun/ofdgo/assets/webui" + "github.com/xiaoqidun/ofdgo/assets/webui/wasm" ) func main() { @@ -39,12 +43,17 @@ func main() { // 返回: http.Handler HTTP处理器 func serveWebUI() http.Handler { files := http.FileServerFS(webuiassets.FS) - checksum := webuiassets.Checksum() + wasm := http.FileServerFS(wasmassets.FS) + checksum := resourceChecksum() serviceWorker, _ := webuiassets.FS.ReadFile("ofdgo_work.js") serviceWorker = append([]byte(fmt.Sprintf("const BUNDLE_CHECKSUM = %q;\n", checksum)), serviceWorker...) return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Cache-Control", "no-store") w.Header().Set("X-OFDGo-Checksum", checksum) + if r.URL.Path == "/ofdgo.wasm" { + wasm.ServeHTTP(w, r) + return + } if r.URL.Path == "/ofdgo_work.js" { http.ServeContent(w, r, "ofdgo_work.js", time.Time{}, bytes.NewReader(serviceWorker)) return @@ -52,3 +61,17 @@ func serveWebUI() http.Handler { files.ServeHTTP(w, r) }) } + +// resourceChecksum 计算WebUI界面与WASM引擎的资源校验值 +// 返回: string SHA-256十六进制校验值 +func resourceChecksum() string { + hash := sha256.New() + for _, resources := range []embed.FS{webuiassets.FS, wasmassets.FS} { + entries, _ := resources.ReadDir(".") + for _, entry := range entries { + data, _ := resources.ReadFile(entry.Name()) + hash.Write(data) + } + } + return hex.EncodeToString(hash.Sum(nil)) +}