Skip to content

Commit

Permalink
chore: generalise handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
andig committed Nov 4, 2024
1 parent 2aa8b15 commit f3c791c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
4 changes: 2 additions & 2 deletions server/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)},
}

Expand Down Expand Up @@ -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},
Expand Down
39 changes: 22 additions & 17 deletions server/http_site_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,23 +126,28 @@ 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)
}

// 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
Expand All @@ -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) {
Expand Down

0 comments on commit f3c791c

Please sign in to comment.