This commit is contained in:
suguo.yao 2023-09-13 17:27:25 +08:00
parent ae30e2c0de
commit d705073188
4 changed files with 38 additions and 23 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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`)
}
}

View File

@ -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,
}
}
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