diff options
Diffstat (limited to 'internal')
| -rw-r--r-- | internal/api/server.go | 36 | ||||
| -rw-r--r-- | internal/cli/serve.go | 7 |
2 files changed, 40 insertions, 3 deletions
diff --git a/internal/api/server.go b/internal/api/server.go index 2dcbf77..afb80ad 100644 --- a/internal/api/server.go +++ b/internal/api/server.go @@ -1,9 +1,11 @@ package api import ( + "bytes" "context" "encoding/json" "fmt" + "io/fs" "log/slog" "net/http" "os" @@ -60,6 +62,7 @@ type Server struct { pushStore pushSubscriptionStore dropsDir string llm *llm.Client + basePath string // URL prefix the UI is mounted at, e.g. "/claudomator" } // SetAPIToken configures a bearer token that must be supplied to access the API. @@ -89,6 +92,12 @@ func (s *Server) SetWorkspaceRoot(path string) { s.workspaceRoot = path } +// SetBasePath sets the URL prefix the UI is served under (e.g. "/claudomator-oss"). +// The server rewrites the base-path meta tag in index.html at request time. +func (s *Server) SetBasePath(p string) { + s.basePath = p +} + // Pool returns the executor pool, for graceful shutdown by the caller. func (s *Server) Pool() *executor.Pool { return s.pool } @@ -180,7 +189,7 @@ func (s *Server) routes() { s.mux.HandleFunc("GET /api/drops", s.handleListDrops) s.mux.HandleFunc("GET /api/drops/{filename}", s.handleGetDrop) s.mux.HandleFunc("POST /api/drops", s.handlePostDrop) - s.mux.Handle("GET /", http.FileServerFS(webui.Files)) + s.mux.HandleFunc("GET /", s.handleStaticFiles) } // forwardResults listens on the executor pool's result and started channels and broadcasts via WebSocket. @@ -777,6 +786,31 @@ func (s *Server) handleGetExecution(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, exec) } +// handleStaticFiles serves embedded web UI files. For index.html it rewrites the +// base-path meta tag so the UI knows which API prefix to use. +func (s *Server) handleStaticFiles(w http.ResponseWriter, r *http.Request) { + if r.URL.Path == "/" || r.URL.Path == "/index.html" { + raw, err := fs.ReadFile(webui.Files, "index.html") + if err != nil { + http.Error(w, "not found", http.StatusNotFound) + return + } + basePath := s.basePath + if basePath == "" { + basePath = "/claudomator" + } + rewritten := bytes.ReplaceAll(raw, + []byte(`content="/claudomator"`), + []byte(`content="`+basePath+`"`), + ) + w.Header().Set("Content-Type", "text/html; charset=utf-8") + w.WriteHeader(http.StatusOK) + _, _ = w.Write(rewritten) + return + } + http.FileServerFS(webui.Files).ServeHTTP(w, r) +} + func writeJSON(w http.ResponseWriter, status int, v interface{}) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(status) diff --git a/internal/cli/serve.go b/internal/cli/serve.go index dae66d6..ae42780 100644 --- a/internal/cli/serve.go +++ b/internal/cli/serve.go @@ -21,6 +21,7 @@ import ( func newServeCmd() *cobra.Command { var addr string var workspaceRoot string + var basePath string cmd := &cobra.Command{ Use: "serve", @@ -29,11 +30,12 @@ func newServeCmd() *cobra.Command { if cmd.Flags().Changed("workspace-root") { cfg.WorkspaceRoot = workspaceRoot } - return serve(addr) + return serve(addr, basePath) }, } cmd.Flags().StringVar(&addr, "addr", ":8484", "listen address") + cmd.Flags().StringVar(&basePath, "base-path", "/claudomator", "URL prefix the UI is served under (e.g. /claudomator-oss)") cmd.Flags().StringVar(&workspaceRoot, "workspace-root", "/workspace", "root directory for listing workspaces") cmd.Flags().StringVar(&cfg.ClaudeImage, "claude-image", cfg.ClaudeImage, "docker image for claude agents") cmd.Flags().StringVar(&cfg.GeminiImage, "gemini-image", cfg.GeminiImage, "docker image for gemini agents") @@ -41,7 +43,7 @@ func newServeCmd() *cobra.Command { return cmd } -func serve(addr string) error { +func serve(addr, basePath string) error { if err := cfg.EnsureDirs(); err != nil { return fmt.Errorf("creating dirs: %w", err) } @@ -153,6 +155,7 @@ func serve(addr string) error { pool.RecoverStaleBlocked() srv := api.NewServer(store, pool, logger, cfg.ClaudeBinaryPath, cfg.GeminiBinaryPath) + srv.SetBasePath(basePath) // Configure notifiers: combine webhook (if set) with web push. notifiers := []notify.Notifier{} |
