server支持自定义配置

This commit is contained in:
白茶清欢 2021-04-17 21:41:40 +08:00
parent 70007b036d
commit 563c334757
7 changed files with 88 additions and 16 deletions

2
.gitignore vendored
View File

@ -14,3 +14,5 @@
# Dependency directories (remove the comment below to include it) # Dependency directories (remove the comment below to include it)
# vendor/ # vendor/
.idea .idea
logs
.vscode

View File

@ -8,6 +8,7 @@
package abstract package abstract
import ( import (
"github.com/go-developer/websocket/config"
"github.com/go-developer/websocket/context" "github.com/go-developer/websocket/context"
) )
@ -65,5 +66,5 @@ type IWebsocket interface {
// Author : go_developer@163.com<张德满> // Author : go_developer@163.com<张德满>
// //
// Date : 7:01 下午 2021/4/17 // Date : 7:01 下午 2021/4/17
GetWSServerConfig() GetWSServerConfig() []config.SetWSServerConfig
} }

View File

@ -8,6 +8,7 @@
package config package config
import ( import (
"github.com/go-developer/gopkg/logger"
"go.uber.org/zap/zapcore" "go.uber.org/zap/zapcore"
) )
@ -35,8 +36,8 @@ const (
DefaultMode = RunModeDebug DefaultMode = RunModeDebug
// DefaultLogLevel 默认的日志级别 // DefaultLogLevel 默认的日志级别
DefaultLogLevel = zapcore.DebugLevel DefaultLogLevel = zapcore.DebugLevel
// DefaultLogSplitInterval 默认的日切割时间 // DefaultLogSplitInterval 默认的日切割时间
DefaultLogSplitInterval = LogSplitIntervalHour DefaultLogSplitInterval = logger.TimeIntervalTypeHour
) )
// WSServerConfig WS-Server的配置 // WSServerConfig WS-Server的配置
@ -49,8 +50,9 @@ type WSServerConfig struct {
LogEnable bool // 开启日志 LogEnable bool // 开启日志
LogConsole bool // 开启控制台日志输出 LogConsole bool // 开启控制台日志输出
LogPath string // 日志路径 LogPath string // 日志路径
LogFile string // 日志文件名
LogLevel zapcore.Level // 日志等级 LogLevel zapcore.Level // 日志等级
LogSplitInterval string // 日至切割的时间间隔 LogSplitInterval logger.TimeIntervalType // 日至切割的时间间隔
} }
// SetWSServerConfig 设置WS-Server的配置 // SetWSServerConfig 设置WS-Server的配置
@ -65,14 +67,19 @@ type SetWSServerConfig func(wsc *WSServerConfig)
// Author : go_developer@163.com<张德满> // Author : go_developer@163.com<张德满>
// //
// Date : 7:25 下午 2021/4/17 // Date : 7:25 下午 2021/4/17
func SetWSServerLogEnable(logPath string, logLevel zapcore.Level, splitInterval string) SetWSServerConfig { func SetWSServerLogEnable(logPath string, logFile string, logLevel zapcore.Level, splitInterval logger.TimeIntervalType) SetWSServerConfig {
return func(wsc *WSServerConfig) { return func(wsc *WSServerConfig) {
if splitInterval != LogSplitIntervalDay && splitInterval != LogSplitIntervalHour { if splitInterval != logger.TimeIntervalTypeMinute &&
splitInterval != logger.TimeIntervalTypeHour &&
splitInterval != logger.TimeIntervalTypeDay &&
splitInterval != logger.TimeIntervalTypeMonth &&
splitInterval != logger.TimeIntervalTypeYear {
// 传入非法值,默认按小时切割日志 // 传入非法值,默认按小时切割日志
splitInterval = LogSplitIntervalHour splitInterval = DefaultLogSplitInterval
} }
wsc.LogEnable = true wsc.LogEnable = true
wsc.LogPath = logPath wsc.LogPath = logPath
wsc.LogFile = logFile
wsc.LogLevel = logLevel wsc.LogLevel = logLevel
wsc.LogSplitInterval = splitInterval wsc.LogSplitInterval = splitInterval
} }

View File

@ -11,6 +11,10 @@ import (
"fmt" "fmt"
"sync" "sync"
"github.com/go-developer/gopkg/logger"
"go.uber.org/zap"
"github.com/go-developer/websocket/message" "github.com/go-developer/websocket/message"
"github.com/go-developer/websocket/config" "github.com/go-developer/websocket/config"
@ -36,8 +40,10 @@ import (
// //
// Date : 8:04 下午 2021/3/27 // Date : 8:04 下午 2021/3/27
type Server struct { type Server struct {
ginRouter *gin.Engine ginRouter *gin.Engine // GIN引擎
wsServer *melody.Melody wsServer *melody.Melody // websocket引擎
conf *config.WSServerConfig // 配置
loggerInstance *zap.Logger // 日志实例
} }
var ( var (
@ -69,10 +75,46 @@ func NewWebsocketServe(wsInstanceList ...abstract.IWebsocket) error {
// 初始化ws server // 初始化ws server
_ = lock.Lock() _ = lock.Lock()
if _, exist := ginRouterTable[wsInstance.GetServerPort()]; !exist { if _, exist := ginRouterTable[wsInstance.GetServerPort()]; !exist {
ginRouterTable[wsInstance.GetServerPort()] = &Server{ wsSetConfigList := wsInstance.GetWSServerConfig()
if nil == wsSetConfigList {
wsSetConfigList = make([]config.SetWSServerConfig, 0)
}
s := &Server{
ginRouter: gin.Default(), ginRouter: gin.Default(),
wsServer: melody.New(), wsServer: melody.New(),
conf: config.NewWSServerConfig(wsSetConfigList...),
} }
if s.conf.LogEnable {
// 开启了日志,初始化日志
if len(s.conf.LogPath) == 0 {
panic(wsInstance.GetModuleFlag() + " 模块开启了日志记录,但是没有配置日志路径")
}
var (
err error
logConf *logger.RotateLogConfig
)
optionList := make([]logger.SetLoggerOptionFunc, 0)
if s.conf.LogConsole {
optionList = append(optionList, logger.WithConsoleOutput())
}
if s.conf.LogConsole {
optionList = append(optionList, logger.WithConsoleOutput())
}
if logConf, err = logger.NewRotateLogConfig(s.conf.LogPath, s.conf.LogFile, logger.WithTimeIntervalType(s.conf.LogSplitInterval)); nil != err {
panic(wsInstance.GetModuleFlag() + " 模块开启了日志记录,日志初始化失败, 失败原因 : " + err.Error())
}
if s.loggerInstance, err = logger.NewLogger(s.conf.LogLevel, logConf, optionList...); nil != err {
panic(wsInstance.GetModuleFlag() + " 模块开启了日志记录,日志初始化失败, 失败原因 : " + err.Error())
}
}
if nil == s.loggerInstance && s.conf.LogConsole {
var err error
// 没有配置文件日志, 但是配置了控制台输出
if s.loggerInstance, err = logger.NewConsoleLogger(s.conf.LogLevel); nil != err {
panic(wsInstance.GetModuleFlag() + " 模块开启了控制台日志记录,日志初始化失败, 失败原因 : " + err.Error())
}
}
ginRouterTable[wsInstance.GetServerPort()] = s
} }
if _, exist := commandTable[wsInstance.GetModuleFlag()]; !exist { if _, exist := commandTable[wsInstance.GetModuleFlag()]; !exist {
commandTable[wsInstance.GetModuleFlag()] = make(map[string]abstract.ICommand) commandTable[wsInstance.GetModuleFlag()] = make(map[string]abstract.ICommand)
@ -133,6 +175,13 @@ func NewWebsocketServe(wsInstanceList ...abstract.IWebsocket) error {
}) })
// 注册指令 // 注册指令
for _, cmd := range wsInstance.GetCommandList() { for _, cmd := range wsInstance.GetCommandList() {
if nil != ginRouterTable[wsInstance.GetServerPort()].loggerInstance {
ginRouterTable[wsInstance.GetServerPort()].loggerInstance.Debug(
"长连接指令注册成功",
zap.String("module", wsInstance.GetModuleFlag()),
zap.String("command", cmd.GetCommand()),
)
}
commandTable[wsInstance.GetModuleFlag()][cmd.GetCommand()] = cmd commandTable[wsInstance.GetModuleFlag()][cmd.GetCommand()] = cmd
} }
go func() { go func() {

View File

@ -10,6 +10,9 @@ package main
import ( import (
"fmt" "fmt"
"github.com/go-developer/gopkg/logger"
"go.uber.org/zap/zapcore"
"github.com/go-developer/websocket/config" "github.com/go-developer/websocket/config"
"github.com/go-developer/websocket/message" "github.com/go-developer/websocket/message"
@ -59,6 +62,12 @@ func (e Example) GetServerPort() int {
return 10099 return 10099
} }
func (e Example) GetWSServerConfig() []config.SetWSServerConfig {
return []config.SetWSServerConfig{
config.SetWSServerLogEnable("./logs", e.GetModuleFlag()+".log", zapcore.DebugLevel, logger.TimeIntervalTypeHour),
}
}
type exampleCommand struct { type exampleCommand struct {
} }

2
go.mod
View File

@ -4,7 +4,7 @@ go 1.16
require ( require (
github.com/gin-gonic/gin v1.6.3 github.com/gin-gonic/gin v1.6.3
github.com/go-developer/gopkg v0.0.0-20210409075258-6a35eb1a9d4c github.com/go-developer/gopkg v0.0.0-20210417123142-b08b27daae93
github.com/go-playground/validator/v10 v10.4.1 // indirect github.com/go-playground/validator/v10 v10.4.1 // indirect
github.com/golang/protobuf v1.5.1 // indirect github.com/golang/protobuf v1.5.1 // indirect
github.com/gorilla/websocket v1.4.2 // indirect github.com/gorilla/websocket v1.4.2 // indirect

4
go.sum
View File

@ -13,6 +13,8 @@ github.com/gin-gonic/gin v1.6.3 h1:ahKqKTFpO5KTPHxWZjEdPScmYaGtLo8Y4DMHoEsnp14=
github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M= github.com/gin-gonic/gin v1.6.3/go.mod h1:75u5sXoLsGZoRN5Sgbi1eraJ4GU3++wFwWzhwvtwp4M=
github.com/go-developer/gopkg v0.0.0-20210409075258-6a35eb1a9d4c h1:WotMdy0sk6f9+kMxzqBbdzlZtok7ieiTHnn1eVeqTTE= github.com/go-developer/gopkg v0.0.0-20210409075258-6a35eb1a9d4c h1:WotMdy0sk6f9+kMxzqBbdzlZtok7ieiTHnn1eVeqTTE=
github.com/go-developer/gopkg v0.0.0-20210409075258-6a35eb1a9d4c/go.mod h1:DZcG3JkuXhqmHwZMrSJefYWNPns7nYBhA9q4ocmpG5o= github.com/go-developer/gopkg v0.0.0-20210409075258-6a35eb1a9d4c/go.mod h1:DZcG3JkuXhqmHwZMrSJefYWNPns7nYBhA9q4ocmpG5o=
github.com/go-developer/gopkg v0.0.0-20210417123142-b08b27daae93 h1:05L+6oi0j9w5mR0/61DklCEwWe+7ock6rMncZ9XGvP0=
github.com/go-developer/gopkg v0.0.0-20210417123142-b08b27daae93/go.mod h1:DZcG3JkuXhqmHwZMrSJefYWNPns7nYBhA9q4ocmpG5o=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A= github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q= github.com/go-playground/locales v0.13.0 h1:HyWk6mgj5qFqCT5fjGBuRArbVDfE4hi8+e8ceBS/t7Q=
@ -62,7 +64,9 @@ github.com/leodido/go-urn v1.2.0/go.mod h1:+8+nEpDfqqsY+g338gtMEUOtuK+4dEMhiQEgx
github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w= github.com/leodido/go-urn v1.2.1 h1:BqpAaACuzVSgi/VLzGZIobT2z4v53pjosyNd9Yv6n/w=
github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY= github.com/leodido/go-urn v1.2.1/go.mod h1:zt4jvISO2HfUBqxjfIshjdMTYS56ZS/qv49ictyFfxY=
github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is= github.com/lestrrat-go/envload v0.0.0-20180220234015-a3eb8ddeffcc/go.mod h1:kopuH9ugFRkIXf3YoqHKyrJ9YfUFsckUU9S7B+XP+is=
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkLibYKgg+SwmyFU9dF2hn6MdTj4=
github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA= github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA=
github.com/lestrrat-go/strftime v1.0.4 h1:T1Rb9EPkAhgxKqbcMIPguPq8glqXTA1koF8n9BHElA8=
github.com/lestrrat-go/strftime v1.0.4/go.mod h1:E1nN3pCbtMSu1yjSVeyuRFVm/U0xoR76fd03sz+Qz4g= github.com/lestrrat-go/strftime v1.0.4/go.mod h1:E1nN3pCbtMSu1yjSVeyuRFVm/U0xoR76fd03sz+Qz4g=
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=