/
init_svc.go
153 lines (127 loc) · 5.81 KB
/
init_svc.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package service
import (
"context"
"github.com/lidenger/otpserver/config/log"
"github.com/lidenger/otpserver/config/serverconf"
"github.com/lidenger/otpserver/config/storeconf/mysqlconf"
"github.com/lidenger/otpserver/config/storeconf/pgsqlconf"
"github.com/lidenger/otpserver/internal/store"
"github.com/lidenger/otpserver/internal/store/localstore"
"github.com/lidenger/otpserver/internal/store/memorystore"
"github.com/lidenger/otpserver/internal/store/mysqlstore"
"github.com/lidenger/otpserver/internal/store/pgsqlstore"
"github.com/lidenger/otpserver/pkg/enum"
)
var SecretSvcIns = &SecretSvc{}
var ServerSvcIns = &ServerSvc{}
var ServerIpListSvcIns = &ServerIpListSvc{}
// store type | healthFunc
var svcStoreStatusMap = make(map[string][]store.HealthFunc)
var loadAllStoreArr = make([]store.LoadAllFunc, 0)
func Initialize(storeDetectionEventChan chan<- struct{}) {
conf := serverconf.GetSysConf()
SecretSvcIns.storeDetectionEventChan = storeDetectionEventChan
SecretSvcIns.storeDetectionEventChan = storeDetectionEventChan
ServerIpListSvcIns.storeDetectionEventChan = storeDetectionEventChan
mysqlSecretStore := &mysqlstore.SecretStore{DB: mysqlconf.DB}
mysqlServerStore := &mysqlstore.ServerStore{DB: mysqlconf.DB}
mysqlServerIpListStore := &mysqlstore.ServerIpListStore{DB: mysqlconf.DB}
pgsqlSecretStore := &pgsqlstore.SecretStore{DB: pgsqlconf.DB}
pgsqlServerStore := &pgsqlstore.ServerStore{DB: pgsqlconf.DB}
pgsqlServerIpListStore := &pgsqlstore.ServerIpListStore{DB: pgsqlconf.DB}
localStoreRootPath := conf.Server.RootPath + conf.Store.RootPath
localSecretStore := &localstore.SecretStore{RootPath: localStoreRootPath}
localServerStore := &localstore.ServerStore{RootPath: localStoreRootPath}
localServerIpListStore := &localstore.ServerIpListStore{RootPath: localStoreRootPath}
memoryDependSecretStoreArr := make([]store.SecretStore, 0)
memoryDependServerStoreArr := make([]store.ServerStore, 0)
memoryDependServerIpListStoreArr := make([]store.ServerIpListStore, 0)
switch conf.Store.MainStore {
case enum.MySQLStore:
SecretSvcIns.Store = mysqlSecretStore
ServerSvcIns.Store = mysqlServerStore
ServerIpListSvcIns.Store = mysqlServerIpListStore
localSecretStore.Store = mysqlSecretStore
localServerStore.Store = mysqlServerStore
localServerIpListStore.Store = mysqlServerIpListStore
addSvcStore(enum.MySQLStore, SecretSvcIns.Store, ServerSvcIns.Store, ServerIpListSvcIns.Store)
case enum.PostGreSQLStore:
SecretSvcIns.Store = pgsqlSecretStore
ServerSvcIns.Store = pgsqlServerStore
ServerIpListSvcIns.Store = mysqlServerIpListStore
localSecretStore.Store = pgsqlSecretStore
localServerStore.Store = pgsqlServerStore
localServerIpListStore.Store = pgsqlServerIpListStore
addSvcStore(enum.PostGreSQLStore, SecretSvcIns.Store, ServerSvcIns.Store, ServerIpListSvcIns.Store)
}
switch conf.Store.BackupStore {
case enum.MySQLStore:
SecretSvcIns.StoreBackup = mysqlSecretStore
ServerSvcIns.StoreBackup = mysqlServerStore
ServerIpListSvcIns.StoreBackup = mysqlServerIpListStore
addSvcStore(enum.MySQLStore, SecretSvcIns.StoreBackup, ServerSvcIns.StoreBackup, ServerIpListSvcIns.StoreBackup)
case enum.PostGreSQLStore:
SecretSvcIns.StoreBackup = pgsqlSecretStore
ServerSvcIns.StoreBackup = pgsqlServerStore
ServerIpListSvcIns.StoreBackup = pgsqlServerIpListStore
addSvcStore(enum.PostGreSQLStore, SecretSvcIns.StoreBackup, ServerSvcIns.StoreBackup, ServerIpListSvcIns.StoreBackup)
}
memoryDependSecretStoreArr = append(memoryDependSecretStoreArr, SecretSvcIns.Store, SecretSvcIns.StoreBackup)
memoryDependServerStoreArr = append(memoryDependServerStoreArr, ServerSvcIns.Store, ServerSvcIns.StoreBackup)
memoryDependServerIpListStoreArr = append(memoryDependServerIpListStoreArr, ServerIpListSvcIns.Store, ServerIpListSvcIns.StoreBackup)
if conf.Store.IsEnableLocal {
memoryDependSecretStoreArr = append(memoryDependSecretStoreArr, localSecretStore)
memoryDependServerStoreArr = append(memoryDependServerStoreArr, localServerStore)
memoryDependServerIpListStoreArr = append(memoryDependServerIpListStoreArr, localServerIpListStore)
loadAllStoreArr = append(loadAllStoreArr, localSecretStore, localServerStore, localServerIpListStore)
}
if conf.Store.IsEnableMemory {
secretMemory := &memorystore.SecretStore{
Stores: memoryDependSecretStoreArr,
StoreDetectionEventChan: storeDetectionEventChan,
}
SecretSvcIns.StoreMemory = secretMemory
serverMemory := &memorystore.ServerStore{
Stores: memoryDependServerStoreArr,
StoreDetectionEventChan: storeDetectionEventChan,
}
ServerSvcIns.StoreMemory = serverMemory
serverIpListMemory := &memorystore.ServerIpListStore{
Stores: memoryDependServerIpListStoreArr,
StoreDetectionEventChan: storeDetectionEventChan,
}
ServerIpListSvcIns.StoreMemory = serverIpListMemory
loadAllStoreArr = append(loadAllStoreArr, secretMemory, serverMemory, serverIpListMemory)
addSvcStore(enum.MemoryStore, SecretSvcIns.StoreMemory, ServerSvcIns.StoreMemory, ServerIpListSvcIns.StoreMemory)
}
log.Info("Service初始化完成:%s", "SecretSvc", "ServerSvc", "ServerIpListSvc")
}
func LoadAllData() {
for _, s := range loadAllStoreArr {
if s.GetStoreType() == enum.LocalStore || s.GetStoreType() == enum.MemoryStore {
_ = s.LoadAll(context.Background())
}
}
}
func FetchLocalStore() []store.LoadAllFunc {
ss := make([]store.LoadAllFunc, 0)
for _, s := range loadAllStoreArr {
if s.GetStoreType() == enum.LocalStore {
ss = append(ss, s)
}
}
return ss
}
func addSvcStore(typ string, funcs ...store.HealthFunc) {
fs := svcStoreStatusMap[typ]
if fs == nil {
fs = make([]store.HealthFunc, 0)
}
fs = append(fs, funcs...)
svcStoreStatusMap[typ] = fs
}
// FetchSvcStores 通过类型获取svc store
func FetchSvcStores(typ string) []store.HealthFunc {
fs := svcStoreStatusMap[typ]
return fs
}