From cc2654c371b3d13476ae8db0aa21f6379870da95 Mon Sep 17 00:00:00 2001 From: Simon Zolin Date: Thu, 8 Aug 2019 12:40:41 +0300 Subject: [PATCH] + /control/stats_config, /control/stats_info --- home/control_stats.go | 54 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 home/control_stats.go diff --git a/home/control_stats.go b/home/control_stats.go new file mode 100644 index 00000000..0dce5510 --- /dev/null +++ b/home/control_stats.go @@ -0,0 +1,54 @@ +package home + +import ( + "encoding/json" + "net/http" + + "github.com/AdguardTeam/golibs/log" +) + +type statsConfig struct { + Interval uint `json:"interval"` +} + +func handleStatsInfo(w http.ResponseWriter, r *http.Request) { + log.Tracef("%s %v", r.Method, r.URL) + + resp := statsConfig{} + resp.Interval = config.DNS.StatsInterval + + jsonVal, err := json.Marshal(resp) + if err != nil { + httpError(w, http.StatusInternalServerError, "json encode: %s", err) + return + } + w.Header().Set("Content-Type", "application/json") + _, err = w.Write(jsonVal) + if err != nil { + httpError(w, http.StatusInternalServerError, "http write: %s", err) + } +} + +func handleStatsConfig(w http.ResponseWriter, r *http.Request) { + log.Tracef("%s %v", r.Method, r.URL) + + reqData := statsConfig{} + err := json.NewDecoder(r.Body).Decode(&reqData) + if err != nil { + httpError(w, http.StatusBadRequest, "json decode: %s", err) + return + } + + if !checkStatsInterval(reqData.Interval) { + httpError(w, http.StatusBadRequest, "Unsupported interval") + return + } + + config.DNS.StatsInterval = reqData.Interval + + httpUpdateConfigReloadDNSReturnOK(w, r) +} + +func checkStatsInterval(i uint) bool { + return i == 1 || i == 7 || i == 30 || i == 90 +}