Merge branch 'develop'

This commit is contained in:
kercylan98 2023-10-08 12:29:20 +08:00
commit e7692a4aff
15 changed files with 298 additions and 4 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

View File

@ -214,6 +214,13 @@ func main() {
}
```
### 基于`xlsx`文件的配置导出工具
该导出器的`xlsx`文件配置使用`JSON`语法进行复杂类型配置,具体可参考图例
- **[`planner/pce/exporter`](planner/pce/exporter)** 是实现了基于`xlsx`文件的配置导出工具,可直接编译成可执行文件使用;
- **[`planner/pce/exporter/xlsx_template.xlsx`](planner/pce/exporter/xlsx_template.xlsx)** 是导出工具的模板文件,其中包含了具体的规则说明。
- 模板文件图例:
![exporter-xlsx-template.png](.github/images/exporter-xlsx-template.png)
### 持续更新的示例项目
- **[Minotaur-Example](https://github.com/kercylan98/minotaur-example)**

3
go.mod
View File

@ -36,6 +36,7 @@ require (
github.com/goccy/go-json v0.10.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jonboulle/clockwork v0.3.0 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/klauspost/compress v1.16.5 // indirect
@ -53,6 +54,8 @@ require (
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/smarty/assertions v1.15.0 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/templexxx/cpu v0.1.0 // indirect
github.com/templexxx/xorsimd v0.4.2 // indirect
github.com/tidwall/match v1.1.1 // indirect

8
go.sum
View File

@ -16,6 +16,7 @@ github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhD
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@ -75,6 +76,8 @@ github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25d
github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/jonboulle/clockwork v0.3.0 h1:9BSCMi8C+0qdApAp4auwX0RkLGUjs956h0EkuQymUhg=
github.com/jonboulle/clockwork v0.3.0/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
@ -145,12 +148,17 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY=
github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec=
github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=
github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60=
github.com/sony/sonyflake v1.2.0 h1:Pfr3A+ejSg+0SPqpoAmQgEtNDAhc2G1SUYk205qVMLQ=
github.com/sony/sonyflake v1.2.0/go.mod h1:LORtCywH/cq10ZbyfhKrHYgAUGH7mOBa76enV9txy/Y=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=

View File

@ -0,0 +1,107 @@
package cmd
import (
"errors"
"github.com/kercylan98/minotaur/planner/pce"
"github.com/kercylan98/minotaur/planner/pce/cs"
"github.com/kercylan98/minotaur/planner/pce/tmpls"
"github.com/kercylan98/minotaur/utils/file"
"github.com/kercylan98/minotaur/utils/hash"
"github.com/kercylan98/minotaur/utils/str"
"github.com/spf13/cobra"
"github.com/tealeg/xlsx"
"os"
"os/exec"
"path/filepath"
"strings"
)
func init() {
var filePath, outPath, exclude string
exportGo := &cobra.Command{
Use: "go",
Short: "Export go language configuration code | 导出 go 语言配置代码",
RunE: func(cmd *cobra.Command, args []string) error {
isDir, err := file.IsDir(outPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
isDir = filepath.Ext(outPath) == ""
} else {
return err
}
}
if isDir {
_ = os.MkdirAll(outPath, os.ModePerm)
outPath = filepath.Join(outPath, "config.go")
} else {
_ = os.MkdirAll(filepath.Dir(outPath), os.ModePerm)
}
fpd, err := file.IsDir(filePath)
if err != nil {
return err
}
var xlsxFiles []string
if fpd {
files, err := os.ReadDir(filePath)
if err != nil {
return err
}
for _, f := range files {
if f.IsDir() || !strings.HasSuffix(f.Name(), ".xlsx") || strings.HasPrefix(f.Name(), "~") {
continue
}
xlsxFiles = append(xlsxFiles, filepath.Join(filePath, f.Name()))
}
} else {
xlsxFiles = append(xlsxFiles, filePath)
}
var golang []*pce.TmplStruct
var exporter = pce.NewExporter()
loader := pce.NewLoader(pce.GetFields())
excludes := hash.ToMapBool(str.SplitTrimSpace(exclude, ","))
for _, xlsxFile := range xlsxFiles {
xf, err := xlsx.OpenFile(xlsxFile)
if err != nil {
return err
}
for _, sheet := range xf.Sheets {
cx := cs.NewXlsx(sheet, cs.XlsxExportTypeServer)
if strings.HasPrefix(cx.GetDisplayName(), "#") || strings.HasPrefix(cx.GetConfigName(), "#") || excludes[cx.GetConfigName()] || excludes[cx.GetDisplayName()] {
continue
}
golang = append(golang, loader.LoadStruct(cx))
}
}
if raw, err := exporter.ExportStruct(tmpls.NewGolang(filepath.Base(filepath.Dir(outPath))), golang...); err != nil {
return err
} else {
if err := file.WriterFile(outPath, raw); err != nil {
return err
}
}
_ = exec.Command("gofmt", "-w", outPath).Run()
return nil
},
}
exportGo.Flags().StringVarP(&filePath, "xlsx", "f", "", "xlsx file path or directory path | xlsx 文件路径或所在目录路径")
exportGo.Flags().StringVarP(&outPath, "output", "o", "", "output path | 输出的 go 文件路径")
exportGo.Flags().StringVarP(&exclude, "exclude", "e", "", "excluded configuration names or display names (comma separated) | 排除的配置名或显示名(英文逗号分隔)")
if err := exportGo.MarkFlagRequired("xlsx"); err != nil {
panic(err)
}
if err := exportGo.MarkFlagRequired("output"); err != nil {
panic(err)
}
rootCmd.AddCommand(exportGo)
}

View File

@ -0,0 +1,114 @@
package cmd
import (
"errors"
"fmt"
"github.com/kercylan98/minotaur/planner/pce"
"github.com/kercylan98/minotaur/planner/pce/cs"
"github.com/kercylan98/minotaur/planner/pce/tmpls"
"github.com/kercylan98/minotaur/utils/file"
"github.com/kercylan98/minotaur/utils/hash"
"github.com/kercylan98/minotaur/utils/str"
"github.com/spf13/cobra"
"github.com/tealeg/xlsx"
"os"
"path/filepath"
"strings"
)
func init() {
var filePath, outPath, exclude, exportType, prefix string
exportJson := &cobra.Command{
Use: "json",
Short: "Export json configuration data | 导出 json 配置数据",
RunE: func(cmd *cobra.Command, args []string) error {
isDir, err := file.IsDir(outPath)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
isDir = filepath.Ext(outPath) == ""
} else {
return err
}
}
if !isDir {
return errors.New("output must be a directory path")
}
_ = os.MkdirAll(outPath, os.ModePerm)
fpd, err := file.IsDir(filePath)
if err != nil {
return err
}
var xlsxFiles []string
if fpd {
files, err := os.ReadDir(filePath)
if err != nil {
return err
}
for _, f := range files {
if f.IsDir() || !strings.HasSuffix(f.Name(), ".xlsx") || strings.HasPrefix(f.Name(), "~") {
continue
}
xlsxFiles = append(xlsxFiles, filepath.Join(filePath, f.Name()))
}
} else {
xlsxFiles = append(xlsxFiles, filePath)
}
var exporter = pce.NewExporter()
loader := pce.NewLoader(pce.GetFields())
excludes := hash.ToMapBool(str.SplitTrimSpace(exclude, ","))
for _, xlsxFile := range xlsxFiles {
xf, err := xlsx.OpenFile(xlsxFile)
if err != nil {
return err
}
for _, sheet := range xf.Sheets {
var cx *cs.Xlsx
switch strings.TrimSpace(strings.ToLower(exportType)) {
case "c":
cx = cs.NewXlsx(sheet, cs.XlsxExportTypeClient)
case "s":
cx = cs.NewXlsx(sheet, cs.XlsxExportTypeServer)
}
if strings.HasPrefix(cx.GetDisplayName(), "#") || strings.HasPrefix(cx.GetConfigName(), "#") || excludes[cx.GetConfigName()] || excludes[cx.GetDisplayName()] {
continue
}
if raw, err := exporter.ExportData(tmpls.NewJSON(), loader.LoadData(cx)); err != nil {
return err
} else {
jsonPath := filepath.Join(outPath, fmt.Sprintf("%s.%s.json", prefix, cx.GetConfigName()))
if err := file.WriterFile(jsonPath, raw); err != nil {
return err
}
}
}
}
return nil
},
}
exportJson.Flags().StringVarP(&filePath, "xlsx", "f", "", "xlsx file path or directory path | xlsx 文件路径或所在目录路径")
exportJson.Flags().StringVarP(&outPath, "output", "o", "", "directory path of the output json file | 输出的 json 文件所在目录路径")
exportJson.Flags().StringVarP(&exportType, "type", "t", "", "export server configuration[s] or client configuration[c] | 导出服务端配置[s]还是客户端配置[c]")
exportJson.Flags().StringVarP(&prefix, "prefix", "p", "", "export configuration file name prefix | 导出配置文件名前缀")
exportJson.Flags().StringVarP(&exclude, "exclude", "e", "", "excluded configuration names or display names (comma separated) | 排除的配置名或显示名(英文逗号分隔)")
if err := exportJson.MarkFlagRequired("xlsx"); err != nil {
panic(err)
}
if err := exportJson.MarkFlagRequired("output"); err != nil {
panic(err)
}
if err := exportJson.MarkFlagRequired("type"); err != nil {
panic(err)
}
rootCmd.AddCommand(exportJson)
}

View File

@ -0,0 +1,23 @@
package cmd
import (
"github.com/spf13/cobra"
"os"
)
// rootCmd 在没有任何子命令的情况下调用时的基本命令
var rootCmd = &cobra.Command{
Use: "exporter",
Short: "An exporter suitable for exporting xlsx configuration templates into go language configuration code and json data files. | 一个适合将 xlsx 配置模板导出为 go 语言配置代码和 json 数据文件的导出器。",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
// Execute 将所有子命令添加到根命令并适当设置标志。这是由 main.main() 调用的。 rootCmd 只需要发生一次
func Execute() {
err := rootCmd.Execute()
if err != nil {
os.Exit(1)
}
}

Binary file not shown.

View File

@ -0,0 +1,7 @@
package main
import "github.com/kercylan98/minotaur/planner/pce/exporter/cmd"
func main() {
cmd.Execute()
}

Binary file not shown.

View File

@ -1,5 +1,6 @@
package cross
// Message 跨服消息数据结构
type Message struct {
ServerId int64 `json:"server_id"`
Packet []byte `json:"packet"`

View File

@ -14,6 +14,7 @@ const (
nasMark = "Cross.Nats"
)
// NewNats 创建一个基于 Nats 实现的跨服消息功能组件
func NewNats(url string, options ...NatsOption) *Nats {
n := &Nats{
url: url,
@ -31,6 +32,7 @@ func NewNats(url string, options ...NatsOption) *Nats {
return n
}
// Nats 基于 Nats 实现的跨服消息功能组件
type Nats struct {
conn *nats.Conn
url string

2
server/gateway/doc.go Normal file
View File

@ -0,0 +1,2 @@
// Package gateway 是用于处理服务器消息的网关模块,适用于对客户端消息进行处理、转发的情况。
package gateway

View File

@ -34,6 +34,14 @@ func NewEndpoint(name string, cli *client.Client, options ...EndpointOption) *En
}
// Endpoint 网关端点
// - 每一个端点均表示了一个目标服务,网关会将数据包转发到该端点,由该端点负责将数据包转发到目标服务。
// - 每个端点会建立一个连接池,默认大小为 DefaultEndpointConnectionPoolSize可通过 WithEndpointConnectionPoolSize 进行设置。
// - 网关在转发数据包时会自行根据延迟维护端点健康值,端点健康值越高,网关越倾向于将数据包转发到该端点。
// - 端点支持连接未中断前始终将数据包转发到特定端点,这样可以保证连接的状态维持。
//
// 连接池:
// - 连接池大小决定了网关服务器与端点服务器建立的连接数,例如当连接池大小为 1 时,那么所有连接到该端点的客户端都会共用一个连接。
// - 连接池的设计可以突破单机理论 65535 个 WebSocket 客户端的限制,适当的增大连接池大小可以提高网关服务器的承载能力。
type Endpoint struct {
gateway *Gateway
client []*client.Client // 端点客户端
@ -46,8 +54,8 @@ type Endpoint struct {
cps int // 端点连接池大小
}
// start 开始与端点建立连接
func (slf *Endpoint) start(gateway *Gateway, cli *client.Client) {
// start 开始与目标服务端点建立连接
func (slf *Endpoint) start(cli *client.Client) {
for {
cur := time.Now().UnixNano()
if err := cli.Run(); err == nil {
@ -76,7 +84,7 @@ func (slf *Endpoint) connect(gateway *Gateway) {
})
cli.RegConnectionClosedEvent(func(conn *client.Client, err any) {
slf.gateway.OnEndpointConnectClosedEvent(slf.gateway, slf)
slf.start(gateway, cli)
slf.start(cli)
})
cli.RegConnectionReceivePacketEvent(func(conn *client.Client, wst int, packet []byte) {
addr, sendTime, packet, err := UnmarshalGatewayInPacket(packet)
@ -93,7 +101,7 @@ func (slf *Endpoint) connect(gateway *Gateway) {
c.SetWST(wst)
slf.gateway.OnEndpointConnectReceivePacketEvent(slf.gateway, slf, c, packet)
})
slf.start(gateway, cli)
slf.start(cli)
leastOnce.Do(least.Done)
}(cli)
}

View File

@ -1,5 +1,7 @@
package str
import "strings"
const (
None = "" // 空字符串
Dunno = "?" // 未知
@ -16,6 +18,16 @@ var (
SlashBytes = []byte("/") // 斜杠
)
// SplitTrimSpace 按照空格分割字符串并去除空格
func SplitTrimSpace(str, sep string) []string {
var strList = strings.Split(str, sep)
var result = make([]string, 0, len(strList))
for _, s := range strList {
result = append(result, strings.TrimSpace(s))
}
return result
}
// FirstUpper 首字母大写
func FirstUpper(str string) string {
var upperStr string