代码拉取完成,页面将自动刷新
package gf
import (
"fmt"
"gitee.com/kingsingnal/util/gconv"
"github.com/Unknwon/goconfig"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis/v7"
_ "github.com/go-sql-driver/mysql"
"github.com/jinzhu/gorm"
log "github.com/sirupsen/logrus"
"sync"
"time"
)
const (
DEFAULT = "default"
successCode = 200
)
var gf *Gf
var configOne sync.Once
var contextPool *sync.Pool
var res404 = Response{
Code: "404",
Msg: "页面不存在",
Data: nil,
}
var resSuccess = Response{
Code: "200",
Msg: "错误信息",
Data: CodeMapping,
}
func GetContext(g *gin.Context) *Context {
c := contextPool.Get().(*Context)
c.Context = g
return c
}
func init() {
gf = &Gf{
Gin: gin.New(),
Log: *log.New(),
Config: &goconfig.ConfigFile{},
Db: make(map[string]*gorm.DB),
Redis: make(map[string]*redis.Client),
}
gf.Gin.NoRoute(func(context *gin.Context) {
context.JSON(successCode, res404)
})
gf.Gin.GET("/codes", func(context *gin.Context) {
context.JSON(successCode, resSuccess)
})
contextPool = &sync.Pool{New: func() interface{} {
return &Context{}
}}
}
func InitConfig(path string) {
configOne.Do(func() {
var err error
GetGf().Config, err = goconfig.LoadConfigFile(path)
if err != nil {
panic(fmt.Sprintf("unable to load config file:%s \n", err))
}
ServiceName, _ = gf.Config.GetValue("", "SERVICE_NAME")
gf.Gin.GET("/"+ServiceName+"/code", func(context *gin.Context) {
context.JSON(200, codeMapping{})
})
})
}
func InitDb() {
cf, err := gf.Config.GetValue("", "MYSQL_DSN")
if err == nil {
db, err := gorm.Open("mysql", cf)
if err == nil {
GetGf().Db[DEFAULT] = db
}
}
if err != nil {
log.Error("db init error: %s", err.Error())
}
}
func InitRedis() {
cf, err := GetGf().Config.GetSection("redis")
if err == nil {
c := redis.NewClient(&redis.Options{
Network: "tcp",
Addr: cf["host"] + ":" + cf["port"],
Dialer: nil,
OnConnect: nil,
Password: cf["password"],
DB: gconv.String2Int(cf["db"], 0),
MaxRetries: 3,
DialTimeout: 5 * time.Second,
ReadTimeout: 3 * time.Second,
WriteTimeout: 3 * time.Second,
PoolSize: gconv.String2Int(cf["poolSize"], 20),
MinIdleConns: gconv.String2Int(cf["minIdleConn"], 5),
MaxConnAge: 20,
PoolTimeout: 3 * time.Second,
IdleTimeout: 5 * time.Second,
})
if c.Ping().Err() == nil {
GetGf().Redis[DEFAULT] = c
}
}
if err != nil {
log.Error("redis init error: %s", err.Error())
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。