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.
55 lines
1.4 KiB
55 lines
1.4 KiB
package data
|
|
|
|
import (
|
|
"context"
|
|
"github.com/go-kratos/kratos/examples/blog/internal/conf"
|
|
"github.com/go-kratos/kratos/examples/blog/internal/data/ent"
|
|
"github.com/go-kratos/kratos/v2/log"
|
|
"github.com/go-redis/redis/extra/redisotel"
|
|
"github.com/go-redis/redis/v8"
|
|
"github.com/google/wire"
|
|
|
|
// init mysql driver
|
|
_ "github.com/go-sql-driver/mysql"
|
|
)
|
|
|
|
// ProviderSet is data providers.
|
|
var ProviderSet = wire.NewSet(NewData, NewArticleRepo)
|
|
|
|
// Data .
|
|
type Data struct {
|
|
db *ent.Client
|
|
rdb *redis.Client
|
|
}
|
|
|
|
// NewData .
|
|
func NewData(conf *conf.Data, logger log.Logger) (*Data, error) {
|
|
log := log.NewHelper("data", logger)
|
|
client, err := ent.Open(
|
|
conf.Database.Driver,
|
|
conf.Database.Source,
|
|
)
|
|
if err != nil {
|
|
log.Errorf("failed opening connection to sqlite: %v", err)
|
|
return nil, err
|
|
}
|
|
// Run the auto migration tool.
|
|
if err := client.Schema.Create(context.Background()); err != nil {
|
|
log.Errorf("failed creating schema resources: %v", err)
|
|
return nil, err
|
|
}
|
|
|
|
rdb := redis.NewClient(&redis.Options{
|
|
Addr: conf.Redis.Addr,
|
|
Password: conf.Redis.Password,
|
|
DB: int(conf.Redis.Db),
|
|
DialTimeout: conf.Redis.DialTimeout.AsDuration(),
|
|
WriteTimeout: conf.Redis.WriteTimeout.AsDuration(),
|
|
ReadTimeout: conf.Redis.ReadTimeout.AsDuration(),
|
|
})
|
|
rdb.AddHook(redisotel.TracingHook{})
|
|
return &Data{
|
|
db: client,
|
|
rdb: rdb,
|
|
}, nil
|
|
}
|
|
|