feat: add docker deployment
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user