forked from shenghui0779/yiigo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
113 lines (87 loc) · 2.22 KB
/
init.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
package yiigo
import (
"path/filepath"
"strings"
"sync"
"github.com/gorilla/websocket"
"github.com/nsqio/go-nsq"
)
// InitOption yiigo初始化选项
type InitOption func(wg *sync.WaitGroup)
// WithMySQL 注册MySQL
func WithMySQL(name string, cfg *DBConfig) InitOption {
return func(wg *sync.WaitGroup) {
defer wg.Done()
initDB(name, MySQL, cfg)
}
}
// WithPostgres 注册Postgres
func WithPostgres(name string, cfg *DBConfig) InitOption {
return func(wg *sync.WaitGroup) {
defer wg.Done()
initDB(name, Postgres, cfg)
}
}
// WithSQLite 注册SQLite
func WithSQLite(name string, cfg *DBConfig) InitOption {
return func(wg *sync.WaitGroup) {
defer wg.Done()
initDB(name, SQLite, cfg)
}
}
// WithMongo 注册MongoDB
// [DSN] mongodb://localhost:27017/?connectTimeoutMS=10000&minPoolSize=10&maxPoolSize=20&maxIdleTimeMS=60000&readPreference=primary
// [Reference] https://docs.mongodb.com/manual/reference/connection-string
func WithMongo(name string, dsn string) InitOption {
return func(wg *sync.WaitGroup) {
defer wg.Done()
initMongoDB(name, dsn)
}
}
// WithRedis 注册Redis
func WithRedis(name string, cfg *RedisConfig) InitOption {
return func(wg *sync.WaitGroup) {
defer wg.Done()
initRedis(name, cfg)
}
}
// WithLogger 注册日志
func WithLogger(name string, cfg *LoggerConfig) InitOption {
return func(wg *sync.WaitGroup) {
defer wg.Done()
if v := strings.TrimSpace(cfg.Filename); len(v) != 0 {
cfg.Filename = filepath.Clean(v)
}
initLogger(name, cfg)
}
}
// WithNSQProducer 设置nsq生产者
func WithNSQProducer(nsqd string, cfg *nsq.Config) InitOption {
return func(wg *sync.WaitGroup) {
defer wg.Done()
initNSQProducer(nsqd, cfg)
}
}
// WithNSQConsumers 设置nsq消费者
func WithNSQConsumers(lookupd []string, consumers ...NSQConsumer) InitOption {
return func(wg *sync.WaitGroup) {
defer wg.Done()
setNSQConsumers(lookupd, consumers...)
}
}
// WithWebsocket 设置websocket
func WithWebsocket(upgrader *websocket.Upgrader) InitOption {
return func(wg *sync.WaitGroup) {
defer wg.Done()
wsupgrader = upgrader
}
}
// Init yiigo初始化
func Init(options ...InitOption) {
var wg sync.WaitGroup
for _, f := range options {
wg.Add(1)
go f(&wg)
}
wg.Wait()
}