spaceapid/handlers/sensors.go

40 lines
1,020 B
Go
Raw Normal View History

package handlers
import (
"io"
"log"
"math"
"net/http"
"strconv"
"time"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/config"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
)
func EnvironmentSensor(
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
resp *types.EnvironmentSensor,
) func(http.ResponseWriter, *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
body := updateEndpointValidator(authDB, validCredentials, w, r)
// 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)
w.WriteHeader(http.StatusBadRequest)
_, _ = io.WriteString(w, "HTTP request body has to be a valid float64 value != +/-Inf")
return
}
// Set SpaceAPI response values
resp.Value = newState
resp.LastChange = time.Now().Unix()
// Respond with OK
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, "Update Successful")
}
}