Replace DB with Store in service/user (#321)

This commit is contained in:
zhaoyang
2022-01-28 19:03:34 +08:00
committed by GitHub
parent 8246d8f8a7
commit cd215a8392
17 changed files with 885 additions and 284 deletions

View File

@@ -1,31 +1,71 @@
package main
import (
"time"
"github.com/micro/micro/v3/service"
"github.com/micro/micro/v3/service/config"
"github.com/micro/micro/v3/service/logger"
db "github.com/micro/services/db/proto"
"github.com/micro/micro/v3/service/store"
"gorm.io/driver/postgres"
"gorm.io/gorm"
otp "github.com/micro/services/otp/proto"
"github.com/micro/services/pkg/tracing"
"github.com/micro/services/user/handler"
"github.com/micro/services/user/migrate"
proto "github.com/micro/services/user/proto"
)
var pgxDsn = "postgresql://postgres:postgres@localhost:5432/db?sslmode=disable"
func migrateData() {
startTime := time.Now()
logger.Info("start migrate ...")
defer func() {
logger.Infof("all migrations are finished, use time: %v", time.Since(startTime))
}()
// Connect to the database
cfg, err := config.Get("micro.db.database")
if err != nil {
logger.Fatalf("Error loading config: %v", err)
}
dsn := cfg.String(pgxDsn)
gormDb, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
if err != nil {
logger.Fatal("Failed to connect to ")
}
migration := migrate.NewMigration(gormDb)
if err := migration.Do(); err != nil {
logger.Fatal("migrate error: ", err)
}
return
}
func main() {
service := service.New(
srv := service.New(
service.Name("user"),
)
service.Init()
srv.Init()
// migration work
go migrateData()
hd := handler.NewUser(
db.NewDbService("db", service.Client()),
otp.NewOtpService("otp", service.Client()),
store.DefaultStore,
otp.NewOtpService("otp", srv.Client()),
)
proto.RegisterUserHandler(service.Server(), hd)
proto.RegisterUserHandler(srv.Server(), hd)
traceCloser := tracing.SetupOpentracing("user")
defer traceCloser.Close()
if err := service.Run(); err != nil {
if err := srv.Run(); err != nil {
logger.Fatal(err)
}
}