infra

package
v0.0.0-...-7cfd609 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 10, 2026 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EngineMysql    = "mysql"
	EnginePostgres = "postgres"
)

Variables

This section is empty.

Functions

func CloseCron

func CloseCron()

func CloseDB

func CloseDB(t *testing.T, db *gorm.DB)

CloseDB 关闭数据库连接,通常在 defer 中调用

func CronLogger

func CronLogger() cron.Logger

func GetDsn

func GetDsn(engine, host, port, user, password, dbname string) string

func GetEtcdClient

func GetEtcdClient() *clientv3.Client

func GetTransaction

func GetTransaction(c context.Context) *gorm.DB

func InitCron

func InitCron()

func NewDB

func NewDB(
	params NewDbParams,
) *gorm.DB

func NewEtcdClient

func NewEtcdClient(endpoints []string) *clientv3.Client

func NewRedisClient

func NewRedisClient(
	host string,
	port int,
	user string,
	password string,
	db int,
) *redis.Client

func NewTestDB

func NewTestDB(t *testing.T, params TestDBParams) *gorm.DB

NewTestDB 创建测试用 MySQL 数据库连接 所有微服务的 data 层测试统一引用此函数,避免重复创建连接的代码。

用法:

func TestXxx(t *testing.T) {
    db := infra.NewTestDB(t, infra.TestDBParams{
        Driver:   "mysql",
        Host:     "127.0.0.1",
        Port:     3306,
        User:     "root",
        Password: "password",
        DBName:   "vcyuan",
    })
    defer func() {
        sqlDB, _ := db.DB()
        sqlDB.Close()
    }()
    repo := NewXxxRepo(&data.Data{DB: db})
    // ...
}

func NewTestRedis

func NewTestRedis(t *testing.T, params TestRedisParams) cache.Cache

NewTestRedis 创建测试用 Redis 缓存实例 所有微服务的 data 层测试统一引用此函数。

用法:

func TestXxx(t *testing.T) {
    cache := infra.NewTestRedis(t, infra.TestRedisParams{
        Host: "127.0.0.1",
        Port: 6379,
    })
    defer cache.Close()
    repo := NewXxxRepo(&data.Data{Cache: cache})
    // ...
}

func SetTransaction

func SetTransaction(c context.Context, tran *gorm.DB) context.Context

func StopTaskQueue

func StopTaskQueue()

func TaskQueue

func TaskQueue() *task.TaskQueue

Types

type CustomCronLogger

type CustomCronLogger struct {
	// contains filtered or unexported fields
}

func (*CustomCronLogger) Error

func (cl *CustomCronLogger) Error(err error, msg string, keysAndValues ...interface{})

func (*CustomCronLogger) Info

func (cl *CustomCronLogger) Info(msg string, keysAndValues ...interface{})

type Data

type Data struct {
	DB *gorm.DB

	RedisClient *RedisClient
	MongoDb     *mongo.Client
	MinioClient *minio.Client
	NatsMQ      *NatsMQ
	// contains filtered or unexported fields
}

func (*Data) WithTransaction

func (d *Data) WithTransaction(c context.Context) *gorm.DB

WithTransaction 从Context中获取父事务(父DB),子事务需自行调用db.Begin().无论是否成功,都会绑定context

type NatsMQ

type NatsMQ struct {
	// contains filtered or unexported fields
}

func NewNatsMQ

func NewNatsMQ(host string, port int) *NatsMQ

func (*NatsMQ) Close

func (n *NatsMQ) Close()

func (*NatsMQ) GetConn

func (n *NatsMQ) GetConn() *nats.Conn

type NewDbParams

type NewDbParams struct {
	Driver string
	Host   string
	Port   int
	User   string
	Pass   string
	DBName string
}

type RedisClient

type RedisClient struct {
	*redis.Client
}

func NewCustomRedisClient

func NewCustomRedisClient(rdb *redis.Client) *RedisClient

func (*RedisClient) GetObject

func (r *RedisClient) GetObject(ctx context.Context, key string, target any) error

target 为指针类型

func (*RedisClient) PutObject

func (r *RedisClient) PutObject(ctx context.Context, key string, target any, expiration time.Duration) error

type TestDBParams

type TestDBParams struct {
	Driver   string // "mysql"
	Host     string // "127.0.0.1"
	Port     int    // 3306
	User     string // "root"
	Password string // 你的密码
	DBName   string // 数据库名
}

TestDBParams MySQL 连接参数,由测试文件手动填入

type TestRedisParams

type TestRedisParams struct {
	Host     string // "127.0.0.1"
	Port     int    // 6379
	Password string // 密码,无密码传 ""
	DB       int    // 数据库编号,默认 0
}

TestRedisParams Redis 连接参数

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL