summaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2023-02-09 20:30:49 +0100
committerxengineering <me@xengineering.eu>2023-02-09 20:30:49 +0100
commit5f5e7e9bce0e463d761a26994766853979ca7ca2 (patch)
treee2cffb18db4366766fc0b175f227f87a0006b9eb /config.go
parenta23d11e7a73c5f9cd223621be2ac63ed53b75e48 (diff)
downloadceres-5f5e7e9bce0e463d761a26994766853979ca7ca2.tar
ceres-5f5e7e9bce0e463d761a26994766853979ca7ca2.tar.zst
ceres-5f5e7e9bce0e463d761a26994766853979ca7ca2.zip
Rename runtime_config.go to config.go
It is shorter.
Diffstat (limited to 'config.go')
-rw-r--r--config.go65
1 files changed, 65 insertions, 0 deletions
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..9302e11
--- /dev/null
+++ b/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
+}