fix: embed admin webui and wasm for serverless runtime

This commit is contained in:
CJACK
2026-02-16 18:56:11 +08:00
parent 057862f7fb
commit f82a7e3e3c
9 changed files with 408 additions and 9 deletions

Binary file not shown.

View File

@@ -0,0 +1,6 @@
package deepseek
import _ "embed"
//go:embed assets/sha3_wasm_bg.7b9ca65ddd.wasm
var embeddedWASM []byte

View File

@@ -33,8 +33,11 @@ func (p *PowSolver) init(ctx context.Context) error {
p.once.Do(func() {
wasmBytes, err := os.ReadFile(p.wasmPath)
if err != nil {
p.err = err
return
if len(embeddedWASM) == 0 {
p.err = err
return
}
wasmBytes = embeddedWASM
}
p.runtime = wazero.NewRuntime(ctx)
p.compiled, p.err = p.runtime.CompileModule(ctx, wasmBytes)

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<!-- SEO Meta Tags -->
<title>DS2API - 管理面板 / Admin Console</title>
<meta name="description" content="DS2API 管理面板:管理 DeepSeek 账号、测试 API、同步 Vercel 配置 / Admin console for accounts, API tests, and Vercel sync." />
<meta name="keywords" content="DeepSeek, API, OpenAI, 管理面板, admin console, DS2API" />
<meta name="author" content="CJackHwang" />
<meta name="robots" content="noindex, nofollow" />
<!-- Open Graph / Social -->
<meta property="og:type" content="website" />
<meta property="og:title" content="DS2API - 管理面板 / Admin Console" />
<meta property="og:description" content="Manage DeepSeek accounts, test the API, and sync Vercel configuration." />
<meta property="og:site_name" content="DS2API" />
<!-- PWA / Mobile -->
<meta name="theme-color" content="#f59e0b" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="apple-mobile-web-app-title" content="DS2API" />
<!-- Favicon - using data URI for orange-yellow gradient icon -->
<link rel="icon" type="image/svg+xml"
href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'%3E%3Cdefs%3E%3ClinearGradient id='g' x1='0%25' y1='0%25' x2='100%25' y2='100%25'%3E%3Cstop offset='0%25' stop-color='%23f59e0b'/%3E%3Cstop offset='100%25' stop-color='%23ef4444'/%3E%3C/linearGradient%3E%3C/defs%3E%3Crect rx='20' width='100' height='100' fill='url(%23g)'/%3E%3Ctext x='50' y='68' font-family='Arial,sans-serif' font-size='48' font-weight='bold' fill='white' text-anchor='middle'%3EDS%3C/text%3E%3C/svg%3E" />
<!-- Fonts -->
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@100..900&display=swap" rel="stylesheet">
<script type="module" crossorigin src="/admin/assets/index-BZnWnZUg.js"></script>
<link rel="stylesheet" crossorigin href="/admin/assets/index-Dd4LAu0y.css">
</head>
<body>
<div id="root"></div>
</body>
</html>

View File

@@ -0,0 +1,9 @@
package webui
import "embed"
// embeddedAdminFS bundles the built admin webui so serverless runtime does not
// depend on function includeFiles behavior.
//
//go:embed assets/admin
var embeddedAdminFS embed.FS

View File

@@ -1,8 +1,10 @@
package webui
import (
"io/fs"
"net/http"
"os"
"path"
"path/filepath"
"strings"
@@ -17,11 +19,18 @@ const welcomeHTML = `<!DOCTYPE html>
</head><body><main><h1>DS2API</h1><p>DeepSeek to OpenAI & Claude Compatible API</p><div class="links"><a href="/admin">管理面板</a><a href="/v1/models">API 状态</a><a href="https://github.com/CJackHwang/ds2api" target="_blank">GitHub</a></div></main></body></html>`
type Handler struct {
StaticDir string
StaticDir string
embeddedAdmin fs.FS
hasEmbeddedUI bool
}
func NewHandler() *Handler {
return &Handler{StaticDir: resolveStaticAdminDir(config.StaticAdminDir())}
h := &Handler{StaticDir: resolveStaticAdminDir(config.StaticAdminDir())}
if sub, err := fs.Sub(embeddedAdminFS, "assets/admin"); err == nil {
h.embeddedAdmin = sub
h.hasEmbeddedUI = true
}
return h
}
func RegisterRoutes(r chi.Router, h *Handler) {
@@ -52,6 +61,11 @@ func (h *Handler) admin(w http.ResponseWriter, r *http.Request) {
h.serveFromDisk(w, r, staticDir)
return
}
if h.hasEmbeddedUI {
if h.serveFromFS(w, r, h.embeddedAdmin) {
return
}
}
http.Error(w, "WebUI not built. Run `cd webui && npm run build` first.", http.StatusNotFound)
}
@@ -116,3 +130,34 @@ func resolveStaticAdminDir(preferred string) string {
}
return filepath.Clean(preferred)
}
func (h *Handler) serveFromFS(w http.ResponseWriter, r *http.Request, rootFS fs.FS) bool {
rel := strings.TrimPrefix(r.URL.Path, "/admin")
rel = strings.TrimPrefix(rel, "/")
safe := strings.TrimPrefix(path.Clean("/"+rel), "/")
if strings.HasPrefix(safe, "../") {
http.NotFound(w, r)
return true
}
if safe != "" && strings.Contains(safe, ".") {
if _, err := fs.Stat(rootFS, safe); err != nil {
http.NotFound(w, r)
return true
}
if strings.HasPrefix(safe, "assets/") {
w.Header().Set("Cache-Control", "public, max-age=31536000, immutable")
} else {
w.Header().Set("Cache-Control", "no-store, must-revalidate")
}
http.ServeFileFS(w, r, rootFS, safe)
return true
}
if _, err := fs.Stat(rootFS, "index.html"); err != nil {
return false
}
w.Header().Set("Cache-Control", "no-store, must-revalidate")
http.ServeFileFS(w, r, rootFS, "index.html")
return true
}

View File

@@ -1,10 +1,5 @@
{
"version": 2,
"functions": {
"api/index.go": {
"includeFiles": "{static/admin/**,sha3_wasm_bg.7b9ca65ddd.wasm}"
}
},
"rewrites": [
{
"source": "/(.*)",