generated from msb_47094/GinChat
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
1.9 KiB
93 lines
1.9 KiB
2 years ago
|
/**
|
||
|
* @Auth:ShenZ
|
||
|
* @Description:
|
||
|
* @CreateDate:2022/06/15 16:37:35
|
||
|
*/
|
||
|
package utils
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"log"
|
||
|
"os"
|
||
|
"time"
|
||
|
|
||
|
"github.com/go-redis/redis/v8"
|
||
|
"github.com/spf13/viper"
|
||
|
"gorm.io/driver/mysql"
|
||
|
"gorm.io/gorm"
|
||
|
"gorm.io/gorm/logger"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
DB *gorm.DB
|
||
|
Red *redis.Client
|
||
|
)
|
||
|
|
||
|
func InitConfig() {
|
||
|
viper.SetConfigName("app")
|
||
|
viper.AddConfigPath("config")
|
||
|
err := viper.ReadInConfig()
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
}
|
||
|
fmt.Println("config app inited 。。。。")
|
||
|
}
|
||
|
|
||
|
func InitMySQL() {
|
||
|
//自定义日志模板 打印SQL语句
|
||
|
newLogger := logger.New(
|
||
|
log.New(os.Stdout, "\r\n", log.LstdFlags),
|
||
|
logger.Config{
|
||
|
SlowThreshold: time.Second, //慢SQL阈值
|
||
|
LogLevel: logger.Info, //级别
|
||
|
Colorful: true, //彩色
|
||
|
},
|
||
|
)
|
||
|
|
||
|
DB, _ = gorm.Open(mysql.Open(viper.GetString("mysql.dns")),
|
||
|
&gorm.Config{Logger: newLogger})
|
||
|
fmt.Println(" MySQL inited 。。。。")
|
||
|
//user := models.UserBasic{}
|
||
|
//DB.Find(&user)
|
||
|
//fmt.Println(user)
|
||
|
}
|
||
|
|
||
|
func InitRedis() {
|
||
|
Red = redis.NewClient(&redis.Options{
|
||
|
Addr: viper.GetString("redis.addr"),
|
||
|
Password: viper.GetString("redis.password"),
|
||
|
DB: viper.GetInt("redis.DB"),
|
||
|
PoolSize: viper.GetInt("redis.poolSize"),
|
||
|
MinIdleConns: viper.GetInt("redis.minIdleConn"),
|
||
|
})
|
||
|
}
|
||
|
|
||
|
const (
|
||
|
PublishKey = "websocket"
|
||
|
)
|
||
|
|
||
|
//Publish 发布消息到Redis
|
||
|
func Publish(ctx context.Context, channel string, msg string) error {
|
||
|
var err error
|
||
|
fmt.Println("Publish 。。。。", msg)
|
||
|
err = Red.Publish(ctx, channel, msg).Err()
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
//Subscribe 订阅Redis消息
|
||
|
func Subscribe(ctx context.Context, channel string) (string, error) {
|
||
|
sub := Red.Subscribe(ctx, channel)
|
||
|
fmt.Println("Subscribe 。。。。", ctx)
|
||
|
msg, err := sub.ReceiveMessage(ctx)
|
||
|
if err != nil {
|
||
|
fmt.Println(err)
|
||
|
return "", err
|
||
|
}
|
||
|
fmt.Println("Subscribe 。。。。", msg.Payload)
|
||
|
return msg.Payload, err
|
||
|
}
|