初始化项目
This commit is contained in:
40
internal/config/config.go
Normal file
40
internal/config/config.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/zeromicro/go-zero/core/stores/sqlx"
|
||||
"github.com/zeromicro/go-zero/rest"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
rest.RestConf
|
||||
MySql MySqlConf
|
||||
Auth struct {
|
||||
AccessSecret string
|
||||
AccessExpire int64
|
||||
}
|
||||
}
|
||||
|
||||
// MySqlConf mysql配置
|
||||
type MySqlConf struct {
|
||||
Addr string
|
||||
User string
|
||||
Password string
|
||||
Database string
|
||||
Loc string `json:",default=Local"`
|
||||
Log string `json:",default=disableStmt,options=allow|disable|disableStmt"`
|
||||
}
|
||||
|
||||
func (m MySqlConf) Dsn() string {
|
||||
return fmt.Sprintf("%s:%s@tcp(%s)/%s?charset=utf8mb4&parseTime=True&loc=%s", m.User, m.Password, m.Addr, m.Database, url.QueryEscape(m.Loc))
|
||||
}
|
||||
|
||||
func (m MySqlConf) Conn(opts ...sqlx.SqlOption) sqlx.SqlConn {
|
||||
if m.Log == "disable" {
|
||||
sqlx.DisableLog()
|
||||
} else if m.Log == "disableStmt" {
|
||||
sqlx.DisableStmtLog()
|
||||
}
|
||||
return sqlx.NewMysql(m.Dsn(), opts...)
|
||||
}
|
||||
Reference in New Issue
Block a user