/
conf.go
140 lines (117 loc) · 2.9 KB
/
conf.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
package app
import (
_ "github.com/go-sql-driver/mysql"
"github.com/go-xorm/xorm"
"gopkg.in/redis.v5"
"gopkg.in/yaml.v2"
"sync"
)
type Config struct {
ServerConf struct {
Port int `yaml:"port"`
} `yaml:"server"`
MySQLConf struct {
Driver string `yaml:"driver"`
DataSource string `yaml:"dataSource"`
ShowSQL bool `yaml:"showSql"`
MaxIdleConns int `yaml:"maxIdleConns"`
MaxOpenConns int `yaml:"maxOpenConns"`
} `yaml:"mysql"`
RedisConf struct {
Address string `yaml:"address"`
Password string `yaml:"password"`
} `yaml:"redis"`
}
type MySQLTemplate struct {
engine *xorm.Engine
}
const (
mysqlStoreEngine = "InnoDB"
mysqlCharset = "utf8mb4"
)
// http://stackoverflow.com/questions/20240179/nil-detection-in-go
var (
mutex sync.RWMutex
config *Config
mySQLTemplate *MySQLTemplate
redisClient *redis.Client
)
func LoadConfig() *Config {
if config != nil {
return config
}
mutex.Lock()
defer mutex.Unlock()
bytes := GetAppConfBytes()
if err := yaml.Unmarshal(bytes, &config); err != nil {
panic(err)
}
return config
}
func ConnectToRedis(config *Config) {
redisConfig := config.RedisConf
redisClient = redis.NewClient(&redis.Options{
Addr: redisConfig.Address,
Password: redisConfig.Password,
DB: 0,
})
}
func GetRedisClient() *redis.Client {
return redisClient
}
func CloseRedisClient() error {
return redisClient.Close()
}
func ConnectToMySQL(config *Config) {
mysqlConfig := config.MySQLConf
engine, err := xorm.NewEngine(mysqlConfig.Driver, mysqlConfig.DataSource)
if err != nil {
panic(err)
}
engine.StoreEngine(mysqlStoreEngine)
engine.Charset(mysqlCharset)
engine.ShowSQL(mysqlConfig.ShowSQL)
engine.SetMaxIdleConns(mysqlConfig.MaxIdleConns)
engine.SetMaxOpenConns(mysqlConfig.MaxOpenConns)
mySQLTemplate = &MySQLTemplate{
engine: engine,
}
}
func GetMySQLTemplate() *MySQLTemplate {
return mySQLTemplate
}
func (t *MySQLTemplate) XormEngine() *xorm.Engine {
return t.engine
}
func (t *MySQLTemplate) Insert(bean interface{}) (int64, error) {
return t.engine.Insert(bean)
}
// Retrieve one record from database by id
// bean: a point of struct
//
// Example:
// GetById(1, new(models.User))
//
func (t *MySQLTemplate) GetById(id int64, bean interface{}) interface{} {
exists, err := t.engine.Id(id).Get(bean)
if err != nil || !exists {
return nil
}
return bean
}
// Retrieve one record from database, bean's non-empty fields will be as conditions
// bean: a point of struct
func (t *MySQLTemplate) GetByNonEmptyFields(bean interface{}) interface{} {
exists, err := t.engine.Get(bean)
if err != nil || !exists {
return nil
}
return bean
}
// Delete records from database, bean's non-empty fields will be as conditions
func (t *MySQLTemplate) DeleteByNonEmptyFields(bean interface{}) (int64, error) {
return t.engine.Delete(bean)
}
func (t *MySQLTemplate) Close() error {
return t.engine.Close()
}