spaceapid/handlers/state.go

37 lines
889 B
Go
Raw Permalink Normal View History

package handlers
import (
"log"
"net/http"
"strconv"
"time"
2024-01-31 17:11:15 +01:00
"git.hamburg.ccc.de/ccchh/spaceapid/config"
"git.hamburg.ccc.de/ccchh/spaceapid/types"
)
func StateOpen(
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
resp *types.SpaceState,
) 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.ParseBool(string(body))
if err != nil {
log.Println("Failed to parse request body from", r.RemoteAddr, "with error:", err)
http.Error(w, "HTTP request body should either be true or false", http.StatusBadRequest)
return
}
// Set SpaceAPI response values
resp.Open = newState
resp.LastChange = time.Now().Unix()
}
}