43 lines
755 B
Go
43 lines
755 B
Go
package repo
|
|
|
|
import (
|
|
"context"
|
|
|
|
"chatapp-cron/internal/config"
|
|
|
|
"github.com/redis/go-redis/v9"
|
|
"gorm.io/driver/mysql"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Repository struct {
|
|
DB *gorm.DB
|
|
Redis *redis.Client
|
|
}
|
|
|
|
func New(cfg config.Config) (*Repository, error) {
|
|
db, err := gorm.Open(mysql.Open(cfg.MySQLDSN), &gorm.Config{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
client := redis.NewClient(&redis.Options{
|
|
Addr: cfg.RedisAddr,
|
|
Password: cfg.RedisPassword,
|
|
DB: cfg.RedisDB,
|
|
})
|
|
|
|
return &Repository{DB: db, Redis: client}, nil
|
|
}
|
|
|
|
func (r *Repository) Ping(ctx context.Context) error {
|
|
if err := r.Redis.Ping(ctx).Err(); err != nil {
|
|
return err
|
|
}
|
|
sqlDB, err := r.DB.DB()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return sqlDB.PingContext(ctx)
|
|
}
|