websocket/config/server.go

100 lines
2.6 KiB
Go

// Package config ...
//
// Description : WS-Server 相关配置
//
// Author : go_developer@163.com<张德满>
//
// Date : 2021-04-17 2:32 下午
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
}