This commit is contained in:
parent
ae30e2c0de
commit
d705073188
|
|
@ -7,7 +7,7 @@ import (
|
||||||
"myschools.me/suguo/snippet/redis"
|
"myschools.me/suguo/snippet/redis"
|
||||||
)
|
)
|
||||||
|
|
||||||
//从redis中认证用户
|
// 从redis中认证用户
|
||||||
func AuthUser() gin.HandlerFunc {
|
func AuthUser() gin.HandlerFunc {
|
||||||
return func(c *gin.Context) {
|
return func(c *gin.Context) {
|
||||||
token := c.GetHeader("Authorization")
|
token := c.GetHeader("Authorization")
|
||||||
|
|
@ -21,7 +21,7 @@ func AuthUser() gin.HandlerFunc {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//从redis中获取用户信息,最佳实践经验建议把此代码放service层
|
// 从redis中获取用户信息,最佳实践经验建议把此代码放service层
|
||||||
func cacheGet(token *string) interface{} {
|
func cacheGet(token *string) interface{} {
|
||||||
var user interface{}
|
var user interface{}
|
||||||
b, err := redis.GetBytes(token)
|
b, err := redis.GetBytes(token)
|
||||||
|
|
|
||||||
19
gin/gin.go
19
gin/gin.go
|
|
@ -13,15 +13,18 @@ import (
|
||||||
|
|
||||||
func Service(conf *Config) {
|
func Service(conf *Config) {
|
||||||
if conf == nil {
|
if conf == nil {
|
||||||
conf = &Config{
|
conf = &Config{}
|
||||||
RootPath: "/",
|
|
||||||
Addr: "0.0.0.0",
|
|
||||||
Port: 8080,
|
|
||||||
Ssl: false,
|
|
||||||
SslPem: "server.pem",
|
|
||||||
SslKey: "server.key",
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
if conf.RootPath == "" {
|
||||||
|
conf.RootPath = "/"
|
||||||
|
}
|
||||||
|
if conf.Addr == "" {
|
||||||
|
conf.Addr = "0.0.0.0"
|
||||||
|
}
|
||||||
|
if conf.Port == 0 {
|
||||||
|
conf.Port = 8080
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
router := gin.New()
|
router := gin.New()
|
||||||
routerSetup(router, &conf.RootPath)
|
routerSetup(router, &conf.RootPath)
|
||||||
|
|
|
||||||
|
|
@ -6,7 +6,7 @@ import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
//路由配置
|
// 路由配置
|
||||||
func routerSetup(router *gin.Engine, rootpath *string) {
|
func routerSetup(router *gin.Engine, rootpath *string) {
|
||||||
router.Use(gin.Recovery())
|
router.Use(gin.Recovery())
|
||||||
router.GET(`/health/check`)
|
router.GET(`/health/check`)
|
||||||
|
|
@ -18,11 +18,4 @@ func routerSetup(router *gin.Engine, rootpath *string) {
|
||||||
r.POST(`/login`)
|
r.POST(`/login`)
|
||||||
r.POST(`/forgot`)
|
r.POST(`/forgot`)
|
||||||
}
|
}
|
||||||
|
|
||||||
ug := router.Group(`/user`)
|
|
||||||
{
|
|
||||||
ug.GET(`/choose/:orgid`)
|
|
||||||
ug.GET(`/detail`)
|
|
||||||
ug.POST(`/update`)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -28,12 +28,31 @@ type Config struct {
|
||||||
func Init(config *Config) {
|
func Init(config *Config) {
|
||||||
if config == nil {
|
if config == nil {
|
||||||
config = &Config{
|
config = &Config{
|
||||||
ConnString: "root:root@tcp(127.0.0.1:3306)/sample?charset=utf8&parseTime=True&loc=Local",
|
MaxIdleConns: 10,
|
||||||
ConnMaxLifetime: 1,
|
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
|
_conf = config
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -44,7 +63,7 @@ func New() (*gorm.DB, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if _conf == nil {
|
if _conf == nil {
|
||||||
return nil, errors.New("组件未初始化,请执行Init!")
|
return nil, errors.New("组件未初始化,请执行Init!")
|
||||||
}
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue