accounts/cmd/cli/main.go
2025-08-09 15:54:27 +08:00

53 lines
1.2 KiB
Go

package main
import (
"context"
"flag"
"io"
"log"
"net/http"
"os"
"time"
"xcontrol/server/proxy"
rconfig "xcontrol/server/rag/config"
)
// main loads server RAG configuration and triggers a manual sync by
// calling the running API server's /api/rag/sync endpoint.
func main() {
configPath := flag.String("config", "", "Path to server RAG configuration file")
flag.Parse()
if *configPath != "" {
cfg, err := rconfig.Load(*configPath)
if err != nil {
log.Fatalf("load config: %v", err)
}
proxy.Set(cfg.Global.Proxy)
}
baseURL := os.Getenv("SERVER_URL")
if baseURL == "" {
baseURL = "http://localhost:8080"
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Minute)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodPost, baseURL+"/api/rag/sync", nil)
if err != nil {
log.Fatalf("create request: %v", err)
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Fatalf("sync request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
b, _ := io.ReadAll(resp.Body)
log.Fatalf("sync failed: %s", string(b))
}
if _, err := io.Copy(os.Stdout, resp.Body); err != nil {
log.Fatalf("read response: %v", err)
}
}