package ik3cloud import ( "encoding" "fmt" "ik3cloud/app/config" "ik3cloud/app/lib/logger" "ik3cloud/app/lib/rpcplugin" "io" "path" "reflect" "time" config2 "git.kumo.work/shama/service/config" "github.com/rcrowley/go-metrics" "github.com/shopspring/decimal" "github.com/smallnest/rpcx/server" "github.com/vmihailenco/msgpack/v5" "go.uber.org/zap" "go.uber.org/zap/zapcore" "gopkg.in/natefinch/lumberjack.v2" ) // RegisterBinaryExt registers a binary marshaler/unmarshaler for the given type. // The type must be implemented both interfaces. func RegisterBinaryExt[T encoding.BinaryMarshaler, _ interface { encoding.BinaryUnmarshaler *T }](extID int8) { var zero T msgpack.RegisterExtEncoder(extID, zero, func(e *msgpack.Encoder, v reflect.Value) ([]byte, error) { m := v.Interface().(encoding.BinaryMarshaler) return m.MarshalBinary() }) msgpack.RegisterExtDecoder(extID, zero, func(d *msgpack.Decoder, v reflect.Value, extLen int) error { u := v.Addr().Interface().(encoding.BinaryUnmarshaler) b := make([]byte, extLen) if err := d.ReadFull(b); err != nil { return err } return u.UnmarshalBinary(b) }) } // InitApp 初始化 func InitApp() (closes []func()) { // 关闭资源 closes = []func(){} // 初始化配置文件 config.InitConfig(&config.IniConfig{ ConfigPath: "config", RunModelErrAllow: true, }) var levels []zapcore.Level for _, level := range config.Config.Log.Levels { levels = append(levels, zapcore.Level(level)) } // 初始化日志 logger.InitLogger(&logger.LoggerConfig{ Levels: levels, ShowLine: config.Config.Log.ShowLine, LogInConsole: config.Config.Log.LogInConsole, EncodeLevel: logger.LowercaseColorLevelEncoder, Prefix: config.Config.Log.Prefix, Writer: func(conf *logger.LoggerConfig, filename string, level zapcore.Level) io.Writer { maxAge := config.Config.Log.MaxAge maxSize := config.Config.Log.MaxSize maxBackups := config.Config.Log.MaxBackups if level == zapcore.ErrorLevel { maxAge = 0 maxSize = 0 maxBackups = 0 } return &lumberjack.Logger{ Filename: path.Join(config.Config.Log.Director, filename+".log"), MaxAge: maxAge, // days Compress: true, // disabled by default MaxSize: maxSize, MaxBackups: maxBackups, } }, }) // 初始化rpc服务 config2.InitConfig(config2.Config{ RegistryServer: config.Config.Server.RegistryServer, }) // 注册序列化 RegisterBinaryExt[decimal.Decimal](1) return } // AddRegistryPlugin @Title 注册中心 func AddRegistryPlugin(s *server.Server) { r := &rpcplugin.ConsulRegisterPlugin{ ServiceAddress: fmt.Sprintf("tcp@%s:%d", config.Config.Server.Host, config.Config.Server.Port), ConsulServers: config.Config.Server.RegistryServer, BasePath: config.Config.Server.BasePath, Metrics: metrics.NewRegistry(), UpdateInterval: time.Minute, } err := r.Start() if err != nil { logger.Logger.Fatal("注册中心初始化失败", zap.Error(err)) } s.Plugins.Add(r) }