-
Notifications
You must be signed in to change notification settings - Fork 168
/
config.go
102 lines (92 loc) · 2.82 KB
/
config.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
package database
import (
"encoding/json"
"fmt"
"github.com/fsnotify/fsnotify"
"github.com/snowlyg/iris-admin/g"
"github.com/snowlyg/iris-admin/server/viper_server"
"github.com/spf13/viper"
)
var CONFIG = Mysql{
Path: "127.0.0.1:3306",
Config: "charset=utf8mb4&parseTime=True&loc=Local",
DbName: "iris-admin",
Username: "root",
Password: "",
MaxIdleConns: 0,
MaxOpenConns: 0,
LogMode: false,
LogZap: "error",
}
type Mysql struct {
Path string `mapstructure:"path" json:"path" yaml:"path"`
Config string `mapstructure:"config" json:"config" yaml:"config"`
DbName string `mapstructure:"db-name" json:"db-name" yaml:"db-name"`
Username string `mapstructure:"username" json:"username" yaml:"username"`
Password string `mapstructure:"password" json:"password" yaml:"password"`
MaxIdleConns int `mapstructure:"max-idle-conns" json:"max-idle-conns" yaml:"max-idle-conns"`
MaxOpenConns int `mapstructure:"max-open-conns" json:"max-open-conns" yaml:"max-open-conns"`
LogMode bool `mapstructure:"log-mode" json:"log-mode" yaml:"log-mode"`
LogZap string `mapstructure:"log-zap" json:"log-zap" yaml:"log-zap"` //silent,error,warn,info,zap
}
// Dsn return mysql dsn
func (m *Mysql) Dsn() string {
return fmt.Sprintf("%s%s?%s", m.BaseDsn(), m.DbName, m.Config)
}
// Dsn return
func (m *Mysql) BaseDsn() string {
return fmt.Sprintf("%s:%s@tcp(%s)/", m.Username, m.Password, m.Path)
}
// IsExist config file is exist
func IsExist() bool {
return getViperConfig().IsFileExist()
}
// Remove remove config file
func Remove() error {
return getViperConfig().Remove()
}
// Recover
func Recover() error {
b, err := json.Marshal(CONFIG)
if err != nil {
return err
}
return getViperConfig().Recover(b)
}
// getViperConfig get viper config
func getViperConfig() viper_server.ViperConfig {
configName := "mysql"
mxIdleConns := fmt.Sprintf("%d", CONFIG.MaxIdleConns)
mxOpenConns := fmt.Sprintf("%d", CONFIG.MaxOpenConns)
logMode := fmt.Sprintf("%t", CONFIG.LogMode)
return viper_server.ViperConfig{
Debug: true,
Directory: g.ConfigDir,
Name: configName,
Type: g.ConfigType,
Watch: func(vi *viper.Viper) error {
if err := vi.Unmarshal(&CONFIG); err != nil {
return fmt.Errorf("get Unarshal error: %v", err)
}
// watch config file change
vi.OnConfigChange(func(e fsnotify.Event) {
fmt.Println("Config file changed:", e.Name)
})
vi.WatchConfig()
return nil
},
//
Default: []byte(`
{
"path": "` + CONFIG.Path + `",
"config": "` + CONFIG.Config + `",
"db-name": "` + CONFIG.DbName + `",
"username": "` + CONFIG.Username + `",
"password": "` + CONFIG.Password + `",
"max-idle-conns": ` + mxIdleConns + `,
"max-open-conns": ` + mxOpenConns + `,
"log-mode": ` + logMode + `,
"log-zap": "` + CONFIG.LogZap + `"
}`),
}
}