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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,6 +202,60 @@ func TestReadOnlyEndpointsRejectUnsupportedMethods(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestStaticFrontendServesIndexAndAssets(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
staticDir := filepath.Join(root, "dist")
|
||||
if err := os.MkdirAll(filepath.Join(staticDir, "assets"), 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(staticDir, "index.html"), []byte(`<html><title>管理台</title><script src="/assets/app.js"></script></html>`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(staticDir, "assets", "app.js"), []byte(`console.log("ok")`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
handler := New(app.Config{CodexHome: root, HTTPAddr: "127.0.0.1:0", StaticDir: staticDir})
|
||||
|
||||
for _, path := range []string{"/", "/workflow"} {
|
||||
req := httptest.NewRequest(http.MethodGet, path, nil)
|
||||
rec := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("%s status = %d, body = %s", path, rec.Code, rec.Body.String())
|
||||
}
|
||||
if !strings.Contains(rec.Body.String(), "<title>管理台</title>") {
|
||||
t.Fatalf("%s did not serve index.html: %s", path, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/assets/app.js", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), `console.log("ok")`) {
|
||||
t.Fatalf("asset status = %d, body = %s", rec.Code, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestStaticFrontendDoesNotMaskMissingAPIRoutes(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
staticDir := filepath.Join(root, "dist")
|
||||
if err := os.MkdirAll(staticDir, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(staticDir, "index.html"), []byte(`<html>index</html>`), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/api/missing", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
New(app.Config{CodexHome: root, HTTPAddr: "127.0.0.1:0", StaticDir: staticDir}).ServeHTTP(rec, req)
|
||||
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Fatalf("status = %d, want %d, body = %s", rec.Code, http.StatusNotFound, rec.Body.String())
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgentValidateEndpointReturnsDiffAndRejectsUnsupportedMethods(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
agentsDir := filepath.Join(root, "agents")
|
||||
|
||||
Reference in New Issue
Block a user