-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
83 lines (66 loc) · 2.37 KB
/
root.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
package datastores
import (
"github.com/spf13/viper"
"time"
)
// ConfigItem represents a configuration item
type ConfigItem struct {
Id int64 `sql:"id" json:"id"`
Application string `sql:"application" json:"application"`
Machine string `sql:"machine" json:"machine"`
Name string `sql:"name" json:"name"`
Value string `sql:"value" json:"value"`
LastUpdated time.Time `sql:"updated" json:"updated"`
}
type ConfigResponse struct {
Status int `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
// ConfigService encapsulates account (user) based operations
// This allows us to create a testable service layer. See
// https://github.com/tonyhb/tonyhb.com/blob/master/posts/Building%20a%20testable%20Golang%20database%20layer.md
// for more information
type ConfigService interface {
// Initialize the store (create the DDL if necessary)
InitStore(overwrite bool) error
// Create / update a config item
Set(c *ConfigItem) (ConfigItem, error)
// Get a specific config item
Get(c *ConfigItem) (ConfigItem, error)
// Get all config items for the given application
GetAllForApplication(application string) ([]ConfigItem, error)
// Get all config items for all applications (including global)
GetAll() ([]ConfigItem, error)
// Get all applications (including global)
GetAllApplications() ([]string, error)
// Remove a config item
Remove(c *ConfigItem) error
}
// Get the currently configured datastore
func GetConfigDatastore() ConfigService {
// Get configuration information and return the appropriate
// provider based on what is configured
if viper.InConfig("datastore") {
dsConfig := viper.Sub("datastore")
// If we have MSSQL, use that:
if dsConfig.InConfig("mssql") {
return MSSqlDB{
Database: dsConfig.GetString("mssql.database"),
Address: dsConfig.GetString("mssql.address"),
User: dsConfig.GetString("mssql.user"),
Password: dsConfig.GetString("mssql.password")}
}
// If we have MySQL, use that:
if dsConfig.InConfig("mysql") {
return MySqlDB{
Database: dsConfig.GetString("mysql.database"),
Address: dsConfig.GetString("mysql.address"),
User: dsConfig.GetString("mysql.user"),
Password: dsConfig.GetString("mysql.password")}
}
}
// Last resort: use BoltDB
return BoltDB{
Database: viper.GetString("datastore.boltdb.database")}
}