Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a62c359fd7 | ||
|
|
b45242dfeb | ||
|
|
a2763676b1 |
133
config/config.go
133
config/config.go
@@ -1,17 +1,11 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/apolloconfig/agollo/v4"
|
||||
apolloConfig "github.com/apolloconfig/agollo/v4/env/config"
|
||||
"github.com/apolloconfig/agollo/v4/storage"
|
||||
"github.com/ayflying/utility_go/pkg"
|
||||
"github.com/gogf/gf/contrib/config/apollo/v2"
|
||||
"github.com/gogf/gf/v2/container/gvar"
|
||||
"github.com/gogf/gf/v2/encoding/gjson"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/os/gfile"
|
||||
"github.com/gogf/gf/v2/os/gres"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -31,137 +25,28 @@ type Cfg struct {
|
||||
Lock sync.Mutex
|
||||
}
|
||||
|
||||
// Deprecated : pkg.Config().GetDbFile(name)
|
||||
func (c *Cfg) GetDbFile(name string) (res *g.Var, err error) {
|
||||
get2, err := g.Model("game_config").
|
||||
Where("name", name).Master().Value("data")
|
||||
err = get2.Scan(&res)
|
||||
if res == nil {
|
||||
res = &gvar.Var{}
|
||||
}
|
||||
pkg.Config().GetDbFile(name)
|
||||
return
|
||||
}
|
||||
|
||||
// Deprecated : pkg.Config().GetFile(name, obj...)
|
||||
func (c *Cfg) GetFile(filename string, obj ...Load) (jsonObj *gjson.Json, err error) {
|
||||
pathStr := "manifest/game/"
|
||||
filePath := pathStr + filename + ".json"
|
||||
//err := gres.Load(pathStr + filename)
|
||||
|
||||
//载入静态资源到文件对象
|
||||
err = gres.Load(filePath)
|
||||
var bytes []byte
|
||||
|
||||
if gfile.IsFile(filePath) {
|
||||
bytes = gfile.GetBytes(filePath)
|
||||
} else {
|
||||
bytes = gres.GetContent(filePath)
|
||||
}
|
||||
|
||||
jsonObj, err = gjson.DecodeToJson(bytes)
|
||||
//g.Dump(filePath, jsonObj)
|
||||
pkg.Config().GetFile(filename)
|
||||
return
|
||||
}
|
||||
|
||||
// getUrlFile 获取远程配置
|
||||
// Deprecated : pkg.Config().GetUrlFile(name)
|
||||
func (c *Cfg) GetUrlFile(name string) (jsonObj *gjson.Json, err error) {
|
||||
urlStr := fmt.Sprintf("http://sdf.sdfs.sdf/%s.json", name)
|
||||
getUrl, err := g.Client().Discovery(nil).Get(nil, urlStr)
|
||||
bytes := getUrl.ReadAll()
|
||||
jsonObj, err = gjson.DecodeToJson(bytes)
|
||||
pkg.Config().GetUrlFile(name)
|
||||
return
|
||||
}
|
||||
|
||||
// 获取阿波罗
|
||||
//func (c *Cfg) GetApollo(name string, obj Load) (jsonObj *gjson.Json, err error) {
|
||||
// jsonObj, err = c.GetApolloV2(name, obj)
|
||||
// return
|
||||
//
|
||||
// //c.Lock.Lock()
|
||||
// //defer c.Lock.Unlock()
|
||||
// //
|
||||
// //Item2Obj[name+".json"] = obj
|
||||
// //var cfg = apolloConfig.AppConfig{
|
||||
// // AppID: ApolloCfg.AppID,
|
||||
// // Cluster: ApolloCfg.Cluster,
|
||||
// // IP: ApolloCfg.IP,
|
||||
// // NamespaceName: name + ".json",
|
||||
// // Secret: ApolloCfg.Secret,
|
||||
// // IsBackupConfig: ApolloCfg.IsBackupConfig,
|
||||
// // BackupConfigPath: ApolloCfg.BackupConfigPath,
|
||||
// // SyncServerTimeout: 60,
|
||||
// // MustStart: true,
|
||||
// //}
|
||||
// ////cfg.NamespaceName = name + ".json"
|
||||
// //
|
||||
// //client, err := agollo.StartWithConfig(func() (*apolloConfig.AppConfig, error) {
|
||||
// // return ApolloCfg, nil
|
||||
// //})
|
||||
// //if client == nil {
|
||||
// // return
|
||||
// //}
|
||||
// //var getStr string
|
||||
// //var getApollo *storage.Config
|
||||
// //for range 5 {
|
||||
// // getApollo = client.GetConfig(cfg.NamespaceName)
|
||||
// // if getApollo != nil {
|
||||
// // break
|
||||
// // }
|
||||
// // time.Sleep(time.Second * 5)
|
||||
// //}
|
||||
// //
|
||||
// //if getApollo != nil {
|
||||
// // getStr = getApollo.GetValue("content")
|
||||
// // if getStr != "" {
|
||||
// // //写入配置
|
||||
// // gfile.PutContents(path.Join("manifest", "game", name+".json"), getStr)
|
||||
// // }
|
||||
// //} else {
|
||||
// // jsonObj, err = c.GetFile(name)
|
||||
// //}
|
||||
// //jsonObj, err = gjson.DecodeToJson(getStr)
|
||||
// ////首次运行加入监听器
|
||||
// //if !gstr.InArray(ApolloListener, name) {
|
||||
// // c2 := &CustomChangeListener{}
|
||||
// // client.AddChangeListener(c2)
|
||||
// // ApolloListener = append(ApolloListener, name)
|
||||
// //}
|
||||
// //return
|
||||
//}
|
||||
|
||||
// Deprecated : pkg.Config().GetApollo(name, obj)
|
||||
func (c *Cfg) GetApollo(name string, obj Load) (jsonObj *gjson.Json, err error) {
|
||||
Item2Obj[name+".json"] = obj
|
||||
|
||||
// 接入阿波罗配置
|
||||
ApolloCfg.NamespaceName = name + ".json"
|
||||
adapter, err := apollo.New(nil, *ApolloCfg)
|
||||
if err != nil {
|
||||
g.Log().Fatalf(nil, `%+v`, err)
|
||||
}
|
||||
// Change the adapter of default configuration instance.
|
||||
g.Cfg(name).SetAdapter(adapter)
|
||||
|
||||
//首次运行加入监听器
|
||||
if !gstr.InArray(ApolloListener, name+".json") {
|
||||
//放置监听器
|
||||
client, _ := agollo.StartWithConfig(func() (*apolloConfig.AppConfig, error) {
|
||||
return &apolloConfig.AppConfig{
|
||||
AppID: ApolloCfg.AppID,
|
||||
Cluster: ApolloCfg.Cluster,
|
||||
NamespaceName: ApolloCfg.NamespaceName,
|
||||
IP: ApolloCfg.IP,
|
||||
IsBackupConfig: ApolloCfg.IsBackupConfig,
|
||||
BackupConfigPath: ApolloCfg.BackupConfigPath,
|
||||
Secret: ApolloCfg.Secret,
|
||||
SyncServerTimeout: ApolloCfg.SyncServerTimeout,
|
||||
MustStart: ApolloCfg.MustStart,
|
||||
}, nil
|
||||
})
|
||||
c2 := &CustomChangeListener{}
|
||||
client.AddChangeListener(c2)
|
||||
ApolloListener = append(ApolloListener, name+".json")
|
||||
}
|
||||
|
||||
cfg, err := g.Cfg(name).Get(nil, "content")
|
||||
cfg.Scan(&jsonObj)
|
||||
pkg.Config().GetApollo(name, obj)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
2
go.mod
2
go.mod
@@ -4,7 +4,7 @@ go 1.24.0
|
||||
|
||||
require (
|
||||
github.com/apolloconfig/agollo/v4 v4.4.0
|
||||
github.com/ayflying/excel2json v1.1.6
|
||||
github.com/ayflying/excel2json v1.1.7
|
||||
github.com/elastic/go-elasticsearch/v8 v8.18.0
|
||||
github.com/go-pay/crypto v0.0.1
|
||||
github.com/go-pay/gopay v1.5.109
|
||||
|
||||
4
go.sum
4
go.sum
@@ -50,8 +50,8 @@ github.com/apolloconfig/agollo/v4 v4.4.0/go.mod h1:6WjI68IzqMk/Y6ghMtrj5AX6Uewo2
|
||||
github.com/armon/circbuf v0.0.0-20150827004946-bbbad097214e/go.mod h1:3U/XgcO3hCbHZ8TKRvWD2dDTCfh9M9ya+I9JpbB7O8o=
|
||||
github.com/armon/go-metrics v0.0.0-20180917152333-f0300d1749da/go.mod h1:Q73ZrmVTwzkszR9V5SSuryQ31EELlFMUz1kKyl939pY=
|
||||
github.com/armon/go-radix v0.0.0-20180808171621-7fddfc383310/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
||||
github.com/ayflying/excel2json v1.1.6 h1:ZzsPM6QP7n2x+E91/+SSr0czSP+5szEmdI/da1EDT9k=
|
||||
github.com/ayflying/excel2json v1.1.6/go.mod h1:GjXYP2cDzeTgQxssQ3ba8QRa9JsScAeJMwCrZ7QTlQo=
|
||||
github.com/ayflying/excel2json v1.1.7 h1:gp4Ls6hNXd1EuT2MEiDQ6yPLaSKIFWkGyaNajMTc59c=
|
||||
github.com/ayflying/excel2json v1.1.7/go.mod h1:GjXYP2cDzeTgQxssQ3ba8QRa9JsScAeJMwCrZ7QTlQo=
|
||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
|
||||
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
|
||||
|
||||
@@ -17,11 +17,11 @@ import (
|
||||
|
||||
var (
|
||||
//ApolloCfg *apolloConfig.AppConfig
|
||||
ApolloCfg *apollo.Config
|
||||
ApolloCfg *apollo.Config
|
||||
// ApolloListener 存储需要监听的 Apollo 配置项名称
|
||||
ApolloListener []string
|
||||
// Item2Obj 存储配置项名称和对应的加载器对象的映射
|
||||
Item2Obj = map[string]Load{}
|
||||
Item2Obj = map[string]Load{}
|
||||
)
|
||||
|
||||
// Load 接口定义了 Load 方法,用于加载数据
|
||||
@@ -86,6 +86,14 @@ func (c *Cfg) GetFile(filename string, _pathStr ...string) (jsonObj *gjson.Json,
|
||||
bytes = gres.GetContent(filePath) // 从打包资源中获取内容
|
||||
}
|
||||
|
||||
//如果还是没有读取到配置,从当前目录返回上级读取
|
||||
if bytes == nil {
|
||||
// 拼接完整的文件路径
|
||||
filePath = "../../" + pathStr + filename + ".json"
|
||||
if gfile.IsFile(filePath) {
|
||||
bytes = gfile.GetBytes(filePath) // 读取物理文件内容
|
||||
}
|
||||
}
|
||||
// 解析 JSON 内容并返回结果
|
||||
jsonObj, err = gjson.DecodeToJson(bytes)
|
||||
return
|
||||
|
||||
@@ -308,8 +308,8 @@ func (s *SocketV1) LeaveGroup(conn *WebsocketData, group string) {
|
||||
}
|
||||
|
||||
//群组广播
|
||||
func (s *SocketV1) SendGroup(cmd int32, group string, req proto.Message) {
|
||||
cacheKey := "websocket:group:" + group
|
||||
func (s *SocketV1) SendGroup(cmd int32, roomId string, req proto.Message) {
|
||||
cacheKey := "websocket:group:" + roomId
|
||||
get, _ := cache.Get(gctx.New(), cacheKey)
|
||||
var list = make(map[int64]string)
|
||||
if !get.IsNil() {
|
||||
|
||||
Reference in New Issue
Block a user