feat 网关鉴权

This commit is contained in:
2025-12-22 18:04:36 +08:00
parent 69cc960fe5
commit 670140e7d3
68 changed files with 1424 additions and 492 deletions

View File

@@ -4,6 +4,7 @@ import (
"common/config"
"common/db/etcd"
"common/db/mysql"
"common/db/redis"
"common/log"
)
@@ -24,6 +25,12 @@ func (p *ModuleDB) Init(cfg *config.DBConfig) error {
return err
}
}
// REDIS
if cfg.Redis != nil {
if err := redis.Init(cfg.Redis); err != nil {
return err
}
}
return nil
}
@@ -34,5 +41,8 @@ func (p *ModuleDB) Stop() error {
if err := mysql.Close(); err != nil {
log.Errorf("close mysql failed: %v", err)
}
if err := redis.Close(); err != nil {
log.Errorf("close redis failed: %v", err)
}
return nil
}

View File

@@ -4,7 +4,6 @@ import (
"common/config"
"context"
"go.etcd.io/etcd/client/v3"
"go.uber.org/zap"
"time"
)
@@ -12,7 +11,6 @@ var instance *Client
type Client struct {
cli *clientv3.Client
log *zap.Logger
}
// Init 初始化

View File

@@ -0,0 +1,50 @@
package redis
import (
"context"
"encoding/json"
"github.com/panjf2000/gnet/v2/pkg/logging"
"github.com/redis/go-redis/v9"
"time"
)
var cacheInstance *CacheClient
type CacheClient struct {
cli *redis.Client
logger logging.Logger
}
func GetCacheClient() *CacheClient {
return cacheInstance
}
func (c *CacheClient) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) {
bytes, err := json.Marshal(value)
if err != nil {
c.logger.Errorf("Set json.Marshal error: %v, key: %v, value: %v", err, key, value)
return
}
if err = c.cli.Set(ctx, key, bytes, expiration).Err(); err != nil {
c.logger.Errorf("Set redis.Set error: %v, key: %v, value: %v", err, key, value)
}
}
func (c *CacheClient) Del(ctx context.Context, keys ...string) {
if err := c.cli.Del(ctx, keys...).Err(); err != nil {
c.logger.Errorf("Set redis.Del error: %v, keys: %v", err, keys)
}
}
// Get 获取数据
func (c *CacheClient) Get(ctx context.Context, key string, dst interface{}) bool {
data, err := c.cli.Get(ctx, key).Bytes()
if err != nil {
return false
}
if err = json.Unmarshal(data, dst); err != nil {
c.logger.Errorf("Get json.Unmarshal error: %v, key: %v", err, key)
return false
}
return true
}

View File

@@ -1,34 +1,53 @@
package redis
import (
"common/config"
"common/log"
"context"
"fmt"
"github.com/redis/go-redis/v9"
"time"
)
var (
var instance *Client
type Client struct {
cli *redis.Client
Nil = redis.Nil
)
}
func Init(host string, port int, password string, db int) error {
cli = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%d", host, port),
Password: password,
DB: db,
func Init(cfg *config.RedisConfig) error {
client := redis.NewClient(&redis.Options{
Addr: cfg.Addr,
Password: cfg.Password,
DB: cfg.DB,
})
instance = &Client{
cli: client,
}
cacheInstance = &CacheClient{
cli: client,
logger: log.GetLogger().Named("CACHE"),
}
_, err := cli.Ping(context.Background()).Result()
_, err := client.Ping(context.Background()).Result()
return err
}
func Client() *redis.Client {
return cli
}
func Close() error {
if cli != nil {
return cli.Close()
if instance != nil && instance.cli != nil {
return instance.cli.Close()
}
return nil
}
func GetClient() *Client {
return instance
}
func (c *Client) Set(ctx context.Context, key string, value interface{}, expiration time.Duration) *redis.StatusCmd {
return c.cli.Set(ctx, key, value, expiration)
}
// Get 获取数据
func (c *Client) Get(ctx context.Context, key string) *redis.StringCmd {
return c.cli.Get(ctx, key)
}