Compare commits

...

3 Commits

Author SHA1 Message Date
tx7do
5e85934e35 feat: refactor. 2024-05-14 16:37:22 +08:00
tx7do
edbcad5973 feat: refactor. 2024-05-14 16:25:53 +08:00
tx7do
6711cb7613 feat: refactor. 2024-05-14 16:25:37 +08:00
4 changed files with 53 additions and 7 deletions

View File

@@ -39,8 +39,8 @@ func NewApp(ll log.Logger, rr kratosRegistry.Registrar, srv ...transport.Server)
)
}
// doBootstrap 执行引导
func doBootstrap(serviceInfo *config.ServiceInfo) (*conf.Bootstrap, log.Logger, kratosRegistry.Registrar) {
// DoBootstrap 执行引导
func DoBootstrap(serviceInfo *config.ServiceInfo) (*conf.Bootstrap, log.Logger, kratosRegistry.Registrar) {
// inject command flags
Flags := config.NewCommandFlags()
Flags.Init()
@@ -78,7 +78,7 @@ func Bootstrap(initApp InitApp, serviceName, version *string) {
}
// bootstrap
cfg, ll, reg := doBootstrap(Service)
cfg, ll, reg := DoBootstrap(Service)
// init app
app, cleanup, err := initApp(ll, reg, cfg)

View File

@@ -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)
}
}

View File

@@ -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
}

View File

@@ -1,4 +1,4 @@
git tag v0.3.7
git tag v0.3.9
git tag api/v0.0.2 --force