1 Star 0 Fork 0

kzangv/gsf-fof

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
Clone or Download
config.go 3.52 KB
Copy Edit Raw Blame History
kzangv authored 2023-04-18 13:11 . fixed
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"`
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/kzangv/gsf-fof.git
git@gitee.com:kzangv/gsf-fof.git
kzangv
gsf-fof
gsf-fof
v0.4.3

Search