-
Notifications
You must be signed in to change notification settings - Fork 0
/
module.go
184 lines (146 loc) · 3.69 KB
/
module.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package modules
import (
"github.com/spf13/cast"
"github.com/spf13/pflag"
C "github.com/yuw-mvc/yuw/configs"
E "github.com/yuw-mvc/yuw/exceptions"
"time"
)
type (
YuwInitialize struct {
Table bool
Redis bool
I18nT bool
Email bool
Admin bool
}
module struct {
util *Utils
}
)
func init() {
m := New()
m.cfg()
var YuwInitialized *YuwInitialize
E.ErrPanic(m.util.MapToStruct(
I.Get("YuwInitialize", []interface{}{}),
&YuwInitialized,
))
if YuwInitialized.Table {
strTimeLocation := cast.ToString(I.Get("Yuw.TimeLocation", C.LocationAsiaShanghai))
TimeLocation, err := m.util.SetTimeLocation(strTimeLocation)
E.ErrPanic(err)
m.db(TimeLocation)
}
if YuwInitialized.Redis {
m.rd()
}
if YuwInitialized.Email {
}
if YuwInitialized.Redis && YuwInitialized.Admin {
E.ErrPanic(
m.util.MapToStruct(
I.Get("Session", map[string]interface{}{}).(map[string]interface{}),
&sessionPoT,
),
)
}
}
func New() *module {
return &module {
util: NewUtils(),
}
}
func (module *module) cfg() {
if I != nil {
return
}
pflag.String("env", "", "environment configure")
pflag.Parse()
init := NewInitialize()
E.ErrPanic(init.Env.BindPFlags(pflag.CommandLine))
I = init.LoadInitializedFromYaml()
E.ErrArray(&E.ErrType{"yuw^m_a":I == nil})
}
func (module *module) db(timeLocation *time.Location) {
sysTimeLocation = timeLocation
var configs *dbConfigs
cfg := I.Get("DBClusters.Configure", map[string]interface{}{}).(map[string]interface{})
if len(cfg) == 0 {
configs = &dbConfigs {
DriverName: "mysql",
MaxOpen: 1000,
MaxIdle: 500,
ShowedSQL: false,
CachedSQL: false,
}
} else {
E.ErrPanic(module.util.MapToStruct(cfg, &configs))
}
env := I.Get("DBClusters.Databases", map[string]interface{}{}).(map[string]interface{})
E.ErrArray(&E.ErrType{"yuw^m_c":len(env) == 0})
masterDB = &dbs{}
slaverDB = &dbs{}
for table, db := range env {
for method, databases := range db.(map[string]interface{}) {
var dbEngines []*database = make([]*database, len(databases.([]interface{})))
for key, database := range databases.([]interface{}) {
var cluster *dbCluster
toMap := module.util.InterfaceToStringInMap(database.(map[interface{}]interface{}))
toMap["Table"] = table
E.ErrPanic(module.util.MapToStruct(toMap, &cluster))
dbEngine := NewDatabase()
dbEngine.dbCluster = cluster
dbEngine.dbConfigs = configs
dbEngines[key] = dbEngine.instance()
}
switch method {
case "master":
(*masterDB)[table] = dbEngines
case "slaver":
(*slaverDB)[table] = dbEngines
default:
continue
}
}
}
}
func (module *module) rd() {
rdNetwork := cast.ToString(I.Get("Redis.Network", defaultRdNetworkType))
if ok, _ := module.util.StrContains(rdNetwork, rdNetworkType ...); ok == false {
rdNetwork = defaultRdNetworkType
}
rdReader := I.Get("Redis.Reader", []interface{}{}).([]interface{})
rdWriter := I.Get("Redis.Writer", []interface{}{}).([]interface{})
E.ErrArray(&E.ErrType{
"yuw^m_init_e":len(rdReader) == 0 || len(rdWriter) == 0,
})
rdsRW := map[string][]interface{} {
"reader": rdReader,
"writer": rdWriter,
}
rdsReader = &rds{}
rdsWriter = &rds{}
for k, rdsVal := range rdsRW {
for _, rdVal := range rdsVal {
var res *RedisPoT
err := module.util.InterfaceToStruct(rdVal.(map[interface{}]interface{}), &res)
if err != nil {
continue
}
r := NewRedis()
r.rPoT = res
var dbKey string = prefixRdDB + cast.ToString(r.rPoT.DB)
switch k {
case "reader":
(*rdsReader)[dbKey] = append((*rdsReader)[dbKey], r.instance())
continue
case "writer":
(*rdsWriter)[dbKey] = append((*rdsWriter)[dbKey], r.instance())
continue
default:
continue
}
}
}
}