github.com/NpoolPlatform/chain-middleware@v0.0.0-20240228100535-eb1bcf896eb9/pkg/testinit/testinit.go (about)

     1  package testinit
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"path"
     7  	"runtime"
     8  
     9  	"github.com/NpoolPlatform/go-service-framework/pkg/app"
    10  
    11  	"github.com/NpoolPlatform/chain-middleware/pkg/db"
    12  
    13  	migrator "github.com/NpoolPlatform/chain-middleware/pkg/migrator"
    14  	servicename "github.com/NpoolPlatform/chain-middleware/pkg/servicename"
    15  
    16  	mysqlconst "github.com/NpoolPlatform/go-service-framework/pkg/mysql/const"
    17  	rabbitmqconst "github.com/NpoolPlatform/go-service-framework/pkg/rabbitmq/const"
    18  	redisconst "github.com/NpoolPlatform/go-service-framework/pkg/redis/const"
    19  )
    20  
    21  func Init() error {
    22  	_, myPath, _, ok := runtime.Caller(0)
    23  	if !ok {
    24  		return fmt.Errorf("cannot get source file path")
    25  	}
    26  
    27  	appName := path.Base(path.Dir(path.Dir(path.Dir(myPath))))
    28  	configPath := fmt.Sprintf("%s/../../cmd/%v", path.Dir(myPath), appName)
    29  
    30  	err := app.Init(
    31  		servicename.ServiceName,
    32  		"",
    33  		"",
    34  		"",
    35  		configPath,
    36  		nil,
    37  		nil,
    38  		mysqlconst.MysqlServiceName,
    39  		rabbitmqconst.RabbitMQServiceName,
    40  		redisconst.RedisServiceName,
    41  	)
    42  	if err != nil {
    43  		return fmt.Errorf("cannot init app stub: %v", err)
    44  	}
    45  	if err := migrator.Migrate(context.Background()); err != nil {
    46  		return fmt.Errorf("fail migrate db: %v", err)
    47  	}
    48  	err = db.Init()
    49  	if err != nil {
    50  		return fmt.Errorf("cannot init database: %v", err)
    51  	}
    52  
    53  	return nil
    54  }