forked from kellydunn/golang-geo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sql_conf.go
121 lines (100 loc) · 3.41 KB
/
sql_conf.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package geo
import (
"fmt"
"github.com/kylelemons/go-gypsy/yaml"
"os"
"path"
)
// Provides a set of configuration variables that describe how to interact with a SQL database.
type SQLConf struct {
driver string
openStr string
table string
latCol string
lngCol string
}
const (
DEFAULT_PGSQL_OPEN_STR = "user=postgres dbname=points sslmode=disable"
DEFAULT_MYSQL_OPEN_STR = "points/root/"
DEFAULT_TEST_OPEN_STR = "\"\""
)
// Returns a SQLConf based on the $DB environment variable
// Returns a PostgreSQL configuration as a default
func sqlConfFromEnv() *SQLConf {
var dbEnv = os.Getenv("DB")
switch dbEnv {
case "mysql":
return &SQLConf{driver: "mymysql", openStr: DEFAULT_MYSQL_OPEN_STR, table: "points", latCol: "lat", lngCol: "lng"}
case "mock":
return &SQLConf{driver: "testdb", openStr: DEFAULT_TEST_OPEN_STR, table: "points", latCol: "lat", lngCol: "lng"}
default:
return &SQLConf{driver: "postgres", openStr: DEFAULT_PGSQL_OPEN_STR, table: "points", latCol: "lat", lngCol: "lng"}
}
}
// Attempts to read config/geo.yml, and creates a SQLConf as described therein.
// Returns the DefaultSQLConf if no config/geo.yml is found, or an error
// if one arises during the process of parsing the configuration file.
func GetSQLConf() (*SQLConf, error) {
return GetSQLConfFromFile("config/geo.yml")
}
// Attempts to read from the passed in filename and creates a SQLconf as described therin.
// Retruns the DefaultSQLConf if the file cannot be found, or an error
// if one arises during the process of parsing the configuration file.
func GetSQLConfFromFile(filename string) (*SQLConf, error) {
DefaultSQLConf := sqlConfFromEnv()
configPath := path.Join(filename)
_, err := os.Stat(configPath)
if err != nil && os.IsNotExist(err) {
return DefaultSQLConf, nil
} else {
// Defaults to development environment, you can override
// by changing the $GO_ENV variable:
// `$ export GO_ENV=environment` (where environment
// can be "production", "test", "staging", etc.)
// TODO: Potentially find a better solution to handling environments
// Perhaps: https://github.com/adeven/goenv ?
goEnv := os.Getenv("GO_ENV")
if goEnv == "" {
goEnv = "development"
}
config, readYamlErr := yaml.ReadFile(configPath)
if readYamlErr != nil {
return nil, readYamlErr
}
return confFromYamlFile(config, goEnv)
}
return nil, err
}
// Creates a new SQLConf and returns a pointer to it.
// If it encounters an error during parsing the file,
// it will return an error instead.
func confFromYamlFile(config *yaml.File, goEnv string) (*SQLConf, error) {
// TODO Refactor this into a more generic method of retrieving info
// Get driver
driver, driveError := config.Get(fmt.Sprintf("%s.driver", goEnv))
if driveError != nil {
return nil, driveError
}
// Get openStr
openStr, openStrError := config.Get(fmt.Sprintf("%s.openStr", goEnv))
if openStrError != nil {
return nil, openStrError
}
// Get table
table, tableError := config.Get(fmt.Sprintf("%s.table", goEnv))
if tableError != nil {
return nil, tableError
}
// Get latCol
latCol, latColError := config.Get(fmt.Sprintf("%s.latCol", goEnv))
if latColError != nil {
return nil, latColError
}
// Get lngCol
lngCol, lngColError := config.Get(fmt.Sprintf("%s.lngCol", goEnv))
if lngColError != nil {
return nil, lngColError
}
sqlConf := &SQLConf{driver: driver, openStr: openStr, table: table, latCol: latCol, lngCol: lngCol}
return sqlConf, nil
}