增加集群部署支持,修复定时任务分组添加后选项不显示

This commit is contained in:
孟帅
2023-07-26 16:49:09 +08:00
parent 996ed818ee
commit 12bf36cd15
22 changed files with 1185 additions and 529 deletions

View File

@@ -7,12 +7,16 @@ package sys
import (
"context"
"github.com/gogf/gf/v2/database/gredis"
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
"github.com/gogf/gf/v2/os/gtime"
"hotgo/internal/consts"
"hotgo/internal/dao"
"hotgo/internal/global"
"hotgo/internal/library/location"
"hotgo/internal/model/input/sysin"
"hotgo/internal/service"
"hotgo/utility/convert"
@@ -21,10 +25,13 @@ import (
type sSysBlacklist struct {
sync.RWMutex
list map[string]struct{}
}
func NewSysBlacklist() *sSysBlacklist {
return &sSysBlacklist{}
return &sSysBlacklist{
list: make(map[string]struct{}),
}
}
func init() {
@@ -111,6 +118,7 @@ func (s *sSysBlacklist) List(ctx context.Context, in *sysin.BlacklistListInp) (l
func (s *sSysBlacklist) VariableLoad(ctx context.Context, err error) {
if err == nil {
s.Load(ctx)
global.PublishClusterSync(ctx, consts.ClusterSyncSysBlacklist, nil)
}
}
@@ -119,14 +127,14 @@ func (s *sSysBlacklist) Load(ctx context.Context) {
s.RLock()
defer s.RUnlock()
global.Blacklists = make(map[string]struct{})
s.list = make(map[string]struct{})
array, err := dao.SysBlacklist.Ctx(ctx).
Fields(dao.SysBlacklist.Columns().Ip).
Where(dao.SysBlacklist.Columns().Status, consts.StatusEnabled).
Array()
if err != nil {
g.Log().Fatalf(ctx, "load blacklist fail%+v", err)
g.Log().Errorf(ctx, "load blacklist fail%+v", err)
return
}
@@ -134,8 +142,26 @@ func (s *sSysBlacklist) Load(ctx context.Context) {
list := convert.IpFilterStrategy(v.String())
if len(list) > 0 {
for k := range list {
global.Blacklists[k] = struct{}{}
s.list[k] = struct{}{}
}
}
}
}
// VerifyRequest 验证请求的访问IP是否在黑名单如果存在则返回错误
func (s *sSysBlacklist) VerifyRequest(r *ghttp.Request) (err error) {
if len(s.list) == 0 {
return
}
if _, ok := s.list[location.GetClientIp(r)]; ok {
err = gerror.NewCode(gcode.New(gcode.CodeServerBusy.Code(), "请求异常,已被封禁,如有疑问请联系管理员!", nil))
return
}
return
}
// ClusterSync 集群同步
func (s *sSysBlacklist) ClusterSync(ctx context.Context, message *gredis.Message) {
s.Load(ctx)
}

View File

@@ -9,12 +9,14 @@ import (
"context"
"fmt"
"github.com/gogf/gf/v2/database/gdb"
"github.com/gogf/gf/v2/database/gredis"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/util/gconv"
"hotgo/internal/consts"
"hotgo/internal/dao"
"hotgo/internal/global"
"hotgo/internal/library/payment"
"hotgo/internal/library/sms"
"hotgo/internal/library/storager"
@@ -37,37 +39,48 @@ func init() {
service.RegisterSysConfig(NewSysConfig())
}
// InitConfig 初始化一些系统启动就需要用到的配置
// InitConfig 初始化系统配置
func (s *sSysConfig) InitConfig(ctx context.Context) {
if err := s.LoadConfig(ctx); err != nil {
g.Log().Fatalf(ctx, "InitConfig fail%+v", err)
}
}
// LoadConfig 加载系统配置
func (s *sSysConfig) LoadConfig(ctx context.Context) (err error) {
wx, err := s.GetWechat(ctx)
if err != nil {
g.Log().Fatalf(ctx, "init wechat conifg fail%+v", err)
return
}
wechat.SetConfig(wx)
pay, err := s.GetPay(ctx)
if err != nil {
g.Log().Fatalf(ctx, "init pay conifg fail%+v", err)
return
}
payment.SetConfig(pay)
upload, err := s.GetUpload(ctx)
if err != nil {
g.Log().Fatalf(ctx, "init upload conifg fail%+v", err)
return
}
storager.SetConfig(upload)
sm, err := s.GetSms(ctx)
if err != nil {
g.Log().Fatalf(ctx, "init sms conifg fail%+v", err)
return
}
sms.SetConfig(sm)
tk, err := s.GetLoadToken(ctx)
if err != nil {
g.Log().Fatalf(ctx, "init token conifg fail%+v", err)
return
}
token.SetConfig(tk)
// 更多
// ...
return
}
// GetLogin 获取登录配置
@@ -259,9 +272,14 @@ func (s *sSysConfig) UpdateConfigByGroup(ctx context.Context, in *sysin.UpdateCo
return
}
}
return s.syncUpdate(ctx, in)
})
if err != nil {
return
}
global.PublishClusterSync(ctx, consts.ClusterSyncSysconfig, nil)
return
}
@@ -308,3 +326,10 @@ func (s *sSysConfig) syncUpdate(ctx context.Context, in *sysin.UpdateConfigInp)
}
return
}
// ClusterSync 集群同步
func (s *sSysConfig) ClusterSync(ctx context.Context, message *gredis.Message) {
if err := s.LoadConfig(ctx); err != nil {
g.Log().Errorf(ctx, "ClusterSync fail%+v", err)
}
}