/
gsconfy.go
106 lines (91 loc) · 3.07 KB
/
gsconfy.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package main
import (
mysql "github.com/Philio/GoMySQL"
conf "goconf.googlecode.com/hg"
"os"
l4g "log4go.googlecode.com/hg"
)
type SolrData struct {
apistring string
core string
server string
authstring string
}
//Move these into a different package?
func loadConfig(file string) map[string]map[string]string {
config, err := conf.ReadConfigFile(file)
if err != nil {
l4g.Error("error reading config: %s\n", err.String())
os.Exit(1)
}
//Setup configuration values
values := make(map[string]map[string]string)
//Setup some default values - these are in the default namespace
//There are 3 configuration namespaces default, database, and stomp
//If the default value is "" that means it is required in the config file.
//An empty value in the config will not overwrite the default value
values["default"] = map[string]string{"host": "localhost", "port": "80", "read_timeout": "0", "write_timeout": "0"}
values["database"] = map[string]string{"host": "localhost", "port": "3306", "user": "", "pass": "", "name": ""}
values["stomp"] = map[string]string{"host": ""}
l4g.Debug("Default vals: %v", values)
//Read values from config
for namespace := range values {
//If there is a default value it's ok if the config key doesn't exist
for key, value := range values[namespace] {
if value != "" {
set := getValue(config, key, namespace, false)
if set != "" {
values[namespace][key] = set
}
} else {
values[namespace][key] = getValue(config, key, namespace, true)
}
}
}
l4g.Debug("Final values: %v", values)
return values
}
func getValue(config *conf.ConfigFile, key string, namespace string, fail bool) string {
str, err := config.GetString(namespace, key)
if err != nil && fail {
//Exit if we can't find an expected value
l4g.Error("Error getting %s: %s\n", key, err.String())
os.Exit(1)
}
return str
}
//Pull information from MySQL to find out which API keys we should handle and how they map
// i.e. servers and cores
func loadSolrServers(config map[string]map[string]string) map[string]map[string]string {
db, err := mysql.DialTCP(config["database"]["host"], config["database"]["user"], config["database"]["pass"], config["database"]["name"])
if err != nil {
l4g.Error("Error connecting to db: %s\n", err.String())
os.Exit(1)
}
stmt, err := db.Prepare("Select apistring,core,server,authstring from cores where gosolr = ?")
if err != nil {
l4g.Error("Error preparing statement: %s", err.String())
os.Exit(1)
}
stmt.BindParams(config["default"]["host"])
err = stmt.Execute()
if err != nil {
l4g.Error("error executing stmt: %s", err.String())
os.Exit(1)
}
var solrrow SolrData
stmt.BindResult(&solrrow.apistring, &solrrow.core, &solrrow.server, &solrrow.authstring)
solr_values := make(map[string]map[string]string)
for {
eof, err := stmt.Fetch()
if err != nil {
l4g.Error("Error fetching row: %s", err.String())
os.Exit(1)
}
solr_values[solrrow.apistring] = map[string]string{"core": solrrow.core, "server": solrrow.server, "authstring": solrrow.authstring}
if eof {
break
}
}
return solr_values
}