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
This commit is contained in:
parent
6e1a8ac0e6
commit
38710484f9
5 changed files with 102 additions and 27 deletions
39
handlers/sensors.go
Normal file
39
handlers/sensors.go
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
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")
|
||||
}
|
||||
}
|
||||
|
|
@ -9,7 +9,6 @@ import (
|
|||
|
||||
"gitlab.hamburg.ccc.de/ccchh/spaceapid/config"
|
||||
"gitlab.hamburg.ccc.de/ccchh/spaceapid/types"
|
||||
"gitlab.hamburg.ccc.de/ccchh/spaceapid/util"
|
||||
)
|
||||
|
||||
func StateOpen(
|
||||
|
|
@ -17,31 +16,7 @@ func StateOpen(
|
|||
resp *types.SpaceAPIResponseV14,
|
||||
) func(http.ResponseWriter, *http.Request) {
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
// Check BasicAuth credentials
|
||||
username, password, ok := r.BasicAuth()
|
||||
if !ok || !util.CheckCredentials(authDB, validCredentials, username, password) {
|
||||
log.Println("Unauthorized request from", r.RemoteAddr)
|
||||
w.Header().Set("WWW-Authenticate", "Basic realm=\"space-api\"")
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Check if PUT method
|
||||
if r.Method != http.MethodPut {
|
||||
log.Println("Wrong METHOD from", r.RemoteAddr)
|
||||
w.Header().Set("Allow", http.MethodPut)
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Read request body
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
log.Println("Failed to read request body from", r.RemoteAddr)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
_, _ = io.WriteString(w, "Failed reading HTTP request body")
|
||||
return
|
||||
}
|
||||
body := updateEndpointValidator(authDB, validCredentials, w, r)
|
||||
|
||||
// Parse request body
|
||||
newState, err := strconv.ParseBool(string(body))
|
||||
|
|
|
|||
45
handlers/util.go
Normal file
45
handlers/util.go
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
package handlers
|
||||
|
||||
import (
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"gitlab.hamburg.ccc.de/ccchh/spaceapid/config"
|
||||
"gitlab.hamburg.ccc.de/ccchh/spaceapid/util"
|
||||
)
|
||||
|
||||
// updateEndpointValidator checks BasicAuth credentials,
|
||||
// checks for correct HTTP method and then returns the request body
|
||||
func updateEndpointValidator(
|
||||
authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID,
|
||||
w http.ResponseWriter, r *http.Request,
|
||||
) (body []byte) {
|
||||
// Check BasicAuth credentials
|
||||
username, password, ok := r.BasicAuth()
|
||||
if !ok || !util.CheckCredentials(authDB, validCredentials, username, password) {
|
||||
log.Println("Unauthorized request from", r.RemoteAddr)
|
||||
w.Header().Set("WWW-Authenticate", "Basic realm=\"space-api\"")
|
||||
w.WriteHeader(http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Check if PUT method
|
||||
if r.Method != http.MethodPut {
|
||||
log.Println("Wrong Method: ", r.Method, "from", r.RemoteAddr, "at", r.RequestURI)
|
||||
w.Header().Set("Allow", http.MethodPut)
|
||||
w.WriteHeader(http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Read request body
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
log.Println("Failed to read request body from", r.RemoteAddr)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
_, _ = io.WriteString(w, "Failed reading HTTP request body")
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue