From f9a5140071703faf0c7515a05f52e69fdc1f11ba Mon Sep 17 00:00:00 2001
From: xengineering <me@xengineering.eu>
Date: Wed, 8 Feb 2023 20:53:20 +0100
Subject: Move all sources to package main

This project is not so big that it needs multiple packages.
---
 runtime_config.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
 create mode 100644 runtime_config.go

(limited to 'runtime_config.go')

diff --git a/runtime_config.go b/runtime_config.go
new file mode 100644
index 0000000..9302e11
--- /dev/null
+++ b/runtime_config.go
@@ -0,0 +1,65 @@
+
+package main
+
+import (
+	"fmt"
+	"log"
+	"flag"
+	"os"
+	"io/ioutil"
+	"encoding/json"
+)
+
+type RuntimeConfig struct {
+	Path string
+	Http HttpConfig `json:"http"`
+	Database DatabaseConfig `json:"database"`
+}
+
+type HttpConfig struct {
+	Host string `json:"bind_host"`
+	Port string `json:"bind_port"`
+	Static string `json:"static"`
+	Templates string `json:"templates"`
+	Storage string `json:"storage"`
+}
+
+type DatabaseConfig struct {
+	Socket string `json:"socket"`
+	User string `json:"user"`
+	Database string `json:"database"`
+	Migrations string `json:"migrations"`
+}
+
+func GetRuntimeConfig() RuntimeConfig {
+
+	// init empty return value
+	config := RuntimeConfig{}
+
+	// read command line flags
+	flag.StringVar(&config.Path, "c", "/etc/ceres/config.json", "Path to ceres configuration file")
+	flag.Parse()
+
+	// open config file
+	configFile, err := os.Open(config.Path)
+	defer configFile.Close()
+	if err != nil {
+		log.Fatalf("Could not open configuration file %s", config.Path)
+	}
+
+	// read byte content
+	configData, err := ioutil.ReadAll(configFile)
+	if err != nil {
+		log.Fatalf("Could not read configuration file %s", config.Path)
+	}
+
+	fmt.Print("Used config: " + string(configData) + "\n")
+
+	// parse content to config structs
+	err = json.Unmarshal(configData, &config)
+	if err != nil {
+		log.Fatalf("Could not parse configuration file %s", config.Path)
+	}
+
+	return config
+}
-- 
cgit v1.2.3-70-g09d2