feat app 模块
This commit is contained in:
30
app/app.go
30
app/app.go
@@ -3,27 +3,29 @@ package app
|
||||
import (
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/module"
|
||||
"git.hlsq.asia/mmorpg/service-robot/config"
|
||||
"github.com/judwhite/go-svc"
|
||||
)
|
||||
|
||||
type Program struct {
|
||||
moduleList []Module // 模块列表
|
||||
}
|
||||
|
||||
type Module interface {
|
||||
init() error
|
||||
start() error
|
||||
stop() error
|
||||
moduleList []module.Module // 模块列表
|
||||
}
|
||||
|
||||
func (p *Program) Init(_ svc.Environment) error {
|
||||
p.moduleList = append(p.moduleList, &ModuleBase{})
|
||||
base := &ModuleBase{}
|
||||
if err := base.Init(); err != nil {
|
||||
return err
|
||||
}
|
||||
p.moduleList = append(p.moduleList, base)
|
||||
p.moduleList = append(p.moduleList, &ModuleWebsocket{})
|
||||
p.moduleList = append(p.moduleList, &ModulePprof{})
|
||||
|
||||
for _, module := range p.moduleList {
|
||||
if err := module.init(); err != nil {
|
||||
for i, m := range p.moduleList {
|
||||
if i == 0 {
|
||||
continue
|
||||
}
|
||||
if err := m.Init(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -32,8 +34,8 @@ func (p *Program) Init(_ svc.Environment) error {
|
||||
}
|
||||
|
||||
func (p *Program) Start() error {
|
||||
for _, module := range p.moduleList {
|
||||
if err := module.start(); err != nil {
|
||||
for _, m := range p.moduleList {
|
||||
if err := m.Start(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -44,8 +46,8 @@ func (p *Program) Start() error {
|
||||
|
||||
func (p *Program) Stop() error {
|
||||
for i := len(p.moduleList) - 1; i >= 0; i-- {
|
||||
module := p.moduleList[i]
|
||||
if err := module.stop(); err != nil {
|
||||
m := p.moduleList[i]
|
||||
if err := m.Stop(); err != nil {
|
||||
log.Errorf("module stop error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
11
app/base.go
11
app/base.go
@@ -2,6 +2,7 @@ package app
|
||||
|
||||
import (
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/module"
|
||||
"git.hlsq.asia/mmorpg/service-common/utils"
|
||||
"git.hlsq.asia/mmorpg/service-robot/config"
|
||||
"math/rand"
|
||||
@@ -11,7 +12,7 @@ import (
|
||||
type ModuleBase struct {
|
||||
}
|
||||
|
||||
func (p *ModuleBase) init() error {
|
||||
func (m *ModuleBase) Init() error {
|
||||
// 配置
|
||||
if err := config.LoadConfig(); err != nil {
|
||||
return err
|
||||
@@ -24,10 +25,14 @@ func (p *ModuleBase) init() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *ModuleBase) start() error {
|
||||
func (m *ModuleBase) Start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *ModuleBase) stop() error {
|
||||
func (m *ModuleBase) Stop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *ModuleBase) Bind(_ ...any) module.Module {
|
||||
return m
|
||||
}
|
||||
|
||||
11
app/pprof.go
11
app/pprof.go
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"errors"
|
||||
"git.hlsq.asia/mmorpg/service-common/log"
|
||||
"git.hlsq.asia/mmorpg/service-common/module"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
"sync"
|
||||
@@ -15,12 +16,12 @@ type ModulePprof struct {
|
||||
server *http.Server
|
||||
}
|
||||
|
||||
func (m *ModulePprof) init() error {
|
||||
func (m *ModulePprof) Init() error {
|
||||
m.wg = &sync.WaitGroup{}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *ModulePprof) start() error {
|
||||
func (m *ModulePprof) Start() error {
|
||||
m.wg.Add(1)
|
||||
go func() {
|
||||
defer m.wg.Done()
|
||||
@@ -35,10 +36,14 @@ func (m *ModulePprof) start() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *ModulePprof) stop() error {
|
||||
func (m *ModulePprof) Stop() error {
|
||||
if err := m.server.Shutdown(context.Background()); err != nil {
|
||||
log.Errorf("stop pprof server failed: %v", err)
|
||||
}
|
||||
m.wg.Wait()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *ModulePprof) Bind(_ ...any) module.Module {
|
||||
return m
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package app
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"git.hlsq.asia/mmorpg/service-common/module"
|
||||
"git.hlsq.asia/mmorpg/service-robot/config"
|
||||
"git.hlsq.asia/mmorpg/service-robot/internal/ws"
|
||||
)
|
||||
@@ -11,7 +12,7 @@ type ModuleWebsocket struct {
|
||||
manager *ws.Manager
|
||||
}
|
||||
|
||||
func (p *ModuleWebsocket) init() error {
|
||||
func (m *ModuleWebsocket) Init() error {
|
||||
cfg := config.Get().Client
|
||||
httpAddr := fmt.Sprintf("%s:%d", cfg.Http.Address, cfg.Http.Port)
|
||||
if cfg.Http.Port == 0 {
|
||||
@@ -21,15 +22,19 @@ func (p *ModuleWebsocket) init() error {
|
||||
if cfg.Websocket.Port == 0 {
|
||||
websocketAddr = cfg.Websocket.Address
|
||||
}
|
||||
p.manager = ws.NewManager(httpAddr, websocketAddr)
|
||||
m.manager = ws.NewManager(httpAddr, websocketAddr)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *ModuleWebsocket) start() error {
|
||||
p.manager.Start()
|
||||
func (m *ModuleWebsocket) Start() error {
|
||||
m.manager.Start()
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *ModuleWebsocket) stop() error {
|
||||
func (m *ModuleWebsocket) Stop() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *ModuleWebsocket) Bind(_ ...any) module.Module {
|
||||
return m
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user