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

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

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

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

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

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

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

@ -57,11 +57,12 @@ func runService(wg *sync.WaitGroup, ss []service.Service) {
gin.SetMode(conf.RunMode())
fmt.Fprintf(color.Output, "\nstarting run service...\n\n")
l := service.MaxSidSize(ss)
for _, s := range ss {
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 {
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()
}(s)
@ -76,12 +77,12 @@ func runManage(wg *sync.WaitGroup, ss []service.Service) {
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
fmt.Fprintf(color.Output, "\nshutting down server...\n\n")
l := service.MaxSidSize(ss)
for _, s := range ss {
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()
}

@ -4,7 +4,11 @@
package util
import "fmt"
import (
"fmt"
"github.com/Masterminds/semver/v3"
)
func PrintHelloBanner(text string) {
@ -14,3 +18,7 @@ func PrintHelloBanner(text string) {
fmt.Println("(__) \\_/\\_/ \\__/(__) \\_/\\_/ \\__/ ")
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