From d70507318807bb614e644f2599f4ea38a84b9322 Mon Sep 17 00:00:00 2001 From: "suguo.yao" Date: Wed, 13 Sep 2023 17:27:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gin/auth-filter.go | 4 ++-- gin/gin.go | 19 +++++++++++-------- gin/router.go | 9 +-------- mysql/mysql.go | 29 ++++++++++++++++++++++++----- 4 files changed, 38 insertions(+), 23 deletions(-) diff --git a/gin/auth-filter.go b/gin/auth-filter.go index 6a8458b..ed82453 100644 --- a/gin/auth-filter.go +++ b/gin/auth-filter.go @@ -7,7 +7,7 @@ import ( "myschools.me/suguo/snippet/redis" ) -//从redis中认证用户 +// 从redis中认证用户 func AuthUser() gin.HandlerFunc { return func(c *gin.Context) { token := c.GetHeader("Authorization") @@ -21,7 +21,7 @@ func AuthUser() gin.HandlerFunc { } } -//从redis中获取用户信息,最佳实践经验建议把此代码放service层 +// 从redis中获取用户信息,最佳实践经验建议把此代码放service层 func cacheGet(token *string) interface{} { var user interface{} b, err := redis.GetBytes(token) diff --git a/gin/gin.go b/gin/gin.go index 3534f4b..d195b27 100644 --- a/gin/gin.go +++ b/gin/gin.go @@ -13,15 +13,18 @@ import ( func Service(conf *Config) { if conf == nil { - conf = &Config{ - RootPath: "/", - Addr: "0.0.0.0", - Port: 8080, - Ssl: false, - SslPem: "server.pem", - SslKey: "server.key", - } + conf = &Config{} } + if conf.RootPath == "" { + conf.RootPath = "/" + } + if conf.Addr == "" { + conf.Addr = "0.0.0.0" + } + if conf.Port == 0 { + conf.Port = 8080 + } + go func() { router := gin.New() routerSetup(router, &conf.RootPath) diff --git a/gin/router.go b/gin/router.go index 677bc7b..87174fc 100644 --- a/gin/router.go +++ b/gin/router.go @@ -6,7 +6,7 @@ import ( "github.com/gin-gonic/gin" ) -//路由配置 +// 路由配置 func routerSetup(router *gin.Engine, rootpath *string) { router.Use(gin.Recovery()) router.GET(`/health/check`) @@ -18,11 +18,4 @@ func routerSetup(router *gin.Engine, rootpath *string) { r.POST(`/login`) r.POST(`/forgot`) } - - ug := router.Group(`/user`) - { - ug.GET(`/choose/:orgid`) - ug.GET(`/detail`) - ug.POST(`/update`) - } } diff --git a/mysql/mysql.go b/mysql/mysql.go index d265615..c710112 100644 --- a/mysql/mysql.go +++ b/mysql/mysql.go @@ -28,12 +28,31 @@ type Config struct { func Init(config *Config) { if config == nil { config = &Config{ - ConnString: "root:root@tcp(127.0.0.1:3306)/sample?charset=utf8&parseTime=True&loc=Local", - ConnMaxLifetime: 1, - MaxIdleConns: 10, - MaxOpenConns: 100, + MaxIdleConns: 10, + MaxOpenConns: 100, } } + if config.ConnString == "" { + config.ConnString = "root:root@tcp(127.0.0.1:3306)/mysql?charset=utf8&parseTime=True&loc=Local" + } + if config.ConnMaxLifetime < 1 { + config.ConnMaxLifetime = 1 + } + if config.ConnMaxLifetime > 6 { + config.ConnMaxLifetime = 6 + } + if config.MaxIdleConns < 1 { + config.MaxIdleConns = 1 + } + if config.MaxIdleConns > 50 { + config.MaxIdleConns = 50 + } + if config.MaxOpenConns < 1 { + config.MaxOpenConns = 1 + } + if config.MaxOpenConns > 500 { + config.MaxOpenConns = 500 + } _conf = config } @@ -44,7 +63,7 @@ func New() (*gorm.DB, error) { } if _conf == nil { - return nil, errors.New("组件未初始化,请执行Init!") + return nil, errors.New("组件未初始化,请执行Init!") } var err error