Files
agentmon/cmd/web-ui/main.go
T
William Valentin 256b841cbf feat: scaffold agentmon services and k8s deploy
Adds Go microservices (ingest-gateway, event-processor, query-api, web-ui), NATS+Postgres wiring, initial schema/init job, ingress manifests for LAN+tailnet, and a multi-arch image build script.
2026-01-17 01:06:57 -08:00

57 lines
1.6 KiB
Go

package main
import (
"encoding/json"
"io"
"log"
"net/http"
"os"
)
func main() {
addr := envDefault("AGENTMON_UI_ADDR", ":8082")
queryAPIBase := envDefault("AGENTMON_QUERY_BASE", "http://query-api")
mux := http.NewServeMux()
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("ok"))
})
mux.HandleFunc("/api/events", func(w http.ResponseWriter, r *http.Request) {
resp, err := http.Get(queryAPIBase + "/v1/events?limit=100")
if err != nil {
w.WriteHeader(http.StatusBadGateway)
_, _ = w.Write([]byte("query-api unreachable"))
return
}
defer resp.Body.Close()
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(resp.StatusCode)
_, _ = io.Copy(w, resp.Body)
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
payload, _ := json.Marshal(map[string]any{"query_api": queryAPIBase})
_, _ = w.Write([]byte("<html><body><h1>agentmon</h1><p>Recent events:</p><pre id='out'>loading...</pre>"))
_, _ = w.Write([]byte("<script>\n"))
_, _ = w.Write([]byte("const cfg=" + string(payload) + ";\n"))
_, _ = w.Write([]byte("fetch('/api/events').then(r=>r.json()).then(j=>{document.getElementById('out').textContent=JSON.stringify(j,null,2);}).catch(e=>{document.getElementById('out').textContent=String(e);});\n"))
_, _ = w.Write([]byte("</script></body></html>"))
})
log.Printf("web-ui listening on %s", addr)
log.Fatal(http.ListenAndServe(addr, mux))
}
func envDefault(key, def string) string {
if v := os.Getenv(key); v != "" {
return v
}
return def
}