-
Notifications
You must be signed in to change notification settings - Fork 32
/
app.go
72 lines (64 loc) · 1.8 KB
/
app.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package internal
import (
"os"
"github.com/fananchong/go-xserver/common"
"github.com/fananchong/go-xserver/internal/components"
"github.com/fananchong/go-xserver/internal/components/misc"
nodegateway "github.com/fananchong/go-xserver/internal/components/node/gateway"
nodelogin "github.com/fananchong/go-xserver/internal/components/node/login"
nodemgr "github.com/fananchong/go-xserver/internal/components/node/mgr"
nodenormal "github.com/fananchong/go-xserver/internal/components/node/normal"
"github.com/fananchong/go-xserver/internal/utils"
)
// App : 应用程序类
type App struct {
ctx *common.Context
components []utils.IComponent
}
// NewApp : 应用程序类的构造函数
func NewApp() *App {
app := &App{
ctx: &common.Context{Context: misc.CreateContext()},
}
return app
}
// Run : 启动应用程序
func (app *App) Run() {
// 注册组件
app.components = []utils.IComponent{
components.NewTime(app.ctx),
components.NewRand(app.ctx),
components.NewConfig(app.ctx),
components.NewLog(app.ctx),
components.NewPprof(app.ctx),
components.NewPlugin(app.ctx),
components.NewRedis(app.ctx),
components.NewRole2Account(app.ctx),
components.NewUID(app.ctx),
components.NewTCPServer(app.ctx),
nodenormal.NewNormal(app.ctx),
nodemgr.NewMgr(app.ctx),
nodelogin.NewLogin(app.ctx),
nodegateway.NewGateway(app.ctx),
components.NewSignal(app.ctx),
}
// 应用程序正式运行
defer app.onAppShutDown()
app.onAppReady()
}
func (app *App) onAppReady() {
misc.SetComponentCount(app.ctx, len(app.components))
for i := 0; i < len(app.components); i++ {
misc.OneComponentOK(app.ctx)
c := app.components[i]
if !c.Start() {
os.Exit(1)
}
}
}
func (app *App) onAppShutDown() {
for i := len(app.components) - 1; i >= 0; i-- {
c := app.components[i]
c.Close()
}
}