asynqmon/redis_info_handlers.go
Chris Gaffney 5cd001daee Add redis-url and redis-insecure-tls command line options
A URL can contain all of the information in the individual flags as a
single string rather than as separate pieces. I've mostly run into URLs
pointing to Redis and this make those existing URLs easier to work with.

This also introduces the -redis-insecure-tls flag which turns off TLS
certificate hostname verification. We've chosen Heroku Redis for a
recent project which requires TLS but, for reasons I don't know, they
don't provide a certificate that is valid for the hostname. I also
wasn't able to get the existing -redis-tls flag to work.
2021-04-26 16:30:27 -07:00

58 lines
1.4 KiB
Go

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
// ****************************************************************************
type RedisInfoResponse struct {
Addr string `json:"address"`
Info map[string]string `json:"info"`
RawInfo string `json:"raw_info"`
}
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)
resp := RedisInfoResponse{
Addr: rdb.Options().Addr,
Info: info,
RawInfo: res,
}
if err := json.NewEncoder(w).Encode(resp); 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
}