optimze service interface add Version() to return version info

pull/196/head
Michael Li 2 years ago
parent b5312cc42c
commit 0e7f47f7a3
No known key found for this signature in database

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/Masterminds/semver/v3"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -15,6 +16,10 @@ import (
"github.com/rocboss/paopao-ce/internal/servants" "github.com/rocboss/paopao-ce/internal/servants"
) )
var (
_ Service = (*adminService)(nil)
)
type adminService struct { type adminService struct {
*baseHttpService *baseHttpService
} }
@ -23,6 +28,10 @@ func (s *adminService) Name() string {
return "AdminService" return "AdminService"
} }
func (s *adminService) Version() *semver.Version {
return semver.MustParse("v0.1.0")
}
func (s *adminService) Init() error { func (s *adminService) Init() error {
s.registerRoute(servants.RegisterAdminServants) s.registerRoute(servants.RegisterAdminServants)
return nil return nil

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/Masterminds/semver/v3"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -15,6 +16,10 @@ import (
"github.com/rocboss/paopao-ce/internal/servants" "github.com/rocboss/paopao-ce/internal/servants"
) )
var (
_ Service = (*botService)(nil)
)
type botService struct { type botService struct {
*baseHttpService *baseHttpService
} }
@ -23,6 +28,10 @@ func (s *botService) Name() string {
return "BotService" return "BotService"
} }
func (s *botService) Version() *semver.Version {
return semver.MustParse("v0.1.0")
}
func (s *botService) Init() error { func (s *botService) Init() error {
s.registerRoute(servants.RegisterBotServants) s.registerRoute(servants.RegisterBotServants)
return nil return nil

@ -8,12 +8,17 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/Masterminds/semver/v3"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/conf"
"github.com/rocboss/paopao-ce/internal/servants" "github.com/rocboss/paopao-ce/internal/servants"
) )
var (
_ Service = (*localossService)(nil)
)
type localossService struct { type localossService struct {
*baseHttpService *baseHttpService
} }
@ -22,6 +27,10 @@ func (s *localossService) Name() string {
return "LocalossService" return "LocalossService"
} }
func (s *localossService) Version() *semver.Version {
return semver.MustParse("v0.1.0")
}
func (s *localossService) Init() error { func (s *localossService) Init() error {
s.registerRoute(servants.RegisterLocalossServants) s.registerRoute(servants.RegisterLocalossServants)
return nil return nil

@ -7,12 +7,14 @@ package service
import ( import (
"log" "log"
"github.com/Masterminds/semver/v3"
"github.com/alimy/cfg" "github.com/alimy/cfg"
"github.com/rocboss/paopao-ce/pkg/types" "github.com/rocboss/paopao-ce/pkg/types"
) )
type Service interface { type Service interface {
Name() string Name() string
Version() *semver.Version
Init() error Init() error
Start() error Start() error
Stop() error Stop() error
@ -24,10 +26,15 @@ func (baseService) Name() string {
return "" return ""
} }
func (baseService) Version() *semver.Version {
return semver.MustParse("v0.0.1")
}
func (baseService) String() string { func (baseService) String() string {
return "" return ""
} }
// InitService Initial service
func InitService() []Service { func InitService() []Service {
ss := newService() ss := newService()
for _, s := range ss { for _, s := range ss {
@ -38,6 +45,18 @@ func InitService() []Service {
return ss return ss
} }
// MaxSidSize max service id string length
func MaxSidSize(ss []Service) int {
length := 0
for _, s := range ss {
size := len(s.Name() + "@" + s.Version().String())
if size > length {
length = size
}
}
return length
}
func newService() (ss []Service) { func newService() (ss []Service) {
ss = append(ss, newWebService()) ss = append(ss, newWebService())

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/Masterminds/semver/v3"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -15,6 +16,10 @@ import (
"github.com/rocboss/paopao-ce/internal/servants" "github.com/rocboss/paopao-ce/internal/servants"
) )
var (
_ Service = (*spaceXService)(nil)
)
type spaceXService struct { type spaceXService struct {
*baseHttpService *baseHttpService
} }
@ -23,6 +28,10 @@ func (s *spaceXService) Name() string {
return "WebService" return "WebService"
} }
func (s *spaceXService) Version() *semver.Version {
return semver.MustParse("v0.1.0")
}
func (s *spaceXService) Init() error { func (s *spaceXService) Init() error {
s.registerRoute(servants.RegisterSpaceXServants) s.registerRoute(servants.RegisterSpaceXServants)
return nil return nil

@ -8,6 +8,7 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/Masterminds/semver/v3"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/gin-contrib/cors" "github.com/gin-contrib/cors"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
@ -15,6 +16,10 @@ import (
"github.com/rocboss/paopao-ce/internal/servants" "github.com/rocboss/paopao-ce/internal/servants"
) )
var (
_ Service = (*webService)(nil)
)
type webService struct { type webService struct {
*baseHttpService *baseHttpService
} }
@ -23,6 +28,10 @@ func (s *webService) Name() string {
return "WebService" return "WebService"
} }
func (s *webService) Version() *semver.Version {
return semver.MustParse("v0.1.0")
}
func (s *webService) Init() error { func (s *webService) Init() error {
s.registerRoute(servants.RegisterWebServants) s.registerRoute(servants.RegisterWebServants)
return nil return nil

@ -8,11 +8,16 @@ import (
"fmt" "fmt"
"net/http" "net/http"
"github.com/Masterminds/semver/v3"
"github.com/fatih/color" "github.com/fatih/color"
"github.com/rocboss/paopao-ce/internal/conf" "github.com/rocboss/paopao-ce/internal/conf"
"github.com/rocboss/paopao-ce/internal/servants/web/routers" "github.com/rocboss/paopao-ce/internal/servants/web/routers"
) )
var (
_ Service = (*oldWebService)(nil)
)
type oldWebService struct { type oldWebService struct {
*baseHttpService *baseHttpService
} }
@ -21,6 +26,10 @@ func (s *oldWebService) Name() string {
return "OldWebService" return "OldWebService"
} }
func (s *oldWebService) Version() *semver.Version {
return semver.MustParse("v0.1.0")
}
func (s *oldWebService) Init() error { func (s *oldWebService) Init() error {
s.registerRoute(routers.RegisterRoute) s.registerRoute(routers.RegisterRoute)
return nil return nil

@ -57,11 +57,12 @@ 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")
l := service.MaxSidSize(ss)
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", s.Name(), s) fmt.Fprintf(color.Output, "%s [start] - %s", util.SidStr(s.Name(), s.Version(), l), s)
if err := s.Start(); err != nil { if err := s.Start(); err != nil {
fmt.Fprintf(color.Output, "%s[start] - occurs on error: %s\n", s.Name(), err) fmt.Fprintf(color.Output, "%s [start] - occurs on error: %s\n", util.SidStr(s.Name(), s.Version(), l), err)
} }
wg.Done() wg.Done()
}(s) }(s)
@ -76,12 +77,12 @@ func runManage(wg *sync.WaitGroup, ss []service.Service) {
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM) signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit <-quit
fmt.Fprintf(color.Output, "\nshutting down server...\n\n") fmt.Fprintf(color.Output, "\nshutting down server...\n\n")
l := service.MaxSidSize(ss)
for _, s := range ss { for _, s := range ss {
if err := s.Stop(); err != nil { if err := s.Stop(); err != nil {
fmt.Fprintf(color.Output, "%s[stop] - occurs on error: %s\n", s.Name(), err) fmt.Fprintf(color.Output, "%s [stop] - occurs on error: %s\n", util.SidStr(s.Name(), s.Version(), l), err)
} }
fmt.Fprintf(color.Output, "%s[stop] - finish...\n", s.Name()) fmt.Fprintf(color.Output, "%s [stop] - finish...\n", util.SidStr(s.Name(), s.Version(), l))
} }
wg.Done() wg.Done()
} }

@ -4,7 +4,11 @@
package util package util
import "fmt" import (
"fmt"
"github.com/Masterminds/semver/v3"
)
func PrintHelloBanner(text string) { func PrintHelloBanner(text string) {
@ -14,3 +18,7 @@ func PrintHelloBanner(text string) {
fmt.Println("(__) \\_/\\_/ \\__/(__) \\_/\\_/ \\__/ ") fmt.Println("(__) \\_/\\_/ \\__/(__) \\_/\\_/ \\__/ ")
fmt.Println(text) fmt.Println(text)
} }
func SidStr(name string, version *semver.Version, size int) string {
return fmt.Sprintf(fmt.Sprintf("%%s%%-%ds", size-len(name+version.String())+4), name, version)
}

Loading…
Cancel
Save