summaryrefslogtreecommitdiff
path: root/config.go
blob: f63d28489e16e50889019da5dc2ccd81a3bc3bd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package main

import (
	"encoding/json"
	"net/netip"
	"os"
	"log"
)

var config RuntimeConfig

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) {
	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)
	}

	err = json.Unmarshal(data, &config)
	if err != nil {
		log.Fatalf("Could not parse configuration file:\n%s\n", err)
	}
}