1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package base
- import (
- "github.com/gogf/gf/v2/frame/g"
- "github.com/gogf/gf/v2/net/ghttp"
- "github.com/gogf/gf/v2/os/gctx"
- "github.com/tiger1103/gfast-token/gftoken"
- "golang.org/x/net/context"
- "nodeMonitor/internal/consts"
- "nodeMonitor/internal/model"
- "nodeMonitor/library/liberr"
- "sync"
- )
- type IGfToken interface {
- GenerateToken(ctx context.Context, key string, data interface{}) (keys string, err error)
- RemoveToken(ctx context.Context, token string) (err error)
- GetRequestToken(r *ghttp.Request) (token string)
- Middleware(group *ghttp.RouterGroup) error
- ParseToken(r *ghttp.Request) (*gftoken.CustomClaims, error)
- IsLogin(r *ghttp.Request) (b bool, failed *gftoken.AuthFailed)
- }
- type gfTokenImpl struct {
- *gftoken.GfToken
- }
- var gT = gfTokenImpl{
- GfToken: gftoken.NewGfToken(),
- }
- func NewGfToken(options *model.TokenOptions) IGfToken {
- var fun gftoken.OptionFunc
- if options.CacheModel == consts.CacheModelRedis {
- fun = gftoken.WithGRedis()
- } else {
- fun = gftoken.WithGCache()
- }
- gT.GfToken = gftoken.NewGfToken(
- gftoken.WithCacheKey(options.CacheKey),
- gftoken.WithTimeout(options.Timeout),
- gftoken.WithMaxRefresh(options.MaxRefresh),
- gftoken.WithMultiLogin(options.MultiLogin),
- gftoken.WithExcludePaths(options.ExcludePaths),
- fun,
- )
- return IGfToken(&gT)
- }
- type gft struct {
- options *model.TokenOptions
- gT IGfToken
- lock *sync.Mutex
- }
- var gftService = &gft{
- options: nil,
- gT: nil,
- lock: &sync.Mutex{},
- }
- func GfToken() IGfToken {
- if gftService.gT == nil {
- gftService.lock.Lock()
- defer gftService.lock.Unlock()
- if gftService.gT == nil {
- ctx := gctx.New()
- err := g.Cfg().MustGet(ctx, "gfToken").Struct(&gftService.options)
- liberr.ErrIsNil(ctx, err)
- gftService.gT = NewGfToken(gftService.options)
- }
- }
- return gftService.gT
- }
|