/
root.go
93 lines (73 loc) · 2.56 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
84
85
86
87
88
89
90
91
92
93
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"`
}
// ConfigResponse represents an API response
type ConfigResponse struct {
Status int `json:"status"`
Message string `json:"message"`
Data interface{} `json:"data"`
}
// WebSocketResponse represents a WebSocket event response
type WebSocketResponse struct {
Type string `json:"type"`
Data ConfigItem `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
dsType := viper.GetString("datastore.type")
if dsType != "" {
switch dsType {
case "mssql":
// If we have MSSQL, use that:
return MSSqlDB{
Database: viper.GetString("datastore.database"),
Address: viper.GetString("datastore.address"),
User: viper.GetString("datastore.user"),
Password: viper.GetString("datastore.password")}
case "mysql":
// If we have MySQL, use that:
return MySqlDB{
Database: viper.GetString("datastore.database"),
Address: viper.GetString("datastore.address"),
User: viper.GetString("datastore.user"),
Password: viper.GetString("datastore.password")}
case "boltdb":
return BoltDB{
Database: viper.GetString("datastore.database")}
}
}
return UnknownDB{}
}