forked from zlyuancn/zconn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
single.go
71 lines (57 loc) · 1.51 KB
/
single.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
/*
-------------------------------------------------
Author : Zhang Fan
date: 2020/6/6
Description :
-------------------------------------------------
*/
package zconn
import (
"fmt"
"github.com/spf13/viper"
)
var singleManager = NewManager()
// 设置单例管理器的选项
func SetSingleManagerOptions(opts ...Option) {
for _, o := range opts {
o(singleManager.opts)
}
}
// 添加配置, 同一个conn类型中重复的conn名会被替换掉
func AddConfig(conntype ConnType, config interface{}, conn_name ...string) {
singleManager.AddConfig(conntype, config, conn_name...)
}
// 移除, 移除之前会关闭连接
func Remove(conntype ConnType, conn_name ...string) {
singleManager.Remove(conntype, conn_name...)
}
// 连接所有
func ConnectAll() error {
return singleManager.ConnectAll()
}
// 关闭所有连接
func CloseAll() {
singleManager.CloseAll()
}
// 获取Conn
func GetConn(conntype ConnType, conn_name ...string) (conn *Conn, ok bool) {
return singleManager.GetConn(conntype, conn_name...)
}
// 添加配置文件, 支持 json, ini, toml, yaml等
func AddFile(file string, filetype ...string) error {
return singleManager.AddFile(file, filetype...)
}
// 添加viper树
func AddViperTree(tree *viper.Viper) error {
return singleManager.AddViperTree(tree)
}
func panicOnErr(err error) {
if err != nil {
panic(err)
}
}
func panicOnErrf(err error, format string, msg ...interface{}) {
if err != nil {
panic(fmt.Sprintf("%s: %s", fmt.Sprintf(format, msg...), err))
}
}