feat: refactor.
This commit is contained in:
@@ -1,19 +1,57 @@
|
||||
package bootstrap
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/go-kratos/kratos/v2"
|
||||
"github.com/go-kratos/kratos/v2/log"
|
||||
"github.com/go-kratos/kratos/v2/registry"
|
||||
|
||||
v1 "github.com/tx7do/kratos-bootstrap/api/gen/go/conf/v1"
|
||||
"testing"
|
||||
"github.com/tx7do/kratos-bootstrap/config"
|
||||
)
|
||||
|
||||
func initApp(logger log.Logger, registrar registry.Registrar, bootstrap *v1.Bootstrap) (*kratos.App, func(), error) {
|
||||
func initApp(logger log.Logger, registrar registry.Registrar, _ *v1.Bootstrap) (*kratos.App, func(), error) {
|
||||
app := NewApp(logger, registrar)
|
||||
return app, func() {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func TestBootstrap(t *testing.T) {
|
||||
Bootstrap(initApp)
|
||||
serviceName := "test"
|
||||
version := "v0.0.1"
|
||||
Bootstrap(initApp, &serviceName, &version)
|
||||
}
|
||||
|
||||
type CustomConfig struct {
|
||||
Cfg string `protobuf:"bytes,1,opt,name=cfg,proto3" json:"cfg,omitempty"`
|
||||
}
|
||||
|
||||
func initAppEx(logger log.Logger, registrar registry.Registrar, _ *v1.Bootstrap, _ *CustomConfig) (*kratos.App, func(), error) {
|
||||
app := NewApp(logger, registrar)
|
||||
return app, func() {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func TestCustomBootstrap(t *testing.T) {
|
||||
customCfg := &CustomConfig{}
|
||||
config.RegisterConfig(customCfg)
|
||||
|
||||
Service.SetName("test")
|
||||
Service.SetVersion("v0.0.1")
|
||||
|
||||
// bootstrap
|
||||
cfg, ll, reg := DoBootstrap(Service)
|
||||
|
||||
// init app
|
||||
app, cleanup, err := initAppEx(ll, reg, cfg, customCfg)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
defer cleanup()
|
||||
|
||||
// run the app.
|
||||
if err = app.Run(); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,6 +21,14 @@ func NewServiceInfo(name, version, id string) *ServiceInfo {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ServiceInfo) SetName(name string) {
|
||||
s.Name = name
|
||||
}
|
||||
|
||||
func (s *ServiceInfo) SetVersion(version string) {
|
||||
s.Version = version
|
||||
}
|
||||
|
||||
func (s *ServiceInfo) GetInstanceId() string {
|
||||
return s.Id + "." + s.Name
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user