feat: add docker deployment

This commit is contained in:
Yoilun
2026-05-25 22:41:12 +08:00
parent 8c1093a53c
commit 4262191462
9 changed files with 225 additions and 4 deletions

View File

@@ -6,6 +6,8 @@ import (
"io"
"net/http"
"os"
"path"
"path/filepath"
"strings"
"codex-agent-manager/internal/agents"
@@ -110,6 +112,10 @@ func New(cfg app.Config) http.Handler {
"source": view.Source,
})
})
mux.HandleFunc("/api/", http.NotFound)
if cfg.StaticDir != "" {
mux.HandleFunc("/", staticFrontendHandler(cfg.StaticDir))
}
return mux
}
@@ -211,3 +217,25 @@ func writeJSON(w http.ResponseWriter, status int, body any) {
w.WriteHeader(status)
_ = json.NewEncoder(w).Encode(body)
}
func staticFrontendHandler(staticDir string) http.HandlerFunc {
fileServer := http.FileServer(http.Dir(staticDir))
indexPath := filepath.Join(staticDir, "index.html")
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet && r.Method != http.MethodHead {
writeJSON(w, http.StatusMethodNotAllowed, map[string]string{"error": "方法不允许"})
return
}
cleanPath := strings.TrimPrefix(path.Clean("/"+r.URL.Path), "/")
if cleanPath == "" {
http.ServeFile(w, r, indexPath)
return
}
target := filepath.Join(staticDir, cleanPath)
if info, err := os.Stat(target); err == nil && !info.IsDir() {
fileServer.ServeHTTP(w, r)
return
}
http.ServeFile(w, r, indexPath)
}
}