diff --git a/server/http.go b/server/http.go index ade06cd94a..8ade922129 100644 --- a/server/http.go +++ b/server/http.go @@ -115,7 +115,7 @@ func (s *HTTPd) RegisterSiteHandlers(site site.API, valueChan chan<- util.Param) "sessions": {"GET", "/sessions", sessionHandler}, "updatesession": {"PUT", "/session/{id:[0-9]+}", updateSessionHandler}, "deletesession": {"DELETE", "/session/{id:[0-9]+}", deleteSessionHandler}, - "telemetry": {"GET", "/settings/telemetry", boolGetHandler(telemetry.Enabled)}, + "telemetry": {"GET", "/settings/telemetry", getHandler(telemetry.Enabled)}, "telemetry2": {"POST", "/settings/telemetry/{value:[01truefalse]+}", boolHandler(telemetry.Enable, telemetry.Enabled)}, } @@ -238,7 +238,7 @@ func (s *HTTPd) RegisterSystemHandler(valueChan chan<- util.Param, cache *util.C "devices": {"GET", "/devices/{class:[a-z]+}", devicesHandler}, "device": {"GET", "/devices/{class:[a-z]+}/{id:[0-9.]+}", deviceConfigHandler}, "devicestatus": {"GET", "/devices/{class:[a-z]+}/{name:[a-zA-Z0-9_.:-]+}/status", deviceStatusHandler}, - "dirty": {"GET", "/dirty", boolGetHandler(ConfigDirty)}, + "dirty": {"GET", "/dirty", getHandler(ConfigDirty)}, "newdevice": {"POST", "/devices/{class:[a-z]+}", newDeviceHandler}, "updatedevice": {"PUT", "/devices/{class:[a-z]+}/{id:[0-9.]+}", updateDeviceHandler}, "deletedevice": {"DELETE", "/devices/{class:[a-z]+}/{id:[0-9.]+}", deleteDeviceHandler}, diff --git a/server/http_site_handler.go b/server/http_site_handler.go index c04ce38fc5..84a326bcb9 100644 --- a/server/http_site_handler.go +++ b/server/http_site_handler.go @@ -126,6 +126,20 @@ func handler[T any](conv func(string) (T, error), set func(T) error, get func() } } +// ptrHandler updates pointer api +func ptrHandler[T any](conv func(string) (T, error), set func(*T) error, get func() *T) http.HandlerFunc { + return handler(func(s string) (*T, error) { + var val *T + v, err := conv(s) + if err == nil { + val = &v + } else if s == "" { + err = nil + } + return val, err + }, set, get) +} + // floatHandler updates float-param api func floatHandler(set func(float64) error, get func() float64) http.HandlerFunc { return handler(parseFloat, set, get) @@ -133,16 +147,7 @@ func floatHandler(set func(float64) error, get func() float64) http.HandlerFunc // floatPtrHandler updates float-pointer api func floatPtrHandler(set func(*float64) error, get func() *float64) http.HandlerFunc { - return handler(func(s string) (*float64, error) { - var val *float64 - f, err := parseFloat(s) - if err == nil { - val = &f - } else if s == "" { - err = nil - } - return val, err - }, set, get) + return ptrHandler(parseFloat, set, get) } // intHandler updates int-param api @@ -155,18 +160,18 @@ func boolHandler(set func(bool) error, get func() bool) http.HandlerFunc { return handler(strconv.ParseBool, set, get) } -// boolGetHandler retrieves bool api values -func boolGetHandler(get func() bool) http.HandlerFunc { - return func(w http.ResponseWriter, r *http.Request) { - jsonResult(w, get()) - } -} - // durationHandler updates duration-param api func durationHandler(set func(time.Duration) error, get func() time.Duration) http.HandlerFunc { return handler(util.ParseDuration, set, get) } +// getHandler returns api results +func getHandler[T any](get func() T) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + jsonResult(w, get()) + } +} + // updateSmartCostLimit sets the smart cost limit globally func updateSmartCostLimit(site site.API) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) {