Improve logging #17
|
@ -26,10 +26,8 @@ func Root(resp *types.SpaceAPIResponseV14) func(http.ResponseWriter, *http.Reque
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Respond with OK
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
_, _ = w.Write(response)
|
_, _ = w.Write(response)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,12 +17,12 @@ func EnvironmentSensor(
|
||||||
resp *types.EnvironmentSensor,
|
resp *types.EnvironmentSensor,
|
||||||
) func(http.ResponseWriter, *http.Request) {
|
) func(http.ResponseWriter, *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
body := updateEndpointValidator(authDB, validCredentials, w, r)
|
body := string(updateEndpointValidator(authDB, validCredentials, w, r))
|
||||||
|
|
||||||
// Parse request body
|
// Parse request body
|
||||||
newState, err := strconv.ParseFloat(string(body), 64)
|
newState, err := strconv.ParseFloat(body, 64)
|
||||||
if err != nil || math.IsInf(newState, 0) {
|
if err != nil || math.IsInf(newState, 0) {
|
||||||
log.Println("Failed to parse request body from", r.RemoteAddr)
|
log.Println("Failed to parse request body from", r.RemoteAddr, "body:", body)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
_, _ = io.WriteString(w, "HTTP request body has to be a valid float64 value != +/-Inf")
|
_, _ = io.WriteString(w, "HTTP request body has to be a valid float64 value != +/-Inf")
|
||||||
return
|
return
|
||||||
|
@ -32,8 +32,6 @@ func EnvironmentSensor(
|
||||||
resp.Value = newState
|
resp.Value = newState
|
||||||
resp.LastChange = time.Now().Unix()
|
resp.LastChange = time.Now().Unix()
|
||||||
|
|
||||||
// Respond with OK
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
_, _ = io.WriteString(w, "Update Successful")
|
_, _ = io.WriteString(w, "Update Successful")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -16,12 +16,12 @@ func StateOpen(
|
||||||
resp *types.SpaceState,
|
resp *types.SpaceState,
|
||||||
) func(http.ResponseWriter, *http.Request) {
|
) func(http.ResponseWriter, *http.Request) {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
body := updateEndpointValidator(authDB, validCredentials, w, r)
|
body := string(updateEndpointValidator(authDB, validCredentials, w, r))
|
||||||
|
|
||||||
// Parse request body
|
// Parse request body
|
||||||
newState, err := strconv.ParseBool(string(body))
|
newState, err := strconv.ParseBool(body)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Failed to parse request body from", r.RemoteAddr)
|
log.Println("Failed to parse request body from", r.RemoteAddr, "body:", body)
|
||||||
w.WriteHeader(http.StatusBadRequest)
|
w.WriteHeader(http.StatusBadRequest)
|
||||||
_, _ = io.WriteString(w, "HTTP request body should either be true or false")
|
_, _ = io.WriteString(w, "HTTP request body should either be true or false")
|
||||||
return
|
return
|
||||||
|
@ -31,8 +31,6 @@ func StateOpen(
|
||||||
resp.Open = newState
|
resp.Open = newState
|
||||||
resp.LastChange = time.Now().Unix()
|
resp.LastChange = time.Now().Unix()
|
||||||
|
|
||||||
// Respond with OK
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
_, _ = io.WriteString(w, "Update Successful")
|
_, _ = io.WriteString(w, "Update Successful")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,7 +18,7 @@ func updateEndpointValidator(
|
||||||
// Check BasicAuth credentials
|
// Check BasicAuth credentials
|
||||||
username, password, ok := r.BasicAuth()
|
username, password, ok := r.BasicAuth()
|
||||||
if !ok || !util.CheckCredentials(authDB, validCredentials, username, password) {
|
if !ok || !util.CheckCredentials(authDB, validCredentials, username, password) {
|
||||||
log.Println("Unauthorized request from", r.RemoteAddr)
|
log.Println("Unauthorized request from", r.RemoteAddr, "Username:", username, "Password:", password)
|
||||||
w.Header().Set("WWW-Authenticate", "Basic realm=\"space-api\"")
|
w.Header().Set("WWW-Authenticate", "Basic realm=\"space-api\"")
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
|
@ -41,5 +41,5 @@ func updateEndpointValidator(
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
return
|
return body
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue