2023-11-05 20:23:31 +01:00
|
|
|
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"
|
2023-11-05 20:23:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func StateOpen(
|
2024-01-13 23:26:04 +01:00
|
|
|
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
|
2024-01-14 01:53:36 +01:00
|
|
|
resp *types.SpaceState,
|
2023-11-05 20:23:31 +01:00
|
|
|
) func(http.ResponseWriter, *http.Request) {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
2024-01-15 23:18:34 +01:00
|
|
|
body, err := updateEndpointValidator(authDB, validCredentials, w, r)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
2023-11-05 20:23:31 +01:00
|
|
|
|
|
|
|
// Parse request body
|
2024-01-15 23:18:34 +01:00
|
|
|
newState, err := strconv.ParseBool(string(body))
|
2023-11-05 20:23:31 +01:00
|
|
|
if err != nil {
|
2024-01-15 23:18:34 +01:00
|
|
|
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)
|
2023-11-05 20:23:31 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set SpaceAPI response values
|
2024-01-14 01:53:36 +01:00
|
|
|
resp.Open = newState
|
|
|
|
resp.LastChange = time.Now().Unix()
|
2023-11-05 20:23:31 +01:00
|
|
|
}
|
|
|
|
}
|
2024-08-03 19:48:33 +02:00
|
|
|
|
|
|
|
func StateMessagePUT(
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set SpaceAPI response values
|
|
|
|
resp.Message = string(body)
|
|
|
|
resp.LastChange = time.Now().Unix()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func StateMessageDELETE(
|
|
|
|
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
|
|
|
|
resp *types.SpaceState,
|
|
|
|
) 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.Message = ""
|
|
|
|
resp.LastChange = time.Now().Unix()
|
|
|
|
}
|
|
|
|
}
|