From 72dc76ec74f7c472c2a61a489e3be4cf04da6f83 Mon Sep 17 00:00:00 2001 From: yusing Date: Tue, 11 Feb 2025 12:52:16 +0800 Subject: [PATCH] api: add system_info endpoint --- internal/api/handler.go | 2 ++ internal/api/v1/system_info.go | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 internal/api/v1/system_info.go diff --git a/internal/api/handler.go b/internal/api/handler.go index 5e9e4b2..dd988dd 100644 --- a/internal/api/handler.go +++ b/internal/api/handler.go @@ -39,6 +39,8 @@ func NewHandler(cfg config.ConfigInstance) http.Handler { mux.HandleFunc("GET", "/v1/logs/ws", auth.RequireAuth(memlogger.LogsWS(cfg))) mux.HandleFunc("GET", "/v1/favicon", auth.RequireAuth(favicon.GetFavIcon)) mux.HandleFunc("POST", "/v1/homepage/set", auth.RequireAuth(v1.SetHomePageOverrides)) + mux.HandleFunc("GET", "/v1/system_info", auth.RequireAuth(useCfg(cfg, v1.SystemInfo))) + mux.HandleFunc("GET", "/v1/system_info/{agent_name}", auth.RequireAuth(useCfg(cfg, v1.SystemInfo))) if common.PrometheusEnabled { mux.Handle("GET /v1/metrics", promhttp.Handler()) diff --git a/internal/api/v1/system_info.go b/internal/api/v1/system_info.go new file mode 100644 index 0000000..a5a2502 --- /dev/null +++ b/internal/api/v1/system_info.go @@ -0,0 +1,38 @@ +package v1 + +import ( + "net/http" + + agentPkg "github.com/yusing/go-proxy/agent/pkg/agent" + U "github.com/yusing/go-proxy/internal/api/v1/utils" + config "github.com/yusing/go-proxy/internal/config/types" + "github.com/yusing/go-proxy/internal/metrics" +) + +func SystemInfo(cfg config.ConfigInstance, w http.ResponseWriter, r *http.Request) { + agentName := r.FormValue("agent_name") + if agentName == "" { + info, err := metrics.GetSystemInfo(r.Context()) + if err != nil { + U.HandleErr(w, r, err) + return + } + U.RespondJSON(w, r, info) + } else { + agent, ok := cfg.GetAgent(agentName) + if !ok { + U.HandleErr(w, r, U.ErrInvalidKey("agent_name"), http.StatusNotFound) + return + } + respData, status, err := agent.Fetch(r.Context(), agentPkg.EndpointSystemInfo) + if err != nil { + U.HandleErr(w, r, err) + return + } + if status != http.StatusOK { + http.Error(w, string(respData), status) + return + } + U.RespondJSON(w, r, respData) + } +}