forked from tophubs/TopList
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MySql.go
54 lines (49 loc) · 976 Bytes
/
MySql.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
package Config
import (
"fmt"
"github.com/BurntSushi/toml"
"log"
"os"
"path/filepath"
"strings"
"sync"
)
type MysqlCfg struct {
Source, Driver string
}
var (
cfg *MysqlCfg
once sync.Once
cfgLock = new(sync.RWMutex)
)
// 读取配置
func MySql() *MysqlCfg {
once.Do(ReloadConfig)
cfgLock.RLock()
defer cfgLock.RUnlock()
return cfg
}
func GetMySqlFilePath() string {
currentPath, err := os.Getwd()
if err != nil {
log.Fatal("获取目录失败!")
}
if strings.HasSuffix(currentPath, "App") {
return strings.ReplaceAll(currentPath, "App", "Config") + "/mysql.toml"
}
return currentPath + "/Config/mysql.toml"
}
func ReloadConfig() {
filePath, err := filepath.Abs(GetMySqlFilePath())
if err != nil {
panic(err)
}
fmt.Printf("parse mysql config once. filePath: %s\n", filePath)
config := new(MysqlCfg)
if _, err := toml.DecodeFile(filePath, config); err != nil {
panic(err)
}
cfgLock.Lock()
defer cfgLock.Unlock()
cfg = config
}