mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-09-22 06:46:34 +08:00
Add API endpoint to fetch redis info
This commit is contained in:
46
redis_info_handlers.go
Normal file
46
redis_info_handlers.go
Normal file
@@ -0,0 +1,46 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
// ****************************************************************************
|
||||
// This file defines:
|
||||
// - http.Handler(s) for redis info related endpoints
|
||||
// ****************************************************************************
|
||||
|
||||
func newRedisInfoHandlerFunc(rdb *redis.Client) http.HandlerFunc {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.Background()
|
||||
res, err := rdb.Info(ctx).Result()
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
info := parseRedisInfo(res)
|
||||
if err := json.NewEncoder(w).Encode(info); err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Parses the return value from the INFO command.
|
||||
// See https://redis.io/commands/info#return-value.
|
||||
func parseRedisInfo(infoStr string) map[string]string {
|
||||
info := make(map[string]string)
|
||||
lines := strings.Split(infoStr, "\r\n")
|
||||
for _, l := range lines {
|
||||
kv := strings.Split(l, ":")
|
||||
if len(kv) == 2 {
|
||||
info[kv[0]] = kv[1]
|
||||
}
|
||||
}
|
||||
return info
|
||||
|
||||
}
|
Reference in New Issue
Block a user