Fetch the repository succeeded.
package define
import (
"fmt"
"github.com/go-sql-driver/mysql"
"net/url"
"strings"
)
var (
PrintfDSN = true
)
// mysql
type ConnectMysql struct {
Addr string `yaml:"addr"`
UserName string `yaml:"user"`
Password string `yaml:"password"`
Database string `yaml:"db"`
Charset string `yaml:"charset"`
Options string `yaml:"options"`
initOpt bool
}
func (c *ConnectMysql) GetPrintDSN() string {
if !c.initOpt {
var params map[string]string
var optValues []string
if c.Options != "" {
optValues = strings.Split(c.Options, "&")
params = make(map[string]string, len(optValues))
for _, v := range optValues {
v = strings.Trim(v, " ")
if v != "" {
kv := strings.Split(v, "=")
if len(kv) > 1 {
params[strings.Trim(kv[0], " ")] = strings.Trim(kv[1], " ")
} else {
params[strings.Trim(kv[0], " ")] = ""
}
}
}
}
c._setOption(params)
}
return fmt.Sprintf("mysql://%s/%s?%s", c.Addr, c.Database, c.Options)
}
func (c *ConnectMysql) GetConnectDSN() string {
return fmt.Sprintf("%s:%s@tcp(%s)/%s?%s", c.UserName, c.Password, c.Addr, c.Database, c.Options)
}
func (c *ConnectMysql) _setOption(params map[string]string) {
if params == nil {
params = make(map[string]string)
}
//if _, ok := params["parseTime"]; !ok {
// params["parseTime"] = "true"
//}
//if _, ok := params["loc"]; !ok {
// params["loc"] = "Local"
//}
if _, ok := params["charset"]; !ok {
if c.Charset == "" {
c.Charset = "utf8mb4"
}
params["charset"] = c.Charset
}
optValues := make([]string, 0, len(params))
for k := range params {
optValues = append(optValues, k+"="+params[k])
}
c.Options = strings.Join(optValues, "&")
c.initOpt = true
}
func (c *ConnectMysql) ParserDSN(val string) error {
cfg, err := mysql.ParseDSN(val)
if err != nil {
return err
}
var params map[string]string
u := strings.Split(val, "?")
if len(u) > 1 {
if q, err := url.ParseQuery(u[1]); err == nil {
params = make(map[string]string, len(q))
for k := range q {
params[k] = q.Get(k)
}
}
}
*c = ConnectMysql{}
c.Addr, c.Database, c.UserName, c.Password = cfg.Addr, cfg.DBName, cfg.User, cfg.Passwd
c._setOption(params)
return nil
}
type EntityMysql struct {
MaxOpen int `yaml:"max_open"`
MaxIdle int `yaml:"max_idle"`
Master ConnectMysql `yaml:",inline"`
Slave []ConnectMysql `yaml:"slave"`
}
type ComponentMysql struct {
SlowThreshold int
OpTimeout int
}
// redis
type ConnectRedis struct {
Addr string `yaml:"addr"`
Pwd string `yaml:"password"`
DB int `yaml:"db"`
}
type EntityRedis struct {
ConnectRedis `yaml:",inline"`
PoolSize int `yaml:"poolsize"`
}
type ComponentRedis struct {
IdleCheckTime,
MaxReadTime,
MaxWriteTime,
MaxRequestTime,
SlowThreshold int
}
// mongo
type EntityMongo struct {
Addr string `yaml:"addr"`
User string `yaml:"user"`
Password string `yaml:"password"`
Database string `yaml:"database"`
DSN string `yaml:"dsn"`
}
type ComponentMongo struct {
MinPoolSize,
MaxPoolSize,
ReqMaxTime,
SelectionTime,
MaxIdleTime,
SlowThreshold int
}
// elastic
type EntityEs struct {
Addr string `yaml:"addr"`
User string `yaml:"user"`
Password string `yaml:"password"`
MaxIdleCnt int `yaml:"max_idle"`
}
type ComponentEs struct {
SlowThreshold,
MaxConnTime,
MaxIdleTime,
MaxRepTime,
KeepAliveTime int
}
// cron
type ComponentCron struct {
ChannelBuffer int `json:"channel_buffer" yaml:"channel_buffer"`
ResizeDuration int `json:"resize_duration" yaml:"resize_duration"`
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。