Adds config file (quanlib.ini) support.
[quanweb.git] / main / config.go
1 package main
2
3 import (
4   "errors"
5   "fmt"
6   "github.com/alyu/configparser"
7   "os"
8   "strconv"
9 )
10
11 type qwConfig struct {
12   user, pass, dbName string 
13   basePath string
14   port int
15 }
16
17 func checkedSection(config *configparser.Configuration, name string) (*configparser.Section, error) {
18   section, err := config.Section(name)
19   if nil != err {
20     fmt.Println("ERROR!  Could not locate section [" + name + "] in config file:", err)
21     return nil, err
22   }
23   return section, nil
24 }
25
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.")
29   }
30
31   value := section.ValueOf(name)
32   return value, nil
33 }
34
35 // Note:  Will exit if config cannot be loaded
36 func GetConfig() (*qwConfig) {
37   config, err := loadConfig()
38   if nil != err {
39     fmt.Println("FATAL!  Cannot load config.  Unable to continue.", err)
40     os.Exit(1)
41   }
42
43   return config
44 }
45
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
49
50   const configFile = "quanlib.ini"
51
52   config, err := configparser.Read(configFile)
53   if nil != err {
54     fmt.Println("ERROR!  Failed to read config file:", configFile, err)
55     return nil, err
56   }
57
58   var section *configparser.Section
59
60   // ------------------
61   // Section:  database
62
63   var dbName, user, pass string
64
65   section, err = checkedSection(config, "database")
66   if nil != err {
67     return nil, err
68   }
69
70   dbName, err = checkedValue(section, "name")
71   if nil != err {
72     return nil, err
73   }
74
75   user, err = checkedValue(section, "user")
76   if nil != err {
77     return nil, err
78   }
79
80   pass, err = checkedValue(section, "pass")
81   if nil != err {
82     return nil, err
83   }
84
85   // --------------------
86   // Section:  filesystem
87
88   var basePath string
89
90   section, err = checkedSection(config, "filesystem")
91   if nil != err {
92     return nil, err
93   }
94
95   basePath, err = checkedValue(section, "basePath")
96   if nil != err {
97     return nil, err
98   }
99
100   // -------------
101   // Section:  web
102
103   var port int
104   var portStr string
105
106   section, err = checkedSection(config, "web")
107   if nil != err {
108     return nil, err
109   }
110
111   portStr, err = checkedValue(section, "port")
112   if nil != err {
113     return nil, err
114   }
115   port, err = strconv.Atoi(portStr)
116   if nil != err {
117     return nil, err
118   }
119
120   return &qwConfig{ user:user, pass:pass, dbName:dbName, 
121                     basePath:basePath,
122                     port:port }, 
123          nil 
124 }