Pull request: all: fix lint and naming issues
Merge in DNS/adguard-home from 2276-fix-lint to master Updates #2276. Squashed commit of the following: commit 433f44cc7b674a20ed60a9d29466ba888b3ef66e Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Mon Dec 7 14:14:28 2020 +0300 querylog: improve code and documentation commit 851df97d2a87de5e7180a502055ee6f1a6defdca Author: Ainar Garipov <A.Garipov@AdGuard.COM> Date: Fri Dec 4 20:36:32 2020 +0300 all: fix lint and naming issues
This commit is contained in:
103
internal/stats/http.go
Normal file
103
internal/stats/http.go
Normal file
@@ -0,0 +1,103 @@
|
||||
// HTTP request handlers for accessing statistics data and configuration settings
|
||||
|
||||
package stats
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/AdguardTeam/golibs/log"
|
||||
)
|
||||
|
||||
func httpError(r *http.Request, w http.ResponseWriter, code int, format string, args ...interface{}) {
|
||||
text := fmt.Sprintf(format, args...)
|
||||
|
||||
log.Info("Stats: %s %s: %s", r.Method, r.URL, text)
|
||||
|
||||
http.Error(w, text, code)
|
||||
}
|
||||
|
||||
// Return data
|
||||
func (s *statsCtx) handleStats(w http.ResponseWriter, r *http.Request) {
|
||||
start := time.Now()
|
||||
d := s.getData()
|
||||
log.Debug("Stats: prepared data in %v", time.Since(start))
|
||||
|
||||
if d == nil {
|
||||
httpError(r, w, http.StatusInternalServerError, "Couldn't get statistics data")
|
||||
return
|
||||
}
|
||||
|
||||
data, err := json.Marshal(d)
|
||||
if err != nil {
|
||||
httpError(r, w, http.StatusInternalServerError, "json encode: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
|
||||
_, err = w.Write(data)
|
||||
if err != nil {
|
||||
httpError(r, w, http.StatusInternalServerError, "json encode: %s", err)
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
type config struct {
|
||||
IntervalDays uint32 `json:"interval"`
|
||||
}
|
||||
|
||||
// Get configuration
|
||||
func (s *statsCtx) handleStatsInfo(w http.ResponseWriter, r *http.Request) {
|
||||
resp := config{}
|
||||
resp.IntervalDays = s.conf.limit / 24
|
||||
|
||||
data, err := json.Marshal(resp)
|
||||
if err != nil {
|
||||
httpError(r, w, http.StatusInternalServerError, "json encode: %s", err)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
_, err = w.Write(data)
|
||||
if err != nil {
|
||||
httpError(r, w, http.StatusInternalServerError, "http write: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Set configuration
|
||||
func (s *statsCtx) handleStatsConfig(w http.ResponseWriter, r *http.Request) {
|
||||
reqData := config{}
|
||||
err := json.NewDecoder(r.Body).Decode(&reqData)
|
||||
if err != nil {
|
||||
httpError(r, w, http.StatusBadRequest, "json decode: %s", err)
|
||||
return
|
||||
}
|
||||
|
||||
if !checkInterval(reqData.IntervalDays) {
|
||||
httpError(r, w, http.StatusBadRequest, "Unsupported interval")
|
||||
return
|
||||
}
|
||||
|
||||
s.setLimit(int(reqData.IntervalDays))
|
||||
s.conf.ConfigModified()
|
||||
}
|
||||
|
||||
// Reset data
|
||||
func (s *statsCtx) handleStatsReset(w http.ResponseWriter, r *http.Request) {
|
||||
s.clear()
|
||||
}
|
||||
|
||||
// Register web handlers
|
||||
func (s *statsCtx) initWeb() {
|
||||
if s.conf.HTTPRegister == nil {
|
||||
return
|
||||
}
|
||||
|
||||
s.conf.HTTPRegister("GET", "/control/stats", s.handleStats)
|
||||
s.conf.HTTPRegister("POST", "/control/stats_reset", s.handleStatsReset)
|
||||
s.conf.HTTPRegister("POST", "/control/stats_config", s.handleStatsConfig)
|
||||
s.conf.HTTPRegister("GET", "/control/stats_info", s.handleStatsInfo)
|
||||
}
|
||||
Reference in New Issue
Block a user