package utils import ( "fmt" "log" "flag" "os" "io/ioutil" "encoding/json" ) type RuntimeConfig struct { Path string Debug bool 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"` Debug bool } 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.BoolVar(&config.Debug, "d", false, "Use this flag if you are in a development environment") 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) } // parse content to config structs err = json.Unmarshal(configData, &config) if err != nil { log.Fatalf("Could not parse configuration file %s", config.Path) } // override defaults if in debugging mode if config.Debug { config.Http.Static = "./data/static" config.Http.Templates = "./data/templates" config.Http.Storage = "./data/storage" } // copy debug value config.Database.Debug = config.Debug // print config if in debug mode if config.Debug { configuration,err := json.MarshalIndent(config, "", " ") if err != nil { log.Fatal(err) } fmt.Print("Used config: " + string(configuration) + "\n") } return config }