From 8c1adae040aeef4f86e9718ceb7f45110cd44c00 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Wed, 31 Jan 2024 17:11:15 +0100 Subject: [PATCH 01/10] Rename module --- config/config.go | 2 +- config/types.go | 2 +- go.mod | 2 +- handlers/root.go | 2 +- handlers/sensors.go | 4 ++-- handlers/state.go | 4 ++-- handlers/util.go | 4 ++-- main.go | 8 ++++---- util/credentials.go | 2 +- util/util.go | 2 +- 10 files changed, 16 insertions(+), 16 deletions(-) diff --git a/config/config.go b/config/config.go index a5258a1..2190837 100644 --- a/config/config.go +++ b/config/config.go @@ -10,7 +10,7 @@ import ( "slices" "strings" - "gitlab.hamburg.ccc.de/ccchh/spaceapid/types" + "git.hamburg.ccc.de/ccchh/spaceapid/types" ) const ( diff --git a/config/types.go b/config/types.go index c2b3f12..4a305ce 100644 --- a/config/types.go +++ b/config/types.go @@ -1,7 +1,7 @@ package config import ( - "gitlab.hamburg.ccc.de/ccchh/spaceapid/types" + "git.hamburg.ccc.de/ccchh/spaceapid/types" ) type HTTPBACredentialID string diff --git a/go.mod b/go.mod index b0eee8a..e3d228e 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ -module gitlab.hamburg.ccc.de/ccchh/spaceapid +module git.hamburg.ccc.de/ccchh/spaceapid go 1.21 diff --git a/handlers/root.go b/handlers/root.go index f68f5fd..47091d5 100644 --- a/handlers/root.go +++ b/handlers/root.go @@ -5,7 +5,7 @@ import ( "log" "net/http" - "gitlab.hamburg.ccc.de/ccchh/spaceapid/types" + "git.hamburg.ccc.de/ccchh/spaceapid/types" ) func Root(resp *types.SpaceAPIResponseV14) func(http.ResponseWriter, *http.Request) { diff --git a/handlers/sensors.go b/handlers/sensors.go index bd881fb..187620e 100644 --- a/handlers/sensors.go +++ b/handlers/sensors.go @@ -7,8 +7,8 @@ import ( "strconv" "time" - "gitlab.hamburg.ccc.de/ccchh/spaceapid/config" - "gitlab.hamburg.ccc.de/ccchh/spaceapid/types" + "git.hamburg.ccc.de/ccchh/spaceapid/config" + "git.hamburg.ccc.de/ccchh/spaceapid/types" ) func EnvironmentSensor( diff --git a/handlers/state.go b/handlers/state.go index 0ec9727..f965db4 100644 --- a/handlers/state.go +++ b/handlers/state.go @@ -6,8 +6,8 @@ import ( "strconv" "time" - "gitlab.hamburg.ccc.de/ccchh/spaceapid/config" - "gitlab.hamburg.ccc.de/ccchh/spaceapid/types" + "git.hamburg.ccc.de/ccchh/spaceapid/config" + "git.hamburg.ccc.de/ccchh/spaceapid/types" ) func StateOpen( diff --git a/handlers/util.go b/handlers/util.go index f29e8cc..890d983 100644 --- a/handlers/util.go +++ b/handlers/util.go @@ -6,8 +6,8 @@ import ( "io" "net/http" - "gitlab.hamburg.ccc.de/ccchh/spaceapid/config" - "gitlab.hamburg.ccc.de/ccchh/spaceapid/util" + "git.hamburg.ccc.de/ccchh/spaceapid/config" + "git.hamburg.ccc.de/ccchh/spaceapid/util" ) // updateEndpointValidator checks BasicAuth credentials, diff --git a/main.go b/main.go index 1ec02a5..609fff1 100644 --- a/main.go +++ b/main.go @@ -10,10 +10,10 @@ import ( "strings" "syscall" - "gitlab.hamburg.ccc.de/ccchh/spaceapid/config" - "gitlab.hamburg.ccc.de/ccchh/spaceapid/handlers" - "gitlab.hamburg.ccc.de/ccchh/spaceapid/types" - "gitlab.hamburg.ccc.de/ccchh/spaceapid/util" + "git.hamburg.ccc.de/ccchh/spaceapid/config" + "git.hamburg.ccc.de/ccchh/spaceapid/handlers" + "git.hamburg.ccc.de/ccchh/spaceapid/types" + "git.hamburg.ccc.de/ccchh/spaceapid/util" ) var ( diff --git a/util/credentials.go b/util/credentials.go index 9352d5e..f515e61 100644 --- a/util/credentials.go +++ b/util/credentials.go @@ -1,7 +1,7 @@ package util import ( - "gitlab.hamburg.ccc.de/ccchh/spaceapid/config" + "git.hamburg.ccc.de/ccchh/spaceapid/config" ) // CheckCredentials validates whether a given username/password pair matches an diff --git a/util/util.go b/util/util.go index 086ee2f..5f109d8 100644 --- a/util/util.go +++ b/util/util.go @@ -7,7 +7,7 @@ import ( "os" "path" - "gitlab.hamburg.ccc.de/ccchh/spaceapid/types" + "git.hamburg.ccc.de/ccchh/spaceapid/types" ) const ( -- 2.44.1 From 905140c5ba3383773d20efe73ab713284c38124e Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Wed, 31 Jan 2024 17:53:45 +0100 Subject: [PATCH 02/10] Adjust template with migrated website and wiki --- config-template.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config-template.json b/config-template.json index c397fd9..be289aa 100644 --- a/config-template.json +++ b/config-template.json @@ -79,7 +79,7 @@ }, "feeds": { "blog": { - "type": "application/atom+xml", + "type": "application/rss+xml", "url": "https://hamburg.ccc.de/feed.xml" }, "calendar": { -- 2.44.1 From 2b6c9a4376e862d5285de61c536ca93cbe9e2fc4 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Wed, 31 Jan 2024 17:57:32 +0100 Subject: [PATCH 03/10] Prettify coordinates in template --- config-template.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config-template.json b/config-template.json index be289aa..48d4f84 100644 --- a/config-template.json +++ b/config-template.json @@ -66,8 +66,8 @@ "url": "https://hamburg.ccc.de/", "location": { "address": "Zeiseweg 9, 22765 Hamburg, Germany", - "lon": 9.9445899999999998, - "lat": 53.55836 + "lon": 9.94444, + "lat": 53.5584 }, "contact": { "phone": "+49 40 23830150", -- 2.44.1 From 4840021b086d4d6ccdfc983505453e18c5ab6e23 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Wed, 31 Jan 2024 18:16:48 +0100 Subject: [PATCH 04/10] Rename config shards in .gitignore --- .gitignore | 6 +++--- README.md | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index bb571c2..dcf0649 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,6 @@ spaceapid spaceapid-state.json # Config shards -credentials.json -dynamic.json -response.json +config-credentials.json +config-dynamic.json +config-response.json diff --git a/README.md b/README.md index f68787a..ad7467a 100644 --- a/README.md +++ b/README.md @@ -55,5 +55,5 @@ Set the environment variable to a comma-separated list of config files or pass t ```shell env CONFIG_PATH=config-template.json go run . # OR -go run . -c credentials.json,dynamic.json,response.json +go run . -c config-credentials.json,config-dynamic.json,config-response.json ``` -- 2.44.1 From c7c6086d11fb469748b479b82a3941960e5a8b83 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Wed, 31 Jan 2024 17:08:39 +0100 Subject: [PATCH 05/10] Rename constant for persistent state file and fix it at compile-time --- util/persistentStateDir.go | 7 +++++++ util/persistentStateDir_linux.go | 5 +++++ util/util.go | 28 ++++++++++++++-------------- 3 files changed, 26 insertions(+), 14 deletions(-) create mode 100644 util/persistentStateDir.go create mode 100644 util/persistentStateDir_linux.go diff --git a/util/persistentStateDir.go b/util/persistentStateDir.go new file mode 100644 index 0000000..96a9749 --- /dev/null +++ b/util/persistentStateDir.go @@ -0,0 +1,7 @@ +//go:build !linux + +package util + +const ( + persistentStateDir = "./" +) diff --git a/util/persistentStateDir_linux.go b/util/persistentStateDir_linux.go new file mode 100644 index 0000000..9f36a47 --- /dev/null +++ b/util/persistentStateDir_linux.go @@ -0,0 +1,5 @@ +package util + +const ( + persistentStateDir = "/var/lib/spaceapid/" +) diff --git a/util/util.go b/util/util.go index 5f109d8..5d1e8bd 100644 --- a/util/util.go +++ b/util/util.go @@ -5,13 +5,13 @@ import ( "errors" "log" "os" - "path" "git.hamburg.ccc.de/ccchh/spaceapid/types" ) const ( - savedStateJSONPath = "/var/lib/spaceapid/spaceapid-state.json" + persistentStateFile = "spaceapid-state.json" + persistentStatePath = persistentStateDir + persistentStateFile ) // MergeOldState merges a given SpaceAPIResponse with the state saved at the time of last program exit and then deletes @@ -23,12 +23,12 @@ func MergeOldState(response *types.SpaceAPIResponseV14) { persistedState types.PersistentStateV14 ) - log.Println("Merging old state from", savedStateJSONPath, "...") + log.Println("Merging old state from", persistentStatePath, "...") // Check if state.json is present - _, err = os.Stat(savedStateJSONPath) + _, err = os.Stat(persistentStatePath) if err != nil { - log.Println("Old state json not accessible at", savedStateJSONPath, ", skipping merge... error:", err) + log.Println("Old state json not accessible at", persistentStatePath, ", skipping merge... error:", err) if errors.Is(err, os.ErrNotExist) { return } @@ -36,14 +36,14 @@ func MergeOldState(response *types.SpaceAPIResponseV14) { } // Read file and load persisted state - oldState, err = os.ReadFile(savedStateJSONPath) + oldState, err = os.ReadFile(persistentStatePath) if err != nil { - log.Println("Error reading old state from", savedStateJSONPath, ", skipping merge... error:", err) + log.Println("Error reading old state from", persistentStatePath, ", skipping merge... error:", err) goto removeOld } err = json.Unmarshal(oldState, &persistedState) if err != nil { - log.Println(savedStateJSONPath, "doesn't seem to contain valid data... error:", err) + log.Println(persistentStatePath, "doesn't seem to contain valid data... error:", err) goto removeOld } @@ -66,23 +66,23 @@ func MergeOldState(response *types.SpaceAPIResponseV14) { // Delete old state json removeOld: - err = os.RemoveAll(savedStateJSONPath) + err = os.RemoveAll(persistentStatePath) if err != nil { - log.Println("Failed to remove", savedStateJSONPath, ", continuing... error:", err) + log.Println("Failed to remove", persistentStatePath, ", continuing... error:", err) } } func SaveCurrentState(response types.SpaceAPIResponseV14) { // Create state directory if not present - err := os.MkdirAll(path.Dir(savedStateJSONPath), 0750) + err := os.MkdirAll(persistentStateDir, 0750) if err != nil { - log.Fatalln("Failed creating", savedStateJSONPath, ", aborting... error:", err) + log.Fatalln("Failed creating", persistentStateDir, ", aborting... error:", err) } // Open persistent state file - file, err := os.OpenFile(savedStateJSONPath, os.O_WRONLY|os.O_CREATE, 0644) + file, err := os.OpenFile(persistentStatePath, os.O_WRONLY|os.O_CREATE, 0644) if err != nil { - log.Fatalln("Failed opening", savedStateJSONPath, "while trying to save current state... error:", err) + log.Fatalln("Failed opening", persistentStatePath, "while trying to save current state... error:", err) } defer file.Close() -- 2.44.1 From 742fb2be586f05f75168fab0dd281437f5795f0d Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Wed, 31 Jan 2024 18:21:01 +0100 Subject: [PATCH 06/10] util.go only contains persistent state handling so rename accordingly --- util/{util.go => persistentState.go} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename util/{util.go => persistentState.go} (100%) diff --git a/util/util.go b/util/persistentState.go similarity index 100% rename from util/util.go rename to util/persistentState.go -- 2.44.1 From 72f5a6798989924518203f860c9da13c01cd2d04 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Wed, 31 Jan 2024 18:25:34 +0100 Subject: [PATCH 07/10] Move persistence stuff to own package --- main.go | 6 +++--- {util => persistence}/persistentState.go | 2 +- {util => persistence}/persistentStateDir.go | 2 +- {util => persistence}/persistentStateDir_linux.go | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) rename {util => persistence}/persistentState.go (99%) rename {util => persistence}/persistentStateDir.go (74%) rename {util => persistence}/persistentStateDir_linux.go (73%) diff --git a/main.go b/main.go index 609fff1..3983cbd 100644 --- a/main.go +++ b/main.go @@ -12,8 +12,8 @@ import ( "git.hamburg.ccc.de/ccchh/spaceapid/config" "git.hamburg.ccc.de/ccchh/spaceapid/handlers" + "git.hamburg.ccc.de/ccchh/spaceapid/persistence" "git.hamburg.ccc.de/ccchh/spaceapid/types" - "git.hamburg.ccc.de/ccchh/spaceapid/util" ) var ( @@ -39,7 +39,7 @@ func main() { conf := config.ParseConfiguration() // Merge old state if present - util.MergeOldState(&conf.Response) + persistence.MergeOldState(&conf.Response) // Register signal handler sc := make(chan os.Signal, 1) @@ -47,7 +47,7 @@ func main() { go func(resp *types.SpaceAPIResponseV14) { <-sc log.Println("Saving state and shutting down...") - util.SaveCurrentState(*resp) + persistence.SaveCurrentState(*resp) os.Exit(0) }(&conf.Response) diff --git a/util/persistentState.go b/persistence/persistentState.go similarity index 99% rename from util/persistentState.go rename to persistence/persistentState.go index 5d1e8bd..01e1912 100644 --- a/util/persistentState.go +++ b/persistence/persistentState.go @@ -1,4 +1,4 @@ -package util +package persistence import ( "encoding/json" diff --git a/util/persistentStateDir.go b/persistence/persistentStateDir.go similarity index 74% rename from util/persistentStateDir.go rename to persistence/persistentStateDir.go index 96a9749..432dcff 100644 --- a/util/persistentStateDir.go +++ b/persistence/persistentStateDir.go @@ -1,6 +1,6 @@ //go:build !linux -package util +package persistence const ( persistentStateDir = "./" diff --git a/util/persistentStateDir_linux.go b/persistence/persistentStateDir_linux.go similarity index 73% rename from util/persistentStateDir_linux.go rename to persistence/persistentStateDir_linux.go index 9f36a47..65c0f8d 100644 --- a/util/persistentStateDir_linux.go +++ b/persistence/persistentStateDir_linux.go @@ -1,4 +1,4 @@ -package util +package persistence const ( persistentStateDir = "/var/lib/spaceapid/" -- 2.44.1 From 8015ed2be5039ea416645fe85211d986ff97ec22 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Tue, 13 Feb 2024 19:37:52 +0100 Subject: [PATCH 08/10] Location is optional for beverage_supply so make it omitempty --- config-template.json | 10 ++++++++++ types/v14.go | 4 ++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/config-template.json b/config-template.json index 48d4f84..31cb500 100644 --- a/config-template.json +++ b/config-template.json @@ -46,6 +46,16 @@ "club-assistant" ] } + ], + "beverage_supply": [ + { + "sensor_data": { + "unit": "btl" + }, + "allowed_credentials": [ + "club-assistant" + ] + } ] }, "state": { diff --git a/types/v14.go b/types/v14.go index 2bcdc06..2492639 100644 --- a/types/v14.go +++ b/types/v14.go @@ -45,7 +45,7 @@ type SpaceState struct { type EnvironmentSensor struct { Value float64 `json:"value"` Unit string `json:"unit"` - Location string `json:"location"` + Location string `json:"location,omitempty"` Name string `json:"name,omitempty"` Description string `json:"description,omitempty"` LastChange int64 `json:"lastchange,omitempty"` @@ -61,7 +61,7 @@ type PersistentStateV14 struct { type PersistentEnvironmentSensor struct { Value float64 `json:"value"` - Location string `json:"location"` + Location string `json:"location,omitempty"` Name string `json:"name,omitempty"` LastChange int64 `json:"lastchange"` } -- 2.44.1 From 7218177625e722e603fbd69b0a3eb0c1a5cefc25 Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Tue, 13 Feb 2024 19:42:37 +0100 Subject: [PATCH 09/10] Refactor environment sensor URL path generation --- main.go | 17 ++++++++--------- util/util.go | 20 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 util/util.go diff --git a/main.go b/main.go index 3983cbd..8e4b013 100644 --- a/main.go +++ b/main.go @@ -2,18 +2,17 @@ package main import ( "flag" - "fmt" "log" "net/http" "os" "os/signal" - "strings" "syscall" "git.hamburg.ccc.de/ccchh/spaceapid/config" "git.hamburg.ccc.de/ccchh/spaceapid/handlers" "git.hamburg.ccc.de/ccchh/spaceapid/persistence" "git.hamburg.ccc.de/ccchh/spaceapid/types" + "git.hamburg.ccc.de/ccchh/spaceapid/util" ) var ( @@ -59,15 +58,15 @@ func main() { handlers.StateOpen(conf.Credentials, conf.Dynamic.State.Open.AllowedCredentials, &conf.Response.State), ) // Register handlers for Environmental Sensors - for key, envSensorConfigs := range conf.Dynamic.Sensors { + for sensorType, envSensorConfigs := range conf.Dynamic.Sensors { for i, envSensorConfig := range envSensorConfigs { - pattern := fmt.Sprintf("/sensors/%s/%s", key, envSensorConfig.SensorData.Location) - if envSensorConfig.SensorData.Name != "" { - pattern += "/" + envSensorConfig.SensorData.Name - } - http.HandleFunc(strings.ToLower(pattern), + urlPath := util.GetSensorURLPath( + sensorType, envSensorConfig.SensorData.Location, envSensorConfig.SensorData.Name, + ) + http.HandleFunc( + urlPath, handlers.EnvironmentSensor( - conf.Credentials, envSensorConfig.AllowedCredentials, &conf.Response.Sensors[key][i], + conf.Credentials, envSensorConfig.AllowedCredentials, &conf.Response.Sensors[sensorType][i], ), ) } diff --git a/util/util.go b/util/util.go new file mode 100644 index 0000000..421f72c --- /dev/null +++ b/util/util.go @@ -0,0 +1,20 @@ +package util + +import ( + "fmt" + "strings" +) + +// GetSensorURLPath generates the URL path for the given sensor details. +// location and name may be optional depending on sensorType, see the schema definition for details. +// The path is always all-lowercase. +func GetSensorURLPath(sensorType, location, name string) string { + path := fmt.Sprintf("/sensors/%s", sensorType) + if location != "" { + path += "/" + location + } + if name != "" { + path += "/" + name + } + return strings.ToLower(path) +} -- 2.44.1 From b98174c7da4dd5734b60969464e26ad4e2d5be2f Mon Sep 17 00:00:00 2001 From: Bennett Wetters Date: Tue, 13 Feb 2024 19:48:18 +0100 Subject: [PATCH 10/10] Update README.md with note about URL paths --- README.md | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ad7467a..dd1839a 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ `spaceapid` serves a [SpaceAPI](https://spaceapi.io)-compatible JSON on port 8080: ```shell -$ curl -X GET http://[::1]:8080 | jq +$ curl http://[::1]:8080 | jq { "api_compatibility": [ "14" @@ -26,7 +26,7 @@ The config consists of three parts: - `"response"` - The static (pre-filled) parts of the response -See [Running](#running) for details. +See [Running](#Running) for details. ## Updating values @@ -37,12 +37,18 @@ curl -X PUT -u user:password -d true http://[::1]:8080/state/open ``` The same is true for the endpoints for sensors configured under `"dynamic"`. -Currently only `temperature` and `humidity` are implemented. +Currently only the sensors with the `value/unit/location/name/description` schema are implemented. +At the time of writing this includes `temperature`, `barometer`, `humidity`, `beverage_supply`, `power_consumption`, +and `account_balance`. +Out-of-spec sensors may also be used as long as they share the same schema. ```shell -curl -X PUT -u user:password -d 23.42 http://[::1]:8080/sensors/{temperature,humidity}/location[/name] +curl -X PUT -u user:password -d 23.42 http://[::1]:8080/sensors/{temperature,humidity,...}[/location[/name]] ``` +As can be seen in the example, the http urls are generated from sensor type and optionally `location` and `name`. +Depending on sensor type `location` might be required for your sensors, see the schema for details. + ## Building See the `go.mod` file for minimum required Go version. -- 2.44.1