-
Notifications
You must be signed in to change notification settings - Fork 1
/
pakku.go
166 lines (147 loc) · 5.79 KB
/
pakku.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
// Copyright (C) 2019 WuPeng <wup364@outlook.com>.
// Use of jsoncfg source code is governed by an MIT-style.
// Permission is hereby granted, free of charge, to any person obtaining a copy of jsoncfg software and associated documentation files (the "Software"), to deal in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
// The above copyright notice and jsoncfg permission notice shall be included in all copies or substantial portions of the Software.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// 入口包
package pakku
import (
"fmt"
"io"
"os"
"strings"
"github.com/wup364/pakku/ipakku"
"github.com/wup364/pakku/mloader"
"github.com/wup364/pakku/modules/appcache"
"github.com/wup364/pakku/modules/appconfig"
"github.com/wup364/pakku/modules/appevent"
"github.com/wup364/pakku/modules/appservice"
"github.com/wup364/pakku/utils/fileutil"
"github.com/wup364/pakku/utils/logs"
)
// NewApplication 新建应用加载器
func NewApplication(name string) ipakku.ApplicationBoot {
return &ApplicationBoot{
modules: make([]ipakku.Module, 0),
showBanner: true,
application: &Application{
Loader: mloader.New(name),
},
}
}
// ApplicationBoot 程序启动引导
type ApplicationBoot struct {
modules []ipakku.Module
application *Application
showBanner bool
loadedCore bool
loadedNet bool
}
// GetInstanceID 获取实例的ID
func (boot *ApplicationBoot) GetApplication() ipakku.Application {
return boot.application
}
// AddModule 加载模块
func (boot *ApplicationBoot) AddModule(mt ipakku.Module) ipakku.ApplicationBoot {
boot.modules = append(boot.modules, mt)
return boot
}
// AddModules 加载模块
func (boot *ApplicationBoot) AddModules(mts ...ipakku.Module) ipakku.ApplicationBoot {
boot.modules = append(boot.modules, mts...)
return boot
}
// EnableCoreModule 加载基础的运行模块 [AppConfig, AppCache, AppEvent]
func (boot *ApplicationBoot) EnableCoreModule() ipakku.ApplicationBoot {
if !boot.loadedCore {
boot.loadedCore = true
boot.AddModules(new(appconfig.AppConfig), new(appcache.AppCache), new(appevent.AppEvent))
}
return boot
}
// EnableNetModule 启用网络模块 [AppService]
func (boot *ApplicationBoot) EnableNetModule() ipakku.ApplicationBoot {
if !boot.loadedNet {
boot.loadedNet = true
boot.AddModule(&appservice.AppService{})
}
return boot
}
// SetLoggerOutput 设置日志输出方式
func (boot *ApplicationBoot) SetLoggerOutput(w io.Writer) ipakku.ApplicationBoot {
logs.SetOutput(w)
return boot
}
// SetLoggerLevel 设置日志输出级别 NONE DEBUG INFO ERROR
func (boot *ApplicationBoot) SetLoggerLevel(lv logs.LoggerLeve) ipakku.ApplicationBoot {
logs.SetLoggerLevel(lv)
return boot
}
// DisableBanner 禁止Banner输出
func (boot *ApplicationBoot) DisableBanner() ipakku.ApplicationBoot {
boot.showBanner = false
return boot
}
// BootStart 启动程序&加载模块
func (boot *ApplicationBoot) BootStart() ipakku.Application {
if boot.showBanner {
boot.printBanner()
}
cwd, _ := os.Getwd()
app := boot.GetApplication()
name := app.GetParam(ipakku.PARAMKEY_APPNAME).ToString("")
logs.Infof("New application, pid: %d, name: %s, cwd: %s, instance: %s \r\n", os.Getpid(), name, cwd, strings.ToUpper(app.GetInstanceID()))
return app.LoadModules(boot.modules...)
}
// printBanner 打印一些特殊记号
func (app *ApplicationBoot) printBanner() {
bannerPath := "./.conf/banner.txt"
if !fileutil.IsFile(bannerPath) {
banner := "" +
" ,----------------, ,---------, \r\n" +
" ,-----------------------, ,\" ,\"| \r\n" +
" ,\" ,\"| ,\" ,\" | \r\n" +
" +-----------------------+ | ,\" ,\" | \r\n" +
" | .-----------------. | | +---------+ | \r\n" +
" | | | | | | -==----'| | \r\n" +
" | | I AM RUNNING! | | | | | | \r\n" +
" | | PLEASE WAIT .. | | |/----|'---= | | \r\n" +
" | | $ >_ | | | ,/|==== ooo | ; \r\n" +
" | | | | | // |(((( [33]| ,\" \r\n" +
" | '-----------------' |,\" .;'| |(((( | ,\" \r\n" +
" +-----------------------\" ;; | | |,\" \r\n" +
" /_)______________(_/ / | +---------+ \r\n" +
" ___________________________/___ ', \r\n" +
" / oooooooooooooooo .o. oooo /, \\,\"----------- \r\n" +
" / ==ooooooooooooooo==.o. ooo= // ,'\\--{)B ,\" \r\n" +
" /_==__==========__==_ooo__ooo=_/' /___________,\" \r\n"
if err := fileutil.WriteTextFile(bannerPath, banner); nil != err {
logs.Errorln(err)
} else {
fmt.Println(banner)
}
} else {
if banner, err := fileutil.ReadFileAsText(bannerPath); nil != err {
logs.Errorln(err)
} else {
fmt.Println(banner)
}
}
}
// Application 暴露loader对象的core方法
type Application struct {
ipakku.Loader
}
// LoadModule 加载模块
func (app *Application) LoadModule(mt ipakku.Module) ipakku.Application {
app.Loader.Load(mt)
return app
}
// LoadModules 加载模块
func (app *Application) LoadModules(mts ...ipakku.Module) ipakku.Application {
app.Loader.Loads(mts...)
return app
}