websocket/define.go

114 lines
2.7 KiB
Go

// Package websocket ...
//
// Description : 数据结构定义
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2021-04-18 7:53 下午
package websocket
import (
"encoding/json"
"time"
"git.zhangdeman.cn/zhangdeman/util"
"git.zhangdeman.cn/zhangdeman/websocket/context"
"go.uber.org/zap"
)
// buildResponseData 构建响应数据
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:23 下午 2021/4/18
func buildResponseData(wsCtx *context.WSContext, cmd string, data interface{}) []byte {
r := map[string]interface{}{
"connection_id": wsCtx.ConnectionID,
"trace_id": wsCtx.TraceID,
"command": cmd,
"data": data,
}
byteData, _ := json.Marshal(r)
return byteData
}
// getLoadDataList 获取需记录的数据列表
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:00 下午 2021/4/18
func getLoadDataList(wsContext *context.WSContext, fieldList ...zap.Field) []zap.Field {
if nil == wsContext {
wsContext = &context.WSContext{}
}
list := []zap.Field{
zap.String("connection_id", wsContext.ConnectionID),
zap.String("trace_id", wsContext.TraceID),
zap.String("module_flag", wsContext.Flag),
zap.String("server_ip", util.IP.GetHostIP()),
zap.Int64("timestamp", time.Now().UnixNano()),
}
list = append(list, fieldList...)
return list
}
const (
logFuncDebug = "debug"
logFuncInfo = "info"
logFuncWarn = "warn"
logFuncError = "error"
logFuncDPanic = "dpanic"
logFuncPanic = "panic"
logFuncFatal = "fatal"
)
// getWsServer 获取WS-Server实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:54 下午 2021/4/18
func getWsServer(port int, flag string) *Server {
return wsServerTable[port][flag]
}
// getLoggerInstance 获取日志实例
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:48 下午 2021/4/18
func getLoggerInstance(moduleFlag string, cmdAllow *bool) *zap.Logger {
if nil != cmdAllow && !*cmdAllow {
return nil
}
return loggerInstanceTable[moduleFlag]
}
// log 记录日志
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:29 下午 2021/4/18
func log(loggerInstance *zap.Logger, f string, message string, fieldList []zap.Field) {
if nil == loggerInstance {
return
}
switch f {
case logFuncInfo:
loggerInstance.Info(message, fieldList...)
case logFuncWarn:
loggerInstance.Warn(message, fieldList...)
case logFuncError:
loggerInstance.Error(message, fieldList...)
case logFuncDPanic:
loggerInstance.DPanic(message, fieldList...)
case logFuncPanic:
loggerInstance.Panic(message, fieldList...)
case logFuncFatal:
loggerInstance.Fatal(message, fieldList...)
case logFuncDebug:
fallthrough
default:
loggerInstance.Debug(message, fieldList...)
}
}