From b054b02be6c92deadb384be66a41d66dd6b08843 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Sat, 25 Jul 2026 22:09:15 +0200 Subject: [PATCH] fix(handlers): Handle DELETE for environment sensors It now works as advertised in the README! *ahem*... --- handlers/sensors.go | 19 ++++++++++++++++++- main.go | 12 +++++++++--- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/handlers/sensors.go b/handlers/sensors.go index 187620e..e3aa51e 100644 --- a/handlers/sensors.go +++ b/handlers/sensors.go @@ -11,7 +11,7 @@ import ( "git.hamburg.ccc.de/ccchh/spaceapid/types" ) -func EnvironmentSensor( +func EnvironmentSensorPUT( authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID, resp *types.EnvironmentSensor, ) func(http.ResponseWriter, *http.Request) { @@ -35,3 +35,20 @@ func EnvironmentSensor( resp.LastChange = time.Now().Unix() } } + +func EnvironmentSensorDELETE( + authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID, + resp *types.EnvironmentSensor, +) 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.Value = 0 + resp.LastChange = time.Now().Unix() + } +} diff --git a/main.go b/main.go index 35b564d..3e867b8 100644 --- a/main.go +++ b/main.go @@ -68,12 +68,18 @@ func main() { // Register handlers for environmental sensors for sensorType, envSensorConfigs := range conf.Dynamic.Sensors { for i, envSensorConfig := range envSensorConfigs { - urlPattern := "PUT " + util.GetSensorURLPath( + urlPattern := util.GetSensorURLPath( sensorType, envSensorConfig.SensorData.Location, envSensorConfig.SensorData.Name, ) http.HandleFunc( - urlPattern, - handlers.EnvironmentSensor( + "PUT "+urlPattern, + handlers.EnvironmentSensorPUT( + conf.Credentials, envSensorConfig.AllowedCredentials, &conf.Response.Sensors[sensorType][i], + ), + ) + http.HandleFunc( + "DELETE "+urlPattern, + handlers.EnvironmentSensorDELETE( conf.Credentials, envSensorConfig.AllowedCredentials, &conf.Response.Sensors[sensorType][i], ), )