// Package config ... // // Description : WS-Server 相关配置 // // Author : go_developer@163.com<张德满> // // Date : 2021-04-17 2:32 下午 package config import ( "github.com/go-developer/gopkg/logger" "github.com/go-developer/websocket/storage" "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 = logger.TimeIntervalTypeHour // DefaultStoreConnection 默认存储连接 DefaultStoreConnection = true // DefaultEnablePprof 默认关闭pprof DefaultEnablePprof = false ) // 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 // 日志路径 LogFile string // 日志文件名 LogLevel zapcore.Level // 日志等级 LogSplitInterval logger.TimeIntervalType // 日至切割的时间间隔 StoreConnection bool // 存储连接 ConnectionManager storage.IConnection // 连接管理实例 EnablePprof bool // 开启pprof, 默认关闭 PprofPort int // pprof监听的端口 } // 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, logFile string, logLevel zapcore.Level, splitInterval logger.TimeIntervalType) SetWSServerConfig { return func(wsc *WSServerConfig) { if splitInterval != logger.TimeIntervalTypeMinute && splitInterval != logger.TimeIntervalTypeHour && splitInterval != logger.TimeIntervalTypeDay && splitInterval != logger.TimeIntervalTypeMonth && splitInterval != logger.TimeIntervalTypeYear { // 传入非法值,默认按小时切割日志 splitInterval = DefaultLogSplitInterval } if len(logPath) == 0 { logPath = "./logs" } wsc.LogEnable = true wsc.LogPath = logPath wsc.LogFile = logFile wsc.LogLevel = logLevel wsc.LogSplitInterval = splitInterval } } // 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 } } // DisableStoreConnection 禁用连接存储 // // Author : go_developer@163.com<张德满> // // Date : 11:10 下午 2021/4/17 func DisableStoreConnection() SetWSServerConfig { return func(wsc *WSServerConfig) { wsc.StoreConnection = true } } // 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, StoreConnection: DefaultStoreConnection, ConnectionManager: storage.NewDefaultConnectionManager(), } for _, o := range optionList { o(c) } return c }