spaceapid/handlers/state.go
Bendodroid 38710484f9
Generate HTTP endpoints for environment sensors
- Move update request sanity checks to new method in handlers/util.go
- Change EnvironmentSensor.Value type because ParseFloat returns float64
2024-01-14 01:10:38 +01:00

39 lines
974 B
Go

package handlers
import (
"io"
"log"
"net/http"
"strconv"
"time"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/config"
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
)
func StateOpen(
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
resp *types.SpaceAPIResponseV14,
) 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.ParseBool(string(body))
if err != nil {
log.Println("Failed to parse request body from", r.RemoteAddr)
w.WriteHeader(http.StatusBadRequest)
_, _ = io.WriteString(w, "HTTP request body should either be true or false")
return
}
// Set SpaceAPI response values
resp.State.Open = newState
resp.State.LastChange = time.Now().Unix()
// Respond with OK
w.WriteHeader(http.StatusOK)
_, _ = io.WriteString(w, "Update Successful")
}
}