diff options
Diffstat (limited to 'internal/api')
| -rw-r--r-- | internal/api/server.go | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/internal/api/server.go b/internal/api/server.go index ffa8cd4..fe883bb 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" @@ -62,6 +64,7 @@ type Server struct { dropsDir string llm *llm.Client budget budgetReporter // optional; per-provider spend headroom for GET /api/budget + basePath string // URL prefix the UI is mounted at, e.g. "/claudomator-oss" } // SetAPIToken configures a bearer token that must be supplied to access the API. @@ -91,6 +94,11 @@ func (s *Server) SetWorkspaceRoot(path string) { s.workspaceRoot = path } +// SetBasePath sets the URL prefix the UI is served under (e.g. "/claudomator-oss"). +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 } @@ -199,7 +207,7 @@ func (s *Server) routes() { s.mux.Handle("POST /chatbot/mcp", chatbotHandler) s.mux.Handle("GET /chatbot/mcp", chatbotHandler) s.mux.Handle("DELETE /chatbot/mcp", chatbotHandler) - 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. @@ -695,6 +703,29 @@ func (s *Server) handleGetExecution(w http.ResponseWriter, r *http.Request) { writeJSON(w, http.StatusOK, exec) } +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) |
