fix: ship webui assets in vercel go deployment
This commit is contained in:
1 parent
056c50676f
commit
a32154f881
7 files changed
+352
-59
No files matched your search
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user