api: add system_info endpoint

This commit is contained in:
yusing 2025-02-11 12:52:16 +08:00
parent 71619042fd
commit 72dc76ec74
2 changed files with 40 additions and 0 deletions

View file

@ -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/logs/ws", auth.RequireAuth(memlogger.LogsWS(cfg)))
mux.HandleFunc("GET", "/v1/favicon", auth.RequireAuth(favicon.GetFavIcon)) mux.HandleFunc("GET", "/v1/favicon", auth.RequireAuth(favicon.GetFavIcon))
mux.HandleFunc("POST", "/v1/homepage/set", auth.RequireAuth(v1.SetHomePageOverrides)) 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 { if common.PrometheusEnabled {
mux.Handle("GET /v1/metrics", promhttp.Handler()) mux.Handle("GET /v1/metrics", promhttp.Handler())

View file

@ -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)
}
}