-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
96 lines (81 loc) · 1.81 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
package gorm
import (
"github.com/hopeio/cherry/utils/configor"
dbi "github.com/hopeio/cherry/utils/dao/database"
"gorm.io/gorm/schema"
"gorm.io/plugin/prometheus"
"time"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
type Config struct {
Type, Charset, Database, TimeZone string
Host string `flag:"name:db_host;usage:数据库host"`
Port int32
User, Password string
Postgres Postgres
Mysql Mysql
Sqlite Sqlite
MaxIdleConns, MaxOpenConns int
ConnMaxLifetime, ConnMaxIdleTime time.Duration
Gorm gorm.Config
EnableStdLogger bool
Logger logger.Config
NamingStrategy schema.NamingStrategy
EnablePrometheus bool
Prometheus prometheus.Config
PrometheusConfigs []PrometheusConfig
}
type Postgres struct {
Schema string
SSLMode string
}
type Mysql struct {
ParseTime string
Loc string
}
type Sqlite struct {
DSN string
}
type PrometheusConfig struct {
Prefix string
Interval uint32
VariableNames []string
}
func (c *Config) Init() {
if c.Type == "" {
c.Type = dbi.Postgres
}
configor.DurationNotify("SlowThreshold", c.Logger.SlowThreshold, 10*time.Millisecond)
if c.TimeZone == "" {
c.TimeZone = "Asia/Shanghai"
}
if c.Postgres.SSLMode == "" {
c.Postgres.SSLMode = "disable"
}
if c.Mysql.Loc == "" {
c.Mysql.Loc = "Local"
}
if c.Mysql.ParseTime == "" {
c.Mysql.ParseTime = "True"
}
if c.Charset == "" {
if c.Type == dbi.Mysql {
c.Charset = "utf8mb4"
}
if c.Type == dbi.Postgres {
c.Charset = "utf8"
}
}
if c.Port == 0 {
if c.Type == dbi.Mysql {
c.Port = 3306
}
if c.Type == dbi.Postgres {
c.Port = 5432
}
}
if c.Sqlite.DSN == "" {
c.Sqlite.DSN = "./sqlite.db"
}
}