-
Notifications
You must be signed in to change notification settings - Fork 0
/
tc.go
134 lines (115 loc) · 2.35 KB
/
tc.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
package tinycells
import (
"errors"
"github.com/andyzhou/tinycells/cmd"
"github.com/andyzhou/tinycells/config"
"github.com/andyzhou/tinycells/crypt"
"github.com/andyzhou/tinycells/db"
"github.com/andyzhou/tinycells/logger"
"github.com/andyzhou/tinycells/sys"
"github.com/andyzhou/tinycells/web"
//"github.com/andyzhou/tinycells/mq"
"github.com/andyzhou/tinycells/util"
"sync"
)
//global variable
var (
_tc *TinyCells
_tcOnce sync.Once
)
//interface
type TinyCells struct {
//mq *mq.MQ
wb *web.Web
db *db.DB
single *sys.Signal
logger *logger.Logger
cmd *cmd.Cmd
crypt *crypt.Crypt
cfg *config.Config
util *util.Util
}
//get single instance
func GetTC() *TinyCells {
_tcOnce.Do(func() {
_tc = NewTinyCells()
})
return _tc
}
//construct
func NewTinyCells() *TinyCells {
this := &TinyCells{
db: db.NewDB(),
single: sys.NewSignal(),
logger: logger.NewLogger(),
cmd: cmd.NewCmd(),
crypt: crypt.NewCrypt(),
util: util.NewUtil(),
}
return this
}
//////////////////////////////////////////////
//setup for first init
//this called before use sub instance
//////////////////////////////////////////////
//init web
func (f *TinyCells) InitWeb() {
if f.wb == nil {
f.wb = web.NewWeb()
}
}
//setup logger
func (f *TinyCells) SetUpLogger(params ...interface{}) error {
if params == nil || len(params) < 0 {
return errors.New("invalid parameter")
}
config, ok := params[0].(*logger.Config)
if !ok || config == nil {
return errors.New("invalid logger config")
}
f.logger.SetConfig(config)
return nil
}
//setup config
func (f *TinyCells) SetupConfig(params ...interface{}) error {
if f.cfg != nil {
return errors.New("config instance had setup")
}
f.cfg = config.NewConfig(params...)
return nil
}
///////////////////////
//get sub instance
///////////////////////
//get single
func (f *TinyCells) GetSingle() *sys.Signal {
return f.single
}
//get web
func (f *TinyCells) GetWeb() *web.Web {
return f.wb
}
//get db
func (f *TinyCells) GetDB() *db.DB {
return f.db
}
//get logger
func (f *TinyCells) GetLogger() *logger.Logger {
return f.logger
}
//get cmd
func (f *TinyCells) GetCmd() *cmd.Cmd {
return f.cmd
}
//get config
func (f *TinyCells) GetConfig() *config.Config {
return f.cfg
}
//get crypt
func (f *TinyCells) GetCrypt() *crypt.Crypt {
return f.crypt
}
//get util
func (f *TinyCells) GetUtil() *util.Util {
return f.util
}