Browse Source

更新包

luoyangwei 1 year ago
parent
commit
0ce3266dd6

+ 1 - 1
config/config.go

@@ -4,7 +4,7 @@ import (
 	"log"
 
 	"github.com/spf13/viper"
-	"x.sikey.com.cn/serverx/confx"
+	"sikey.com/websocket/pkg/confx"
 )
 
 var Websocket websocket

+ 7 - 10
go.mod

@@ -1,27 +1,30 @@
 module sikey.com/websocket
 
-go 1.21.4
+go 1.22.1
 
 require (
 	firebase.google.com/go v3.13.0+incompatible
 	github.com/DeanThompson/ginpprof v0.0.0-20201112072838-007b1e56b2e1
+	github.com/bwmarrin/snowflake v0.3.0
 	github.com/denisbrodbeck/machineid v1.0.1
 	github.com/gin-gonic/gin v1.9.1
+	github.com/go-sql-driver/mysql v1.7.1
 	github.com/golang-jwt/jwt/v5 v5.2.0
 	github.com/google/uuid v1.4.0
 	github.com/gorilla/websocket v1.5.1
+	github.com/joho/godotenv v1.5.1
 	github.com/mitchellh/mapstructure v1.5.0
 	github.com/nats-io/nats.go v1.33.1
 	github.com/redis/go-redis/v9 v9.5.1
 	github.com/rotisserie/eris v0.5.4
 	github.com/spf13/viper v1.18.2
 	go.uber.org/zap v1.27.0
+	golang.org/x/crypto v0.19.0
+	gopkg.in/natefinch/lumberjack.v2 v2.2.1
+	gorm.io/driver/mysql v1.5.4
 	gorm.io/gorm v1.25.7
-	x.sikey.com.cn/serverx v1.2.32
 )
 
-// replace x.sikey.com.cn/serverx v1.2.26 => ../serverx
-
 require (
 	cloud.google.com/go v0.110.10 // indirect
 	cloud.google.com/go/compute v1.23.3 // indirect
@@ -30,7 +33,6 @@ require (
 	cloud.google.com/go/iam v1.1.5 // indirect
 	cloud.google.com/go/longrunning v0.5.4 // indirect
 	cloud.google.com/go/storage v1.35.1 // indirect
-	github.com/bwmarrin/snowflake v0.3.0 // indirect
 	github.com/bytedance/sonic v1.10.2 // indirect
 	github.com/cespare/xxhash/v2 v2.2.0 // indirect
 	github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect
@@ -42,7 +44,6 @@ require (
 	github.com/go-playground/locales v0.14.1 // indirect
 	github.com/go-playground/universal-translator v0.18.1 // indirect
 	github.com/go-playground/validator/v10 v10.17.0 // indirect
-	github.com/go-sql-driver/mysql v1.7.1 // indirect
 	github.com/goccy/go-json v0.10.2 // indirect
 	github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
 	github.com/golang/protobuf v1.5.3 // indirect
@@ -52,7 +53,6 @@ require (
 	github.com/hashicorp/hcl v1.0.0 // indirect
 	github.com/jinzhu/inflection v1.0.0 // indirect
 	github.com/jinzhu/now v1.1.5 // indirect
-	github.com/joho/godotenv v1.5.1 // indirect
 	github.com/json-iterator/go v1.1.12 // indirect
 	github.com/klauspost/compress v1.17.2 // indirect
 	github.com/klauspost/cpuid/v2 v2.2.6 // indirect
@@ -76,7 +76,6 @@ require (
 	go.opencensus.io v0.24.0 // indirect
 	go.uber.org/multierr v1.11.0 // indirect
 	golang.org/x/arch v0.7.0 // indirect
-	golang.org/x/crypto v0.19.0 // indirect
 	golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
 	golang.org/x/net v0.21.0 // indirect
 	golang.org/x/oauth2 v0.15.0 // indirect
@@ -93,7 +92,5 @@ require (
 	google.golang.org/grpc v1.59.0 // indirect
 	google.golang.org/protobuf v1.32.0 // indirect
 	gopkg.in/ini.v1 v1.67.0 // indirect
-	gopkg.in/natefinch/lumberjack.v2 v2.2.1 // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect
-	gorm.io/driver/mysql v1.5.4 // indirect
 )

+ 2 - 4
go.sum

@@ -299,8 +299,8 @@ google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
 google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
 google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
 gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
-gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
-gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
+gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
+gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
 gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
 gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
 gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
@@ -317,5 +317,3 @@ honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWh
 honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
 nullprogram.com/x/optparse v1.0.0/go.mod h1:KdyPE+Igbe0jQUrVfMqDMeJQIJZEuyV7pjYmp6pbG50=
 rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
-x.sikey.com.cn/serverx v1.2.32 h1:bqvD46TF19GY2FnO5IRdVLsF8fjAtuWC72Uai/Xp004=
-x.sikey.com.cn/serverx v1.2.32/go.mod h1:+jho37jloxhYP66RsEKw4Qc+bcoInP/zWP4/Xhx18GM=

+ 10 - 0
pkg/algorit/md5.go

@@ -0,0 +1,10 @@
+package algorit
+
+import (
+	"crypto/md5"
+	"fmt"
+)
+
+func Md5(orig string) string {
+	return fmt.Sprintf("%x", md5.Sum([]byte(orig)))
+}

+ 10 - 0
pkg/algorit/md5_test.go

@@ -0,0 +1,10 @@
+package algorit
+
+import (
+	"fmt"
+	"testing"
+)
+
+func TestMd5(t *testing.T) {
+	fmt.Println(Md5("123"))
+}

+ 84 - 0
pkg/confx/config.go

@@ -0,0 +1,84 @@
+package confx
+
+import (
+	"log"
+	"os"
+	"strings"
+
+	_ "github.com/joho/godotenv/autoload"
+	"github.com/rotisserie/eris"
+	"github.com/spf13/viper"
+	"sikey.com/websocket/pkg/dbx"
+	"sikey.com/websocket/pkg/logx"
+	"sikey.com/websocket/pkg/natx"
+	"sikey.com/websocket/pkg/rdbx"
+)
+
+// defaultConfigFileType 默认的配置文件格式
+const defaultConfigFileType = "yaml"
+
+const (
+	nodeId = "NODE_ID"
+)
+
+type LoadOption func() error
+
+// SetEnvConfig 将环境变量设置到默认值
+func SetEnvConfig(inp ...string) {
+	for _, in := range inp {
+		viper.SetDefault(in, os.Getenv(in))
+		log.Printf("Default env: %s, value: %s\n", in, viper.GetString(in))
+	}
+}
+
+// LoadConfig 加载配置
+func LoadConfig(file string, opts ...LoadOption) error {
+	viper.SetConfigFile(file)
+	viper.SetConfigType(defaultConfigFileType)
+	if err := viper.ReadInConfig(); err != nil {
+		panic(eris.Wrap(err, "无法加载配置"))
+	}
+
+	var err error
+	for _, opt := range opts {
+		if err = opt(); err != nil {
+			return err
+		}
+	}
+
+	if containsKey(logx.KeyPrefix) {
+		logx.NewLogger()
+	}
+	if containsKey(dbx.KeyPrefix) {
+		dbx.Connect()
+	}
+	if containsKey(dbx.KeyPrefix) {
+		rdbx.Connect()
+	}
+	if containsKey(natx.KeyPrefix) {
+		natx.Connect()
+	}
+	return nil
+}
+
+func GetServerPort() int {
+	return viper.GetInt("port")
+}
+
+func GetServerName() string {
+	return viper.GetString("name")
+}
+
+func containsKey(k string) bool {
+	var keys = viper.AllKeys()
+	for _, key := range keys {
+		var prefix = key
+		if strings.Contains(key, ".") {
+			prefix = strings.Split(key, ".")[0]
+		}
+		if prefix == k {
+			return true
+		}
+	}
+	return false
+}

+ 7 - 0
pkg/confx/config_test.go

@@ -0,0 +1,7 @@
+package confx
+
+import "testing"
+
+func TestLoad(t *testing.T) {
+	LoadConfig("./websocket.toml")
+}

+ 163 - 0
pkg/dbx/mysqlx.go

@@ -0,0 +1,163 @@
+package dbx
+
+import (
+	"context"
+	"log"
+	"net"
+	"os"
+	"strconv"
+	"strings"
+	"time"
+
+	"github.com/go-sql-driver/mysql"
+	_ "github.com/go-sql-driver/mysql"
+	"github.com/spf13/viper"
+	"golang.org/x/crypto/ssh"
+	sql "gorm.io/driver/mysql"
+	"gorm.io/gorm"
+	"gorm.io/gorm/logger"
+)
+
+const KeyPrefix = "mysql"
+
+var dbConn *gorm.DB
+
+type mysqlConfig struct {
+	// 	ssh
+	// user
+	// password
+	// hostname
+	SSH                       bool          `mapstructure:"ssh"` // SSH 是否开启SSH
+	SSHHostname               string        `mapstructure:"hostname"`
+	SSHUser                   string        `mapstructure:"user"`
+	SSHPassword               string        `mapstructure:"password"`
+	Dsn                       string        `mapstructure:"dsn"`                           // Dsn 数据源地址
+	SkipDefaultTransaction    bool          `mapstructure:"skip_default_transaction"`      // SkipDefaultTransaction 跳过默认事务
+	SlowThreshold             time.Duration `mapstructure:"slow_threshold"`                // SlowThreshold 慢 SQL 阈值
+	IgnoreRecordNotFoundError bool          `mapstructure:"ignore_record_not_found_error"` // IgnoreRecordNotFoundError 忽略记录未找到的错误
+	MaxLifetime               time.Duration `mapstructure:"max_lifetime"`                  // MaxLifetime 连接的有效时长
+	MaxOpenConns              int           `mapstructure:"max_open_conns"`                // MaxOpenConns 打开数据库连接的最大数量。
+	MaxIdleConns              int           `mapstructure:"max_idle_conns"`                // MaxIdleConns 空闲连接池中连接的最大数量
+}
+
+type driverConfig struct {
+	username string
+	password string
+	protocol string
+	address  string
+	port     int
+	db       string
+	params   string
+}
+
+func (vc *driverConfig) formatDSN() string {
+	return vc.username + ":" + vc.password + "@" +
+		vc.protocol + "(" + vc.address + ":" + strconv.Itoa(vc.port) + ")/" + vc.db + "?" + vc.params
+}
+
+type Dialer struct {
+	client *ssh.Client
+}
+
+func (v *Dialer) Dial(ctx context.Context, address string) (net.Conn, error) {
+	return v.client.Dial("tcp", address)
+}
+
+// Connect 初始化 mysql 连接
+func Connect() *gorm.DB {
+	cfg := readMysqlConfig()
+	conn, err := gorm.Open(sql.New(sql.Config{
+		DSN:                       cfg.Dsn,
+		DefaultStringSize:         255,
+		SkipInitializeWithVersion: false,
+	}), getGormConfig(cfg))
+	if err != nil {
+		log.Panicln(err)
+	}
+
+	sqlDB, _ := conn.DB()
+	// SetMaxIdleConns 设置空闲连接池中连接的最大数量
+	sqlDB.SetMaxIdleConns(cfg.MaxIdleConns)
+	// SetMaxOpenConns 设置打开数据库连接的最大数量。
+	sqlDB.SetMaxOpenConns(cfg.MaxOpenConns)
+	// SetConnMaxLifetime 设置了连接可复用的最大时间。
+	sqlDB.SetConnMaxLifetime(cfg.MaxLifetime)
+
+	dbConn = conn
+	log.Printf("Mysql connected to %s \n", cfg.Dsn)
+	return conn
+}
+
+func GetConnect() *gorm.DB {
+	return dbConn
+}
+
+func withDsn(dsn string) *driverConfig {
+	// root:qq123123@tcp(127.0.0.1:3306)/sikey?charset=utf8mb4&parseTime=true&loc=Local
+	var user = strings.Split(dsn, "@")
+	var ua = strings.Split(user[0], ":")
+	var protocol = strings.Split(user[1], "(")
+	var address = strings.Split(protocol[1], ")")
+	var addr = strings.Split(address[0], ":")
+	var port, err = strconv.ParseInt(addr[1], 10, 64)
+	if err != nil {
+		port = 3306
+	}
+	return &driverConfig{
+		username: ua[0],
+		password: ua[1],
+		protocol: protocol[0],
+		address:  addr[0],
+		port:     int(port),
+		db:       strings.Split(address[1], "?")[0][1:],
+		params:   strings.Split(address[1], "?")[1],
+	}
+}
+
+// readMysqlConfig 加载配置
+func readMysqlConfig() mysqlConfig {
+	var cfg mysqlConfig
+	if err := viper.UnmarshalKey("mysql", &cfg); err != nil {
+		log.Fatalln(err)
+	}
+
+	if cfg.SSH {
+		config := &ssh.ClientConfig{
+			User: cfg.SSHUser,
+			Auth: []ssh.AuthMethod{
+				ssh.Password(cfg.SSHPassword),
+			},
+			HostKeyCallback: ssh.InsecureIgnoreHostKey(),
+		}
+		var err error
+		var clt *ssh.Client
+		if clt, err = ssh.Dial("tcp", cfg.SSHHostname, config); err != nil {
+			log.Fatalln(err)
+		}
+
+		var protocol = "ssh"
+		vc := withDsn(cfg.Dsn)
+		vc.protocol = protocol
+		cfg.Dsn = vc.formatDSN()
+		mysql.RegisterDialContext(protocol, (&Dialer{client: clt}).Dial)
+	}
+	return cfg
+}
+
+// getGormConfig 获取 gorm 配置
+func getGormConfig(cfg mysqlConfig) *gorm.Config {
+	return &gorm.Config{
+		DisableForeignKeyConstraintWhenMigrating: true,
+		SkipDefaultTransaction:                   cfg.SkipDefaultTransaction,
+		Logger:                                   defaultLogger(cfg),
+	}
+}
+
+// defaultLogger 默认的日志打印
+func defaultLogger(cfg mysqlConfig) logger.Interface {
+	return logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{
+		SlowThreshold:             cfg.SlowThreshold * time.Millisecond, // Slow SQL threshold
+		LogLevel:                  logger.Silent,                        // Log level
+		IgnoreRecordNotFoundError: cfg.IgnoreRecordNotFoundError,        // Ignore ErrRecordNotFound error for logger
+	})
+}

+ 29 - 0
pkg/filex/filex.go

@@ -0,0 +1,29 @@
+package filex
+
+import "os"
+
+// Exists 判断所给路径文件/文件夹是否存在
+func Exists(path string) bool {
+	_, err := os.Stat(path) //os.Stat获取文件信息
+	if err != nil {
+		if os.IsExist(err) {
+			return true
+		}
+		return false
+	}
+	return true
+}
+
+// IsDir 判断所给路径是否为文件夹
+func IsDir(path string) bool {
+	s, err := os.Stat(path)
+	if err != nil {
+		return false
+	}
+	return s.IsDir()
+}
+
+// IsFile 判断所给路径是否为文件
+func IsFile(path string) bool {
+	return !IsDir(path)
+}

+ 44 - 0
pkg/format/date.go

@@ -0,0 +1,44 @@
+package format
+
+import (
+	"errors"
+	"strconv"
+	"strings"
+	"time"
+)
+
+const (
+	WordStartDate = "1983-01-01"
+)
+
+const (
+	DateParseAllFormat                  = "2006-01-02 15:04:05"
+	DateParseAllUnixMilliFormat         = "2006-01-02 15:04:05.000000"
+	UnsignedDateParseAllUnixMilliFormat = "20060102150405.000000"
+	DateParseLeftFormat                 = "2006-01-02"
+	DateParseRightFormat                = "15:04:05"
+
+	DateParseRightMinuteFormat = "15:04"
+)
+
+// LoadLocation loads location by timezone
+// tz is timezone, use UTC+8 time zone format.
+func LoadLocation(tz string) (*time.Location, error) {
+	if tz == "" {
+		return nil, errors.New("timezone is empty")
+	}
+
+	var zone *time.Location
+	var offset int
+	var sp []string
+	if strings.Contains(tz, "+") {
+		sp = strings.Split(tz, "+")
+		offset, _ = strconv.Atoi(sp[1])
+		zone = time.FixedZone(sp[0], +offset*3600)
+	} else {
+		sp = strings.Split(tz, "-")
+		offset, _ = strconv.Atoi(sp[1])
+		zone = time.FixedZone(sp[0], -offset*3600)
+	}
+	return zone, nil
+}

+ 12 - 0
pkg/format/date_test.go

@@ -0,0 +1,12 @@
+package format
+
+import (
+	"fmt"
+	"testing"
+	"time"
+)
+
+func TestLoadLocation(t *testing.T) {
+	location, _ := LoadLocation("UTC-8")
+	fmt.Println(time.Now().In(location).Format(DateParseAllFormat))
+}

+ 7 - 0
pkg/format/imei.go

@@ -0,0 +1,7 @@
+package format
+
+import "strings"
+
+func IMEI(mac string) string {
+	return strings.Replace(mac, ":", "", -1)
+}

+ 12 - 0
pkg/format/number.go

@@ -0,0 +1,12 @@
+package format
+
+import (
+	"fmt"
+	"strconv"
+)
+
+// ToFixedWithTwoDigits 保留两位小数
+func ToFixedWithTwoDigits(value float64) float64 {
+	value, _ = strconv.ParseFloat(fmt.Sprintf("%.2f", value), 64)
+	return value
+}

+ 25 - 0
pkg/gid/gid.go

@@ -0,0 +1,25 @@
+package gid
+
+import (
+	"log"
+
+	"github.com/bwmarrin/snowflake"
+)
+
+var node *snowflake.Node
+
+func SetNodeId(n int64) {
+	var err error
+	node, err = snowflake.NewNode(n)
+	if err != nil {
+		log.Fatalln(err)
+	}
+
+	// First test generation
+	log.Printf("Node: %d, First test generation %s.\n", n, node.Generate().String())
+}
+
+// GetSnowflakeId 生成雪花ID
+func GetSnowflakeId() string {
+	return node.Generate().String()
+}

+ 42 - 0
pkg/logx/logx.go

@@ -0,0 +1,42 @@
+package logx
+
+import (
+	"os"
+
+	"github.com/spf13/viper"
+	"go.uber.org/zap"
+	"go.uber.org/zap/zapcore"
+	"gopkg.in/natefinch/lumberjack.v2"
+)
+
+const KeyPrefix = "logger"
+
+func NewLogger() {
+	var encoderConfig = zap.NewProductionEncoderConfig()
+	encoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
+
+	var encoder zapcore.Encoder
+	if viper.GetString("ENVIRONMENT") != "debug" {
+		encoder = zapcore.NewJSONEncoder(encoderConfig)
+	} else {
+		encoder = zapcore.NewConsoleEncoder(encoderConfig)
+	}
+
+	var filename = viper.GetString("logger.filename")
+	var maxSize = viper.GetInt("logger.max_size")
+	var maxBackups = viper.GetInt("logger.max_backups")
+	var maxAge = viper.GetInt("logger.max_age")
+	var compress = viper.GetBool("logger.compress")
+
+	lumberjack := zapcore.AddSync(&lumberjack.Logger{
+		Filename:   filename,   // 日志文件路径
+		MaxSize:    maxSize,    // 每个日志文件的最大大小(以MB为单位)
+		MaxBackups: maxBackups, // 保留的旧日志文件的最大数量
+		MaxAge:     maxAge,     // 最多保留的天数
+		Compress:   compress,   // 是否压缩旧日志文件
+	})
+
+	multiWriteSyncer := zapcore.NewMultiWriteSyncer(zapcore.AddSync(os.Stdout), lumberjack)
+	core := zapcore.NewCore(encoder, multiWriteSyncer, zap.NewAtomicLevelAt(zap.InfoLevel))
+	zap.ReplaceGlobals(zap.New(core))
+}

+ 32 - 0
pkg/natx/nats.go

@@ -0,0 +1,32 @@
+package natx
+
+import (
+	"log"
+
+	"github.com/nats-io/nats.go"
+	"github.com/rotisserie/eris"
+	"github.com/spf13/viper"
+)
+
+const KeyPrefix = "nats"
+
+var natsConn *nats.Conn
+
+func Connect() *nats.Conn {
+	addr := viper.GetString("nats.addr")
+	conn, err := nats.Connect(addr)
+	if err != nil {
+		log.Fatalln(eris.Wrap(err, "nats connect error"))
+	}
+	natsConn = conn
+	log.Printf("Nats connected to %s \n", addr)
+	return conn
+}
+
+func GetConnect() *nats.Conn {
+	return natsConn
+}
+
+func GetSubject() string {
+	return viper.GetString("nats.subject")
+}

+ 57 - 0
pkg/rdbx/redisx.go

@@ -0,0 +1,57 @@
+package rdbx
+
+import (
+	"context"
+	"log"
+
+	"github.com/redis/go-redis/v9"
+	"github.com/spf13/viper"
+)
+
+const KeyPrefix = "redis"
+
+var rdbConn *redis.Client
+
+type redisConfig struct {
+	Addr     string `yaml:"addr"`     // Addr 链接地址
+	DB       int    `yaml:"db"`       // DB 数据库, 一般默认是0
+	Password string `yaml:"password"` // Password 密码
+
+	// Channel 来控制订阅频道, 连接发送消息会发送到频道
+	// Channel string `yaml:"channel"` // Channel 订阅频道
+	// ConnectKey 连接 key, websocket connect 创建时会在 redis 里保存信息,
+	// 这里的 ConnectKey 相当于前缀保存在 redis 里的 key
+	ConnectKey string `yaml:"connect_key"`
+}
+
+func Connect() *redis.Client {
+	cfg := readRedisConfig()
+	addr := cfg.Addr
+	rdb := redis.NewClient(&redis.Options{
+		Addr:     addr,
+		Password: cfg.Password,
+		DB:       cfg.DB,
+	})
+	if err := rdb.Ping(context.Background()).Err(); err != nil {
+		log.Fatalln(err)
+	}
+	rdbConn = rdb
+	log.Printf("Redis connected to %s \n", addr)
+	return rdb
+}
+
+func GetConnect() *redis.Client {
+	return rdbConn
+}
+
+func GetRedisConfig() redisConfig {
+	return readRedisConfig()
+}
+
+func readRedisConfig() redisConfig {
+	var cfg redisConfig
+	if err := viper.UnmarshalKey("redis", &cfg); err != nil {
+		log.Fatalln(err)
+	}
+	return cfg
+}

+ 2 - 2
repositories/online_repostiroy_test.go

@@ -6,8 +6,8 @@ import (
 
 	"go.uber.org/zap"
 	"sikey.com/websocket/config"
-	"x.sikey.com.cn/serverx/dbx"
-	"x.sikey.com.cn/serverx/rdbx"
+	"sikey.com/websocket/pkg/dbx"
+	"sikey.com/websocket/pkg/rdbx"
 )
 
 func TestOnlineRepository_Is(t *testing.T) {

+ 1 - 1
repositories/session_repositroy_test.go

@@ -7,7 +7,7 @@ import (
 	"testing"
 
 	"sikey.com/websocket/config"
-	"x.sikey.com.cn/serverx/dbx"
+	"sikey.com/websocket/pkg/dbx"
 )
 
 func getSessionRepository() SessionRepository {

+ 1 - 1
server/client.go

@@ -12,8 +12,8 @@ import (
 	"github.com/rotisserie/eris"
 	"go.uber.org/zap"
 	"sikey.com/websocket/models"
+	"sikey.com/websocket/pkg/gid"
 	"sikey.com/websocket/repositories"
-	"x.sikey.com.cn/serverx/gid"
 )
 
 type Client struct {

+ 1 - 1
server/hub_test.go

@@ -6,8 +6,8 @@ import (
 
 	"github.com/gin-gonic/gin"
 	"sikey.com/websocket/config"
+	"sikey.com/websocket/pkg/dbx"
 	"sikey.com/websocket/repositories"
-	"x.sikey.com.cn/serverx/dbx"
 )
 
 var userId = "d6faa0af-b863-48bb-b658-d961a9381585"

+ 3 - 3
server/nats.go

@@ -7,10 +7,10 @@ import (
 
 	"github.com/nats-io/nats.go"
 	"go.uber.org/zap"
+	"sikey.com/websocket/pkg/dbx"
+	"sikey.com/websocket/pkg/natx"
+	"sikey.com/websocket/pkg/rdbx"
 	"sikey.com/websocket/repositories"
-	"x.sikey.com.cn/serverx/dbx"
-	"x.sikey.com.cn/serverx/natx"
-	"x.sikey.com.cn/serverx/rdbx"
 )
 
 const (

+ 3 - 3
websocket.go

@@ -14,11 +14,11 @@ import (
 	"github.com/google/uuid"
 	"github.com/gorilla/websocket"
 	"sikey.com/websocket/config"
+	"sikey.com/websocket/pkg/dbx"
+	"sikey.com/websocket/pkg/gid"
+	"sikey.com/websocket/pkg/rdbx"
 	"sikey.com/websocket/repositories"
 	"sikey.com/websocket/server"
-	"x.sikey.com.cn/serverx/dbx"
-	"x.sikey.com.cn/serverx/gid"
-	"x.sikey.com.cn/serverx/rdbx"
 )
 
 var name = flag.String("n", "w303", "the name of the server")