增加WS-Server配置定义

This commit is contained in:
白茶清欢 2021-04-17 19:41:02 +08:00
parent c26624befb
commit 70007b036d
4 changed files with 105 additions and 0 deletions

View File

@ -59,4 +59,11 @@ type IWebsocket interface {
// //
// Date : 6:55 下午 2021/3/27 // Date : 6:55 下午 2021/3/27
GetServerPort() int GetServerPort() int
// GetWSServerConfig 获取WS-Server的配置
//
// Author : go_developer@163.com<张德满>
//
// Date : 7:01 下午 2021/4/17
GetWSServerConfig()
} }

View File

@ -6,3 +6,94 @@
// //
// Date : 2021-04-17 2:32 下午 // Date : 2021-04-17 2:32 下午
package config package config
import (
"go.uber.org/zap/zapcore"
)
const (
// RunModeProduct 生产环境
RunModeProduct = "product"
// RunModeDebug debug环境
RunModeDebug = "debug"
)
const (
// LogSplitIntervalHour 按小时切割日志
LogSplitIntervalHour = "hour"
// LogSplitIntervalDay 按天切割日志
LogSplitIntervalDay = "day"
)
// 定义相关默认值
const (
// DefaultLogEnable 默认关闭日志
DefaultLogEnable = false
// DefaultLogConsole 默认开启控制台输出
DefaultLogConsole = true
// DefaultMode 默认为Debug模式
DefaultMode = RunModeDebug
// DefaultLogLevel 默认的日志级别
DefaultLogLevel = zapcore.DebugLevel
// DefaultLogSplitInterval 默认的日至切割时间
DefaultLogSplitInterval = LogSplitIntervalHour
)
// WSServerConfig WS-Server的配置
//
// Author : go_developer@163.com<张德满>
//
// Date : 7:02 下午 2021/4/17
type WSServerConfig struct {
Mode string // 运行模式
LogEnable bool // 开启日志
LogConsole bool // 开启控制台日志输出
LogPath string // 日志路径
LogLevel zapcore.Level // 日志等级
LogSplitInterval string // 日至切割的时间间隔
}
// SetWSServerConfig 设置WS-Server的配置
//
// Author : go_developer@163.com<张德满>
//
// Date : 7:03 下午 2021/4/17
type SetWSServerConfig func(wsc *WSServerConfig)
// SetWSServerLogEnable 开启日志记录
//
// Author : go_developer@163.com<张德满>
//
// Date : 7:25 下午 2021/4/17
func SetWSServerLogEnable(logPath string, logLevel zapcore.Level, splitInterval string) SetWSServerConfig {
return func(wsc *WSServerConfig) {
if splitInterval != LogSplitIntervalDay && splitInterval != LogSplitIntervalHour {
// 传入非法值,默认按小时切割日志
splitInterval = LogSplitIntervalHour
}
wsc.LogEnable = true
wsc.LogPath = logPath
wsc.LogLevel = logLevel
wsc.LogSplitInterval = splitInterval
}
}
// NewWSServerConfig 生成新的WS-Server配置
//
// Author : go_developer@163.com<张德满>
//
// Date : 7:21 下午 2021/4/17
func NewWSServerConfig(optionList ...SetWSServerConfig) *WSServerConfig {
c := &WSServerConfig{
Mode: DefaultMode,
LogEnable: DefaultLogEnable,
LogConsole: DefaultLogConsole,
LogPath: "",
LogLevel: DefaultLogLevel,
LogSplitInterval: DefaultLogSplitInterval,
}
for _, o := range optionList {
o(c)
}
return c
}

2
go.mod
View File

@ -15,6 +15,8 @@ require (
github.com/pkg/errors v0.9.1 github.com/pkg/errors v0.9.1
github.com/tidwall/gjson v1.7.4 github.com/tidwall/gjson v1.7.4
github.com/ugorji/go v1.2.4 // indirect github.com/ugorji/go v1.2.4 // indirect
go.uber.org/multierr v1.6.0 // indirect
go.uber.org/zap v1.16.0 // indirect
gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376 gopkg.in/olahol/melody.v1 v1.0.0-20170518105555-d52139073376
gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect
) )

5
go.sum
View File

@ -113,8 +113,13 @@ go.opentelemetry.io/otel/metric v0.17.0/go.mod h1:hUz9lH1rNXyEwWAhIWCMFWKhYtpASg
go.opentelemetry.io/otel/oteltest v0.17.0/go.mod h1:JT/LGFxPwpN+nlsTiinSYjdIx3hZIGqHCpChcIZmdoE= go.opentelemetry.io/otel/oteltest v0.17.0/go.mod h1:JT/LGFxPwpN+nlsTiinSYjdIx3hZIGqHCpChcIZmdoE=
go.opentelemetry.io/otel/trace v0.17.0/go.mod h1:bIujpqg6ZL6xUTubIUgziI1jSaUPthmabA/ygf/6Cfg= go.opentelemetry.io/otel/trace v0.17.0/go.mod h1:bIujpqg6ZL6xUTubIUgziI1jSaUPthmabA/ygf/6Cfg=
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ= go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU= go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA= go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
go.uber.org/zap v1.16.0 h1:uFRZXykJGK9lLY4HtgSw44DnIcAM+kRBP7x5m+NpAOM=
go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ= go.uber.org/zap v1.16.0/go.mod h1:MA8QOfq0BHJwdXa996Y4dYkAqRKB8/1K1QMMZVaNZjQ=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=