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

@@ -9,14 +9,19 @@ type Config struct {
CodexHome string
HTTPAddr string
WorkspaceRoot string
StaticDir string
}
func DefaultConfig() Config {
httpAddr := envOrDefault("HTTP_ADDR", "127.0.0.1:18083")
staticDir := os.Getenv("STATIC_DIR")
workspaceRoot := envOrDefault("WORKSPACE_ROOT", ".")
if codexHome := os.Getenv("CODEX_HOME"); codexHome != "" {
return Config{
CodexHome: codexHome,
HTTPAddr: "127.0.0.1:18083",
WorkspaceRoot: ".",
HTTPAddr: httpAddr,
WorkspaceRoot: workspaceRoot,
StaticDir: staticDir,
}
}
home, err := os.UserHomeDir()
@@ -25,7 +30,15 @@ func DefaultConfig() Config {
}
return Config{
CodexHome: filepath.Join(home, ".codex"),
HTTPAddr: "127.0.0.1:18083",
WorkspaceRoot: ".",
HTTPAddr: httpAddr,
WorkspaceRoot: workspaceRoot,
StaticDir: staticDir,
}
}
func envOrDefault(name string, fallback string) string {
if value := os.Getenv(name); value != "" {
return value
}
return fallback
}

View File

@@ -17,6 +17,24 @@ func TestDefaultConfigUsesCODEXHomeOverride(t *testing.T) {
}
}
func TestDefaultConfigUsesDockerRuntimeOverrides(t *testing.T) {
t.Setenv("HTTP_ADDR", "0.0.0.0:18083")
t.Setenv("STATIC_DIR", "/app/web/dist")
t.Setenv("WORKSPACE_ROOT", "/app")
cfg := DefaultConfig()
if cfg.HTTPAddr != "0.0.0.0:18083" {
t.Fatalf("HTTPAddr = %q, want %q", cfg.HTTPAddr, "0.0.0.0:18083")
}
if cfg.StaticDir != "/app/web/dist" {
t.Fatalf("StaticDir = %q, want %q", cfg.StaticDir, "/app/web/dist")
}
if cfg.WorkspaceRoot != "/app" {
t.Fatalf("WorkspaceRoot = %q, want %q", cfg.WorkspaceRoot, "/app")
}
}
func TestDefaultConfigFallsBackToUserCodexHome(t *testing.T) {
t.Setenv("CODEX_HOME", "")
home, err := os.UserHomeDir()

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)
}
}

View File

@@ -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")