From 7eeb81293b62997c58bc6487dce827f8d37a2407 Mon Sep 17 00:00:00 2001 From: Finessse Date: Wed, 17 Dec 2025 19:11:32 +0700 Subject: [PATCH] cmd: add cliproxymgr local UI --- cmd/cliproxymgr/main.go | 198 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 cmd/cliproxymgr/main.go diff --git a/cmd/cliproxymgr/main.go b/cmd/cliproxymgr/main.go new file mode 100644 index 00000000..6370577b --- /dev/null +++ b/cmd/cliproxymgr/main.go @@ -0,0 +1,198 @@ +package main + +import ( + "encoding/json" + "flag" + "fmt" + "log" + "net/http" + "os" + "strings" + "time" + + "github.com/router-for-me/CLIProxyAPI/v6/internal/desktopctl" +) + +func main() { + var ( + uiAddr string + repoRoot string + configPath string + exePath string + noOpen bool + ) + flag.StringVar(&uiAddr, "listen", "127.0.0.1:7331", "UI listen address") + flag.StringVar(&repoRoot, "repo", "", "Repo root (used to locate bin/ and logs/)") + flag.StringVar(&configPath, "config", "", "Path to config.yaml (defaults to /config.yaml)") + flag.StringVar(&exePath, "exe", "", "Path to CLIProxyAPI binary (defaults to /bin/cliproxyapi-latest.exe)") + flag.BoolVar(&noOpen, "no-open", false, "Don't auto-open browser") + flag.Parse() + + mux := http.NewServeMux() + + mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + if r.URL.Path != "/" { + http.NotFound(w, r) + return + } + w.Header().Set("Content-Type", "text/html; charset=utf-8") + _, _ = w.Write([]byte(indexHTML)) + }) + + mux.HandleFunc("/api/status", func(w http.ResponseWriter, r *http.Request) { + st, _ := desktopctl.StatusFor(configPath) + writeJSON(w, st) + }) + mux.HandleFunc("/api/start", func(w http.ResponseWriter, r *http.Request) { + st, err := desktopctl.Start(desktopctl.StartOptions{RepoRoot: repoRoot, ConfigPath: configPath, ExePath: exePath}) + if err != nil { + writeErr(w, err) + return + } + writeJSON(w, st) + }) + mux.HandleFunc("/api/stop", func(w http.ResponseWriter, r *http.Request) { + if err := desktopctl.Stop(desktopctl.StopOptions{}); err != nil { + writeErr(w, err) + return + } + writeJSON(w, map[string]any{"ok": true}) + }) + mux.HandleFunc("/api/restart", func(w http.ResponseWriter, r *http.Request) { + st, err := desktopctl.Restart(desktopctl.StartOptions{RepoRoot: repoRoot, ConfigPath: configPath, ExePath: exePath}) + if err != nil { + writeErr(w, err) + return + } + writeJSON(w, st) + }) + mux.HandleFunc("/api/open-ui", func(w http.ResponseWriter, r *http.Request) { + if err := desktopctl.OpenManagementUI(configPath); err != nil { + writeErr(w, err) + return + } + writeJSON(w, map[string]any{"ok": true}) + }) + mux.HandleFunc("/api/open-logs", func(w http.ResponseWriter, r *http.Request) { + if err := desktopctl.OpenLogsFolder(repoRoot, configPath); err != nil { + writeErr(w, err) + return + } + writeJSON(w, map[string]any{"ok": true}) + }) + + srv := &http.Server{ + Addr: uiAddr, + Handler: mux, + ReadHeaderTimeout: 5 * time.Second, + } + + url := "http://" + uiAddr + if strings.HasPrefix(uiAddr, ":") { + url = "http://127.0.0.1" + uiAddr + } + if !noOpen { + _ = desktopctl.OpenBrowser(url) + } + + log.Printf("cliproxymgr listening on %s", url) + if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} + +func writeJSON(w http.ResponseWriter, v any) { + w.Header().Set("Content-Type", "application/json; charset=utf-8") + enc := json.NewEncoder(w) + enc.SetIndent("", " ") + _ = enc.Encode(v) +} + +func writeErr(w http.ResponseWriter, err error) { + w.WriteHeader(http.StatusBadRequest) + writeJSON(w, map[string]any{"error": err.Error()}) +} + +const indexHTML = ` + + + + + CLIProxyAPI Manager + + + +

CLIProxyAPI Manager

+
+
+
Status: …
+
+
+
+
+ + + + + +
+
+
Snippets (updates when running)
+
Loading…
+
+
+ + + +`