spaceapid/handlers/sensors.go
Bennett Wetters 34d05ffa49 feat(handlers): Handle DELETE for environment sensors
It now works as advertised in the README! *ahem*...
2026-07-25 22:51:26 +02:00

54 lines
1.4 KiB
Go

package handlers
import (
"log"
"math"
"net/http"
"strconv"
"time"
"git.hamburg.ccc.de/ccchh/spaceapid/config"
"git.hamburg.ccc.de/ccchh/spaceapid/types"
)
func EnvironmentSensorPUT(
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
resp *types.EnvironmentSensor,
) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
body, err := updateEndpointValidator(authDB, validCredentials, w, r)
if err != nil {
log.Println(err)
return
}
// Parse request body
newState, err := strconv.ParseFloat(string(body), 64)
if err != nil || math.IsInf(newState, 0) {
log.Println("Failed to parse request body from", r.RemoteAddr, "with error:", err)
http.Error(w, "HTTP request body has to be a valid float64 value != +/-Inf", http.StatusBadRequest)
return
}
// Set SpaceAPI response values
resp.Value = newState
resp.LastChange = time.Now().Unix()
}
}
func EnvironmentSensorDELETE(
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
resp *types.EnvironmentSensor,
) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
_, err := updateEndpointValidator(authDB, validCredentials, w, r)
if err != nil {
log.Println(err)
return
}
// Set SpaceAPI response values
resp.Value = 0
resp.LastChange = time.Now().Unix()
}
}