package utils 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 }