From 70b2e8069b9ba724a0dc53d646656a2ad5bb5fd7 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Sun, 14 Jan 2024 01:53:36 +0100 Subject: [PATCH] Only pass State struct pointer to StateOpen, not entire response --- handlers/state.go | 6 +++--- main.go | 2 +- types/v14.go | 10 ++++++---- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/handlers/state.go b/handlers/state.go index 13f37c0..47bc6ab 100644 --- a/handlers/state.go +++ b/handlers/state.go @@ -13,7 +13,7 @@ import ( func StateOpen( authDB config.HTTPBACredentials, validCredentials []config.HTTPBACredentialID, - resp *types.SpaceAPIResponseV14, + resp *types.SpaceState, ) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { body := updateEndpointValidator(authDB, validCredentials, w, r) @@ -28,8 +28,8 @@ func StateOpen( } // Set SpaceAPI response values - resp.State.Open = newState - resp.State.LastChange = time.Now().Unix() + resp.Open = newState + resp.LastChange = time.Now().Unix() // Respond with OK w.WriteHeader(http.StatusOK) diff --git a/main.go b/main.go index c9060fb..044ceed 100644 --- a/main.go +++ b/main.go @@ -37,7 +37,7 @@ func main() { handlers.Root(&conf.Response), ) http.HandleFunc("/state/open", - handlers.StateOpen(conf.Credentials, conf.Dynamic.State.Open.AllowedCredentials, &conf.Response), + handlers.StateOpen(conf.Credentials, conf.Dynamic.State.Open.AllowedCredentials, &conf.Response.State), ) // Register handlers for Environmental Sensors for key, envSensorConfigs := range conf.Dynamic.Sensors { diff --git a/types/v14.go b/types/v14.go index 63e6765..8540fe7 100644 --- a/types/v14.go +++ b/types/v14.go @@ -11,10 +11,7 @@ type SpaceAPIResponseV14 struct { Lat float64 `json:"lat"` Lon float64 `json:"lon"` } `json:"location"` - State struct { - Open bool `json:"open"` - LastChange int64 `json:"lastchange"` - } `json:"state"` + State SpaceState `json:"state"` Contact struct { Phone string `json:"phone"` IRC string `json:"irc"` @@ -40,6 +37,11 @@ type SpaceAPIResponseV14 struct { } `json:"links"` } +type SpaceState struct { + Open bool `json:"open"` + LastChange int64 `json:"lastchange"` +} + type EnvironmentSensor struct { Value float64 `json:"value"` Unit string `json:"unit"`