summaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
Diffstat (limited to 'config.go')
-rw-r--r--config.go42
1 files changed, 42 insertions, 0 deletions
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..3bb153b
--- /dev/null
+++ b/config.go
@@ -0,0 +1,42 @@
+package main
+
+import (
+ "encoding/json"
+ "net/netip"
+ "os"
+ "log"
+)
+
+type RuntimeConfig struct {
+ Devices DevicesConfig
+ Web WebConfig
+}
+
+type DevicesConfig struct {
+ Hs100 []Hs100Conf
+}
+
+type WebConfig struct {
+ Listen netip.AddrPort
+}
+
+// parseConfig() parses and validates the runtime configuration file and
+// returns it as Go datastructure.
+func parseConfig(path string) RuntimeConfig {
+ data, err := os.ReadFile(path)
+ if err != nil {
+ log.Fatalf("Could not read '%s'!", path)
+ }
+
+ if !json.Valid(data) {
+ log.Fatalf("%s contains invalid JSON!", path)
+ }
+
+ config := RuntimeConfig{}
+ err = json.Unmarshal(data, &config)
+ if err != nil {
+ log.Fatalf("Could not parse configuration file:\n%s\n", err)
+ }
+
+ return config
+}