mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-05 00:45:29 +08:00
fix: ship webui assets in vercel go deployment
This commit is contained in:
3
.gitignore
vendored
3
.gitignore
vendored
@@ -64,8 +64,7 @@ pnpm-lock.yaml
|
||||
*.tsbuildinfo
|
||||
.cache/
|
||||
.parcel-cache/
|
||||
static/admin/*
|
||||
!static/admin/.gitkeep
|
||||
# static/admin build output is versioned for serverless deployments
|
||||
|
||||
# Environment
|
||||
.env.local
|
||||
|
||||
@@ -1,9 +0,0 @@
|
||||
package ds2api
|
||||
|
||||
import "embed"
|
||||
|
||||
// EmbeddedAdminFS bundles static/admin so serverless targets can serve WebUI
|
||||
// without relying on runtime filesystem inclusion.
|
||||
//
|
||||
//go:embed static/admin
|
||||
var EmbeddedAdminFS embed.FS
|
||||
@@ -1,16 +1,13 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
root "ds2api"
|
||||
"ds2api/internal/config"
|
||||
)
|
||||
|
||||
@@ -20,18 +17,11 @@ 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
|
||||
embeddedAdmin fs.FS
|
||||
hasEmbeddedUI bool
|
||||
StaticDir string
|
||||
}
|
||||
|
||||
func NewHandler() *Handler {
|
||||
h := &Handler{StaticDir: config.StaticAdminDir()}
|
||||
if sub, err := fs.Sub(root.EmbeddedAdminFS, "static/admin"); err == nil {
|
||||
h.embeddedAdmin = sub
|
||||
h.hasEmbeddedUI = true
|
||||
}
|
||||
return h
|
||||
return &Handler{StaticDir: config.StaticAdminDir()}
|
||||
}
|
||||
|
||||
func RegisterRoutes(r chi.Router, h *Handler) {
|
||||
@@ -61,11 +51,6 @@ func (h *Handler) admin(w http.ResponseWriter, r *http.Request) {
|
||||
h.serveFromDisk(w, r)
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -98,34 +83,3 @@ func (h *Handler) serveFromDisk(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Cache-Control", "no-store, must-revalidate")
|
||||
http.ServeFile(w, r, index)
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
297
static/admin/assets/index-BZnWnZUg.js
Normal file
297
static/admin/assets/index-BZnWnZUg.js
Normal file
File diff suppressed because one or more lines are too long
1
static/admin/assets/index-Dd4LAu0y.css
Normal file
1
static/admin/assets/index-Dd4LAu0y.css
Normal file
File diff suppressed because one or more lines are too long
43
static/admin/index.html
Normal file
43
static/admin/index.html
Normal 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>
|
||||
@@ -1,5 +1,13 @@
|
||||
{
|
||||
"version": 2,
|
||||
"functions": {
|
||||
"api/index.go": {
|
||||
"includeFiles": [
|
||||
"static/admin/**",
|
||||
"sha3_wasm_bg.7b9ca65ddd.wasm"
|
||||
]
|
||||
}
|
||||
},
|
||||
"rewrites": [
|
||||
{
|
||||
"source": "/(.*)",
|
||||
|
||||
Reference in New Issue
Block a user