diff options
| author | xengineering <me@xengineering.eu> | 2025-12-20 14:06:34 +0100 |
|---|---|---|
| committer | xengineering <me@xengineering.eu> | 2025-12-20 14:06:34 +0100 |
| commit | d6fa255b1df26a12329a614601a027e074aabeb2 (patch) | |
| tree | b16cb727be8f14821e2ddbfa2d3179af2c8db457 | |
| parent | ba367f41077be88338db981c88802d7292ac7013 (diff) | |
| download | sia-server-d6fa255b1df26a12329a614601a027e074aabeb2.tar sia-server-d6fa255b1df26a12329a614601a027e074aabeb2.tar.zst sia-server-d6fa255b1df26a12329a614601a027e074aabeb2.zip | |
Add default config parsing
This is a first step towards configurability.
| -rw-r--r-- | config.go | 38 | ||||
| -rw-r--r-- | config_test.go | 14 | ||||
| -rw-r--r-- | configs/default.json | 11 | ||||
| -rw-r--r-- | meson.build | 1 |
4 files changed, 64 insertions, 0 deletions
diff --git a/config.go b/config.go new file mode 100644 index 0000000..38f0af9 --- /dev/null +++ b/config.go @@ -0,0 +1,38 @@ +package main + +import ( + _ "embed" + "encoding/json" + "log" +) + +//go:embed configs/default.json +var defaultConfig []byte + +type StartupConfig struct { + MQTT struct { + Broker string `json:"broker"` + ClientID string `json:"client-id"` + TopicPrefix string `json:"topic-prefix"` + } `json:"mqtt"` + + Homematic struct { + CCU string `json:"ccu"` + PollingPeriodMilliseconds int `json:"polling-period-milliseconds"` + } `json:"homematic"` +} + +func (sc *StartupConfig) FromJSON(data []byte) error { + return json.Unmarshal(data, sc) +} + +func GetStartupConfig() StartupConfig { + config := StartupConfig{} + + err := config.FromJSON(defaultConfig) + if err != nil { + log.Fatalf("Could not parse default config: %v", err) + } + + return config +} diff --git a/config_test.go b/config_test.go new file mode 100644 index 0000000..c568a34 --- /dev/null +++ b/config_test.go @@ -0,0 +1,14 @@ +package main + +import ( + "testing" +) + +func TestDefaultConfig(t *testing.T) { + config := StartupConfig{} + + err := config.FromJSON(defaultConfig) + if err != nil { + t.Fatalf("Failed parsing default config from JSON: %v", err) + } +} diff --git a/configs/default.json b/configs/default.json new file mode 100644 index 0000000..9d3fb07 --- /dev/null +++ b/configs/default.json @@ -0,0 +1,11 @@ +{ + "mqtt": { + "broker": "tcp://127.0.0.1:1883", + "client-id": "siaserver", + "topic-prefix": "sia" + }, + "homematic": { + "ccu": "http://127.0.0.1:8080", + "polling-period-milliseconds": 50 + } +} diff --git a/meson.build b/meson.build index 9861cdf..0917410 100644 --- a/meson.build +++ b/meson.build @@ -8,6 +8,7 @@ sia_server = custom_target( meson.current_source_dir() / 'cache.go', meson.current_source_dir() / 'mqtt.go', meson.current_source_dir() / 'homematic.go', + meson.current_source_dir() / 'config.go', ], output : 'sia-server', command : [ |
