/
g.go
157 lines (141 loc) · 4.91 KB
/
g.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
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
// 常用数据类型以及对象封装
package g
import (
"gitee.com/johng/gf/g/os/gcfg"
"gitee.com/johng/gf/g/os/gview"
"gitee.com/johng/gf/g/util/gconv"
"gitee.com/johng/gf/g/frame/gins"
"gitee.com/johng/gf/g/os/gcache"
"gitee.com/johng/gf/g/os/gfsnotify"
"gitee.com/johng/gf/g/database/gdb"
"gitee.com/johng/gf/g/database/gredis"
"gitee.com/johng/gf/g/net/ghttp"
"gitee.com/johng/gf/g/net/gtcp"
"gitee.com/johng/gf/g/net/gudp"
"gitee.com/johng/gf/g/util/gregx"
)
const (
gIS_DATABASE_CONFIG_CACHED = "gf.core.component.database.cached"
)
// 常用map数据结构(使用别名)
type Map = map[string]interface{}
// 常用list数据结构(使用别名)
type List = []Map
// 阻塞等待HTTPServer执行完成(同一进程多HTTPServer情况下)
func Wait() {
ghttp.Wait()
}
// HTTPServer单例对象
func Server(name...interface{}) *ghttp.Server {
return ghttp.GetServer(name...)
}
// TCPServer单例对象
func TcpServer(name...interface{}) *gtcp.Server {
return gtcp.GetServer(name...)
}
// UDPServer单例对象
func UdpServer(name...interface{}) *gudp.Server {
return gudp.GetServer(name...)
}
// 核心对象:View
func View() *gview.View {
return gins.View()
}
// Config配置管理对象
// 配置文件目录查找依次为:启动参数cfgpath、当前程序运行目录
func Config() *gcfg.Config {
return gins.Config()
}
// 数据库操作对象,使用了连接池
func Database(name...string) *gdb.Db {
config := gins.Config()
if config == nil {
return nil
}
// 数据库配置是否已经设置
if gcache.Get(gIS_DATABASE_CONFIG_CACHED) == nil {
if m := config.GetMap("database"); m != nil {
c := gdb.Config{}
for group, v := range m {
cg := gdb.ConfigGroup{}
if list, ok := v.([]interface{}); ok {
for _, nodev := range list {
node := gdb.ConfigNode{}
nodem := nodev.(map[string]interface{})
if value, ok := nodem["host"]; ok {
node.Host = gconv.String(value)
}
if value, ok := nodem["port"]; ok {
node.Port = gconv.String(value)
}
if value, ok := nodem["user"]; ok {
node.User = gconv.String(value)
}
if value, ok := nodem["pass"]; ok {
node.Pass = gconv.String(value)
}
if value, ok := nodem["name"]; ok {
node.Name = gconv.String(value)
}
if value, ok := nodem["type"]; ok {
node.Type = gconv.String(value)
}
if value, ok := nodem["role"]; ok {
node.Role = gconv.String(value)
}
if value, ok := nodem["charset"]; ok {
node.Charset = gconv.String(value)
}
if value, ok := nodem["priority"]; ok {
node.Priority = gconv.Int(value)
}
cg = append(cg, node)
}
}
c[group] = cg
}
gdb.SetConfig(c)
gcache.Set(gIS_DATABASE_CONFIG_CACHED, struct{}{}, 0)
// 使用gfsnotify进行文件监控,当配置文件有任何变化时,清空数据库配置缓存
gfsnotify.Add(Config().GetFilePath(), func(event *gfsnotify.Event) {
gcache.Remove(gIS_DATABASE_CONFIG_CACHED)
})
}
}
if db, err := gdb.New(name...); err == nil {
return db
} else {
return nil
}
}
// Redis操作对象,使用了连接池
func Redis(name...string) *gredis.Redis {
group := "default"
if len(name) > 0 {
group = name[0]
}
config := gins.Config()
if config == nil {
return nil
}
if m := config.GetMap("redis"); m != nil {
// host:port[,db[,pass]]
if v, ok := m[group]; ok {
array, err := gregx.MatchString(`(.+):(\d+),{0,1}(\d*),{0,1}(.*)`, gconv.String(v))
if err == nil {
return gredis.New(gredis.Config{
Host : array[1],
Port : gconv.Int(array[2]),
Db : gconv.Int(array[3]),
Pass : array[4],
})
}
}
}
return nil
}