optimize main function logic and custom service create logic

pull/196/head
Michael Li 2 years ago
parent 3ad6f66a76
commit 6a53632b9b
No known key found for this signature in database

@ -7,6 +7,7 @@ package service
import ( import (
"log" "log"
"github.com/rocboss/paopao-ce/pkg/cfg"
"github.com/rocboss/paopao-ce/pkg/types" "github.com/rocboss/paopao-ce/pkg/types"
) )
@ -27,13 +28,23 @@ func (baseService) String() string {
return "" return ""
} }
func InitService() (ss []Service) { func InitService() []Service {
ss = append(ss, newWebService(), newOldWebService()) ss := newService()
for _, s := range ss { for _, s := range ss {
if err := s.Init(); err != nil { if err := s.Init(); err != nil {
log.Fatalf("initial %s service error: %s", s.Name(), err) log.Fatalf("initial %s service error: %s", s.Name(), err)
} }
} }
return ss
}
func newService() (ss []Service) {
ss = append(ss, newWebService())
// add oldWebService if not depredcated OldWebService
cfg.Not("Deprecated:OldWeb", func() {
ss = append(ss, newOldWebService())
})
return return
} }

@ -7,10 +7,10 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"log"
"os" "os"
"os/signal" "os/signal"
"strings" "strings"
"sync"
"syscall" "syscall"
"github.com/fatih/color" "github.com/fatih/color"
@ -48,24 +48,27 @@ func init() {
} }
func flagParse() { func flagParse() {
flag.BoolVar(&noDefaultFeatures, "no-default-features", false, "whether use default features") flag.BoolVar(&noDefaultFeatures, "no-default-features", false, "whether not use default features")
flag.Var(&features, "features", "use special features") flag.Var(&features, "features", "use special features")
flag.Parse() flag.Parse()
} }
func runService(ss []service.Service) { func runService(wg *sync.WaitGroup, ss []service.Service) {
gin.SetMode(conf.RunMode()) gin.SetMode(conf.RunMode())
fmt.Fprintf(color.Output, "\nstarting run service...\n\n") fmt.Fprintf(color.Output, "\nstarting run service...\n\n")
for _, s := range ss { for _, s := range ss {
go func(s service.Service) { go func(s service.Service) {
fmt.Fprintf(color.Output, "%s[start] - %s\n", s.Name(), s) fmt.Fprintf(color.Output, "%s[start] - %s", s.Name(), s)
if err := s.Start(); err != nil { if err := s.Start(); err != nil {
log.Fatalf("%s[start] - occurs on error: %s\n", s.Name(), err) fmt.Fprintf(color.Output, "%s[start] - occurs on error: %s\n", s.Name(), err)
} }
wg.Done()
}(s) }(s)
} }
}
func runManage(wg *sync.WaitGroup, ss []service.Service) {
quit := make(chan os.Signal, 1) quit := make(chan os.Signal, 1)
// kill (no param) default send syscall.SIGTERM // kill (no param) default send syscall.SIGTERM
// kill -2 is syscall.SIGINT // kill -2 is syscall.SIGINT
@ -80,13 +83,20 @@ func runService(ss []service.Service) {
} }
fmt.Fprintf(color.Output, "%s[stop] - finish...\n", s.Name()) fmt.Fprintf(color.Output, "%s[stop] - finish...\n", s.Name())
} }
wg.Done()
} }
func main() { func main() {
util.PrintHelloBanner(debug.VersionInfo()) util.PrintHelloBanner(debug.VersionInfo())
if ss := service.InitService(); len(ss) > 0 { if ss := service.InitService(); len(ss) > 0 {
runService(ss) wg := &sync.WaitGroup{}
wg.Add(len(ss) + 1)
runService(wg, ss)
go runManage(wg, ss)
wg.Wait()
} else { } else {
fmt.Fprintf(color.Output, "no service need start so just exit") fmt.Fprintf(color.Output, "no service need start so just exit")
} }

Loading…
Cancel
Save