From d4e1f48ff12db48e9a9bdbd3253c1041328ca66d Mon Sep 17 00:00:00 2001 From: xengineering Date: Fri, 11 Jun 2021 10:36:20 +0200 Subject: Implement Configuration with JSON --- Makefile | 1 + config/default.json | 6 ++++++ src/main.go | 41 ++++++++++++++++++++++++++++++++++++++++- src/web.go | 11 ++++++++--- 4 files changed, 55 insertions(+), 4 deletions(-) create mode 100644 config/default.json diff --git a/Makefile b/Makefile index 88e4c4f..7531fdb 100644 --- a/Makefile +++ b/Makefile @@ -16,6 +16,7 @@ clean: install: all make -C python install DESTDIR=$(abspath $(DESTDIR)) make -C src install DESTDIR=$(abspath $(DESTDIR)) + install -Dm 644 config/default.json $(DESTDIR)/etc/birdscan/config.json install -Dm 644 systemd/birdscan.service $(DESTDIR)$(PREFIX)/lib/systemd/system/birdscan.service install -Dm 644 systemd/birdscan.sysusers $(DESTDIR)$(PREFIX)/lib/sysusers.d/birdscan.conf install -Dm 644 data/html/index.html $(DESTDIR)$(PREFIX)/share/birdscan/html/index.html diff --git a/config/default.json b/config/default.json new file mode 100644 index 0000000..6288daf --- /dev/null +++ b/config/default.json @@ -0,0 +1,6 @@ +{ + "webserver":{ + "bind_address":"127.0.0.1", + "bind_port":"8080" + } +} 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 +} + diff --git a/src/web.go b/src/web.go index b57c5f5..4306b2c 100644 --- a/src/web.go +++ b/src/web.go @@ -15,15 +15,20 @@ const ( APP_DATA = "/usr/share/birdscan" ) -func runServer() { +type webConfig struct { + BindAddress string `json:"bind_address"` + BindPort string `json:"bind_port"` +} + +func runServer(cfg *webConfig) { router := chi.NewRouter() router.Use(middleware.Logger) router.Get("/", indexHandler) router.Get("/css/birdscan.css", cssHandler) - log.Println("Binding to 'http://" + "127.0.0.1" + ":" + "8080" + "'") - log.Fatal(http.ListenAndServe("127.0.0.1" + ":" + "8080", router)) + log.Println("Binding to 'http://" + cfg.BindAddress + ":" + cfg.BindPort + "'") + log.Fatal(http.ListenAndServe(cfg.BindAddress + ":" + cfg.BindPort, router)) } func indexHandler(w http.ResponseWriter, r *http.Request) { -- cgit v1.2.3-70-g09d2