6 "github.com/alyu/configparser"
11 type qwConfig struct {
12 user, pass, dbName string
17 func checkedSection(config *configparser.Configuration, name string) (*configparser.Section, error) {
18 section, err := config.Section(name)
20 fmt.Println("ERROR! Could not locate section [" + name + "] in config file:", err)
26 func checkedValue(section *configparser.Section, name string) (string, error) {
27 if ! section.Exists(name) {
28 return "", errors.New("ERROR! Value named \"" + name + "\" not found in config file.")
31 value := section.ValueOf(name)
35 // Note: Will exit if config cannot be loaded
36 func GetConfig() (*qwConfig) {
37 config, err := loadConfig()
39 fmt.Println("FATAL! Cannot load config. Unable to continue.", err)
46 func loadConfig() (*qwConfig, error) {
47 // TODO: Make the path to the config file configurable (environment variable?)
48 // TODO: Load the config file once, and cache the values for subsequent getConfig() calls
50 const configFile = "quanlib.ini"
52 config, err := configparser.Read(configFile)
54 fmt.Println("ERROR! Failed to read config file:", configFile, err)
58 var section *configparser.Section
63 var dbName, user, pass string
65 section, err = checkedSection(config, "database")
70 dbName, err = checkedValue(section, "name")
75 user, err = checkedValue(section, "user")
80 pass, err = checkedValue(section, "pass")
85 // --------------------
86 // Section: filesystem
90 section, err = checkedSection(config, "filesystem")
95 basePath, err = checkedValue(section, "basePath")
106 section, err = checkedSection(config, "web")
111 portStr, err = checkedValue(section, "port")
115 port, err = strconv.Atoi(portStr)
120 return &qwConfig{ user:user, pass:pass, dbName:dbName,