/
mysql_options.go
90 lines (72 loc) · 3.4 KB
/
mysql_options.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
// Copyright 2020 Lingfei Kong <colin404@foxmail.com>. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package options
import (
"time"
"github.com/spf13/pflag"
"gorm.io/gorm"
"github.com/marmotedu/iam/pkg/db"
)
// MySQLOptions defines options for mysql database.
type MySQLOptions struct {
Host string `json:"host,omitempty" mapstructure:"host"`
Username string `json:"username,omitempty" mapstructure:"username"`
Password string `json:"-" mapstructure:"password"`
Database string `json:"database" mapstructure:"database"`
MaxIdleConnections int `json:"max-idle-connections,omitempty" mapstructure:"max-idle-connections"`
MaxOpenConnections int `json:"max-open-connections,omitempty" mapstructure:"max-open-connections"`
MaxConnectionLifeTime time.Duration `json:"max-connection-life-time,omitempty" mapstructure:"max-connection-life-time"`
LogLevel int `json:"log-level" mapstructure:"log-level"`
}
// NewMySQLOptions create a `zero` value instance.
func NewMySQLOptions() *MySQLOptions {
return &MySQLOptions{
Host: "127.0.0.1:3306",
Username: "",
Password: "",
Database: "",
MaxIdleConnections: 100,
MaxOpenConnections: 100,
MaxConnectionLifeTime: time.Duration(10) * time.Second,
LogLevel: 1, // Silent
}
}
// Validate verifies flags passed to MySQLOptions.
func (o *MySQLOptions) Validate() []error {
errs := []error{}
return errs
}
// AddFlags adds flags related to mysql storage for a specific APIServer to the specified FlagSet.
func (o *MySQLOptions) AddFlags(fs *pflag.FlagSet) {
fs.StringVar(&o.Host, "mysql.host", o.Host, ""+
"MySQL service host address. If left blank, the following related mysql options will be ignored.")
fs.StringVar(&o.Username, "mysql.username", o.Username, ""+
"Username for access to mysql service.")
fs.StringVar(&o.Password, "mysql.password", o.Password, ""+
"Password for access to mysql, should be used pair with password.")
fs.StringVar(&o.Database, "mysql.database", o.Database, ""+
"Database name for the server to use.")
fs.IntVar(&o.MaxIdleConnections, "mysql.max-idle-connections", o.MaxOpenConnections, ""+
"Maximum idle connections allowed to connect to mysql.")
fs.IntVar(&o.MaxOpenConnections, "mysql.max-open-connections", o.MaxOpenConnections, ""+
"Maximum open connections allowed to connect to mysql.")
fs.DurationVar(&o.MaxConnectionLifeTime, "mysql.max-connection-life-time", o.MaxConnectionLifeTime, ""+
"Maximum connection life time allowed to connecto to mysql.")
fs.IntVar(&o.LogLevel, "mysql.log-mode", o.LogLevel, ""+
"Specify gorm log level.")
}
// NewClient create mysql store with the given config.
func (o *MySQLOptions) NewClient() (*gorm.DB, error) {
opts := &db.Options{
Host: o.Host,
Username: o.Username,
Password: o.Password,
Database: o.Database,
MaxIdleConnections: o.MaxIdleConnections,
MaxOpenConnections: o.MaxOpenConnections,
MaxConnectionLifeTime: o.MaxConnectionLifeTime,
LogLevel: o.LogLevel,
}
return db.New(opts)
}