diff --git a/pkg/starter/starter.go b/pkg/starter/starter.go new file mode 100644 index 0000000..7dc7996 --- /dev/null +++ b/pkg/starter/starter.go @@ -0,0 +1,46 @@ +package starter + +import ( + "fmt" + "os" + "os/signal" + "syscall" + + "gitea.ddegame.cn/open/servicebase/pkg/log" +) + +type IStarter interface { + Init() error +} + +type Starter struct { + instanceList []IStarter + instanceCount int + errs []error +} + +func NewStarter() *Starter { + return &Starter{} +} + +func (s *Starter) With(instance IStarter) *Starter { + s.instanceList = append(s.instanceList, instance) + return s +} + +func (s *Starter) Run() { + for _, instance := range s.instanceList { + if err := instance.Init(); err != nil { + s.errs = append(s.errs, err) + } + } + if len(s.errs) > 0 { + panic(fmt.Sprintf("some errors: %v", s.errs)) + } + log.Info("service all up") + ch := make(chan os.Signal, 1) + signal.Notify(ch, syscall.SIGTERM, syscall.SIGINT) + sig := <-ch + log.InfoF("api server Got %s signal. Aborting...\n", sig) + os.Exit(1) +}