mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-23 10:57:44 +08:00
fix: embed admin webui for serverless runtime
This commit is contained in:
9
embedded_admin.go
Normal file
9
embedded_admin.go
Normal file
@@ -0,0 +1,9 @@
|
||||
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,13 +1,16 @@
|
||||
package webui
|
||||
|
||||
import (
|
||||
"io/fs"
|
||||
"net/http"
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/go-chi/chi/v5"
|
||||
|
||||
root "ds2api"
|
||||
"ds2api/internal/config"
|
||||
)
|
||||
|
||||
@@ -17,11 +20,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: config.StaticAdminDir()}
|
||||
h := &Handler{StaticDir: config.StaticAdminDir()}
|
||||
if sub, err := fs.Sub(root.EmbeddedAdminFS, "static/admin"); err == nil {
|
||||
h.embeddedAdmin = sub
|
||||
h.hasEmbeddedUI = true
|
||||
}
|
||||
return h
|
||||
}
|
||||
|
||||
func RegisterRoutes(r chi.Router, h *Handler) {
|
||||
@@ -47,10 +57,19 @@ func (h *Handler) index(w http.ResponseWriter, _ *http.Request) {
|
||||
}
|
||||
|
||||
func (h *Handler) admin(w http.ResponseWriter, r *http.Request) {
|
||||
if fi, err := os.Stat(h.StaticDir); err != nil || !fi.IsDir() {
|
||||
http.Error(w, "WebUI not built. Run `cd webui && npm run build` first.", http.StatusNotFound)
|
||||
if fi, err := os.Stat(h.StaticDir); err == nil && fi.IsDir() {
|
||||
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)
|
||||
}
|
||||
|
||||
func (h *Handler) serveFromDisk(w http.ResponseWriter, r *http.Request) {
|
||||
path := strings.TrimPrefix(r.URL.Path, "/admin")
|
||||
path = strings.TrimPrefix(path, "/")
|
||||
if path != "" && strings.Contains(path, ".") {
|
||||
@@ -79,3 +98,34 @@ func (h *Handler) admin(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