websocket/config/server.go

145 lines
4.1 KiB
Go
Raw Normal View History

// Package config ...
//
// Description : WS-Server 相关配置
//
// Author : go_developer@163.com<张德满>
//
// Date : 2021-04-17 2:32 下午
package config
2021-04-17 19:41:02 +08:00
import (
2021-04-17 21:41:40 +08:00
"github.com/go-developer/gopkg/logger"
2021-04-17 23:34:46 +08:00
"github.com/go-developer/websocket/storage"
2021-04-17 19:41:02 +08:00
"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
2021-04-17 21:41:40 +08:00
// DefaultLogSplitInterval 默认的日志切割时间
DefaultLogSplitInterval = logger.TimeIntervalTypeHour
2021-04-17 23:14:54 +08:00
// DefaultStoreConnection 默认存储连接
DefaultStoreConnection = true
2021-04-19 00:03:25 +08:00
// DefaultEnablePprof 默认关闭pprof
DefaultEnablePprof = false
2021-04-17 19:41:02 +08:00
)
// WSServerConfig WS-Server的配置
//
// Author : go_developer@163.com<张德满>
//
// Date : 7:02 下午 2021/4/17
type WSServerConfig struct {
2021-04-17 23:34:46 +08:00
Mode string // 运行模式
LogEnable bool // 开启日志
LogConsole bool // 开启控制台日志输出
LogPath string // 日志路径
LogFile string // 日志文件名
LogLevel zapcore.Level // 日志等级
LogSplitInterval logger.TimeIntervalType // 日至切割的时间间隔
StoreConnection bool // 存储连接
ConnectionManager storage.IConnection // 连接管理实例
2021-04-19 00:03:25 +08:00
EnablePprof bool // 开启pprof, 默认关闭
PprofPort int // pprof监听的端口
2021-04-17 19:41:02 +08:00
}
// 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
2021-04-17 21:41:40 +08:00
func SetWSServerLogEnable(logPath string, logFile string, logLevel zapcore.Level, splitInterval logger.TimeIntervalType) SetWSServerConfig {
2021-04-17 19:41:02 +08:00
return func(wsc *WSServerConfig) {
2021-04-17 21:41:40 +08:00
if splitInterval != logger.TimeIntervalTypeMinute &&
splitInterval != logger.TimeIntervalTypeHour &&
splitInterval != logger.TimeIntervalTypeDay &&
splitInterval != logger.TimeIntervalTypeMonth &&
splitInterval != logger.TimeIntervalTypeYear {
2021-04-17 19:41:02 +08:00
// 传入非法值,默认按小时切割日志
2021-04-17 21:41:40 +08:00
splitInterval = DefaultLogSplitInterval
2021-04-17 19:41:02 +08:00
}
if len(logPath) == 0 {
logPath = "./logs"
}
2021-04-17 19:41:02 +08:00
wsc.LogEnable = true
wsc.LogPath = logPath
2021-04-17 21:41:40 +08:00
wsc.LogFile = logFile
2021-04-17 19:41:02 +08:00
wsc.LogLevel = logLevel
wsc.LogSplitInterval = splitInterval
}
}
2021-04-19 00:03:25 +08:00
// EnablePprof 开启PProf,由于多模块共享一个进程,任意一个模块开启,就认为是开启
//
// Author : go_developer@163.com<张德满>
//
// Date : 11:24 下午 2021/4/18
func EnablePprof(pprofPort int) SetWSServerConfig {
return func(wsc *WSServerConfig) {
wsc.EnablePprof = true
wsc.PprofPort = pprofPort
}
}
2021-04-17 23:14:54 +08:00
// DisableStoreConnection 禁用连接存储
//
// Author : go_developer@163.com<张德满>
//
// Date : 11:10 下午 2021/4/17
func DisableStoreConnection() SetWSServerConfig {
return func(wsc *WSServerConfig) {
wsc.StoreConnection = true
}
}
2021-04-17 19:41:02 +08:00
// NewWSServerConfig 生成新的WS-Server配置
//
// Author : go_developer@163.com<张德满>
//
// Date : 7:21 下午 2021/4/17
func NewWSServerConfig(optionList ...SetWSServerConfig) *WSServerConfig {
c := &WSServerConfig{
2021-04-17 23:34:46 +08:00
Mode: DefaultMode,
LogEnable: DefaultLogEnable,
LogConsole: DefaultLogConsole,
LogPath: "",
LogLevel: DefaultLogLevel,
LogSplitInterval: DefaultLogSplitInterval,
StoreConnection: DefaultStoreConnection,
ConnectionManager: storage.NewDefaultConnectionManager(),
2021-04-17 19:41:02 +08:00
}
for _, o := range optionList {
o(c)
}
return c
}