diff options
author | xengineering <mail2xengineering@protonmail.com> | 2021-06-11 10:36:20 +0200 |
---|---|---|
committer | xengineering <mail2xengineering@protonmail.com> | 2021-06-11 10:45:35 +0200 |
commit | d4e1f48ff12db48e9a9bdbd3253c1041328ca66d (patch) | |
tree | e76a950b5a249b410743f3f26849831452083d11 /src/main.go | |
parent | d6fa6b112451f10f505db886d0ed023b3b931814 (diff) | |
download | birdscan-d4e1f48ff12db48e9a9bdbd3253c1041328ca66d.tar birdscan-d4e1f48ff12db48e9a9bdbd3253c1041328ca66d.tar.zst birdscan-d4e1f48ff12db48e9a9bdbd3253c1041328ca66d.zip |
Implement Configuration with JSON
Diffstat (limited to 'src/main.go')
-rw-r--r-- | src/main.go | 41 |
1 files changed, 40 insertions, 1 deletions
diff --git a/src/main.go b/src/main.go index 68f2af3..d64533e 100644 --- a/src/main.go +++ b/src/main.go @@ -5,14 +5,53 @@ package main import ( "log" "time" + "os" + "io/ioutil" + "encoding/json" ) +const ( + CONFIG_FILE = "/etc/birdscan/config.json" +) + +type config struct { + WebConfig webConfig `json:"webserver"` +} + func main() { log.SetFlags(0) // disable timestamp because systemd takes care of that log.Println("Starting birdscan") - go runServer() + cfg := readConfig() + go runServer(&cfg.WebConfig) for { time.Sleep(1 * time.Second) } } +func readConfig() config { + + log.Printf("Reading config file %s", CONFIG_FILE) + var retval config + + // open the config file + configFile, err := os.Open(CONFIG_FILE) + defer configFile.Close() + if err != nil { + log.Fatalf("Could not open configuration file %s", CONFIG_FILE) + } + + // read byte content + byteData, err := ioutil.ReadAll(configFile) + if err != nil { + log.Fatalf("Could not read configuration file %s", CONFIG_FILE) + } + + // parse content to config structs + err = json.Unmarshal(byteData, &retval) + if err != nil { + log.Fatalf("Could not parse configuration file %s", CONFIG_FILE) + } + + return retval +} + |