// Command mongo-index ensures Gateway MongoDB indexes exist. package main import ( "context" "flag" "fmt" "os" "time" "gateway/internal/config" memberrepo "gateway/internal/model/member/repository" notifrepo "gateway/internal/model/notification/repository" "github.com/zeromicro/go-zero/core/conf" ) var configFile = flag.String("f", "etc/gateway.dev.yaml", "config file") func main() { if err := run(); err != nil { fmt.Fprintln(os.Stderr, err) os.Exit(1) } } func run() error { flag.Parse() var c config.Config conf.MustLoad(*configFile, &c) if c.Mongo.Host == "" { return fmt.Errorf("mongo-index: Mongo.Host is empty in config") } ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) defer cancel() notifRepo := notifrepo.NewNotificationRepository(notifrepo.NotificationRepositoryParam{Conf: &c.Mongo}) dlqRepo := notifrepo.NewNotificationDLQRepository(notifrepo.NotificationDLQRepositoryParam{Conf: &c.Mongo}) if err := notifRepo.Index20260520001UP(ctx); err != nil { return fmt.Errorf("mongo-index: notifications: %w", err) } if err := dlqRepo.Index20260520001UP(ctx); err != nil { return fmt.Errorf("mongo-index: notification_dlq: %w", err) } if err := memberrepo.EnsureMongoIndexes(ctx, &c.Mongo); err != nil { return fmt.Errorf("mongo-index: member: %w", err) } fmt.Println("mongo-index: notifications + notification_dlq + member indexes OK") return nil }