-
Notifications
You must be signed in to change notification settings - Fork 5
/
application.go
420 lines (332 loc) · 9.15 KB
/
application.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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
package pgo2
import (
"flag"
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"sync"
"github.com/pinguo/pgo2/config"
"github.com/pinguo/pgo2/iface"
"github.com/pinguo/pgo2/logs"
"github.com/pinguo/pgo2/util"
)
func init() {
// 预先声明系统参数
for _,vFlag:=range globalParams{
_ = flag.String(vFlag.Name, vFlag.DefValue, vFlag.Usage)
}
}
func NewApp() *Application {
exeBase := filepath.Base(os.Args[0])
exeExt := filepath.Ext(os.Args[0])
exeDir := filepath.Dir(os.Args[0])
app := &Application{
mode: ModeWeb,
env: "",
name: strings.TrimSuffix(exeBase, exeExt),
components: make(map[string]interface{}),
objects: make(map[string]iface.IObject),
}
app.basePath = app.genBasePath(exeDir)
app.Init(os.Args)
return app
}
type Application struct {
mode string // running mode, WEB or CMD
env string // running env, eg. develop/online/testing-dev/testing-qa
name string // App name
basePath string // base path of App
runtimePath string // runtime path for log etc.
publicPath string // public path for web assets
viewPath string // path for view template
config config.IConfig
container *Container
server *Server
router *Router
log *logs.Log
status iface.IStatus
i18n iface.II18n
view iface.IView
stopBefore *StopBefore // 服务停止前执行 [{"obj":"func"}]
components map[string]interface{}
objects map[string]iface.IObject
lock sync.RWMutex
args map[string]string
}
func (app *Application) initArgs(osArgs []string) map[string]string {
app.args = make(map[string]string)
nArg := len(osArgs)
for k, cmd := range osArgs {
if k == 0 {
continue
}
if strings.Index(cmd, "--") != 0 {
continue
}
value := ""
name := strings.Replace(cmd, "--", "", 1)
if strings.Index(name, "=") > 0 {
tmpName := strings.Split(name, "=")
name = tmpName[0]
value = tmpName[1]
} else {
nextK := k + 1
if nextK < nArg {
nextV := osArgs[nextK]
if strings.Index(nextV, "--") != 0 {
value = nextV
}
}
}
app.args[name] = value
}
return app.args
}
func (app *Application) HasArg(name string) bool {
_, has := app.args[name]
return has
}
func (app *Application) Arg(name string) string {
if v, has := app.args[name]; has {
return v
}
return ""
}
func (app *Application) help() bool {
return app.HasArg("help")
}
func (app *Application) Init(osArgs []string) {
args := app.initArgs(osArgs)
// overwrite running env
if env, has := args["env"]; has && len(env) > 0 {
app.env = env
}
// overwrite running mode
if _, has := args["cmd"]; has {
app.mode = ModeCmd
}
// overwrite base path
if base, has := args["base"]; has && len(base) > 0 {
app.basePath, _ = filepath.Abs(base)
}
// set basic path alias
type dummy struct{}
pkgPath := reflect.TypeOf(dummy{}).PkgPath()
SetAlias("@app", app.basePath)
SetAlias("@pgo2", strings.TrimPrefix(pkgPath, VendorPrefix))
// initialize config object
app.config = config.New(app.basePath, app.env)
// initialize container object
enablePool, _ := app.config.Get("app.container.enablePool").(string)
app.container = NewContainer(enablePool)
// initialize server object
svrConf, _ := app.config.Get("app.server").(map[string]interface{})
app.server = NewServer(svrConf)
// overwrite appName
if name := app.config.GetString("app.name", ""); len(name) > 0 {
app.name = name
}
// overwrite GOMAXPROCS
if n := app.config.GetInt("app.GOMAXPROCS", 0); n > 0 {
runtime.GOMAXPROCS(n)
}
// set runtime path
runtimePath := app.config.GetString("app.runtimePath", "@app/runtime")
app.runtimePath, _ = filepath.Abs(GetAlias(runtimePath))
SetAlias("@runtime", app.runtimePath)
// set public path
publicPath := app.config.GetString("app.publicPath", "@app/public")
app.publicPath, _ = filepath.Abs(GetAlias(publicPath))
SetAlias("@public", app.publicPath)
// set view path
viewPath := app.config.GetString("app.viewPath", "@app/view")
app.viewPath, _ = filepath.Abs(GetAlias(viewPath))
SetAlias("@view", app.viewPath)
// create runtime directory if not exists
if _, e := os.Stat(app.runtimePath); os.IsNotExist(e) {
if e := os.MkdirAll(app.runtimePath, 0755); e != nil {
panic(fmt.Sprintf("failed to create %s, %s", app.runtimePath, e))
}
}
}
func (app *Application) genBasePath(exeDir string) string {
basePath, _ := filepath.Abs(filepath.Join(exeDir, ".."))
return basePath
}
// Mode running mode, web:1, cmd:2
func (app *Application) Mode() string {
return app.mode
}
// CmdMode
func (app *Application) CmdMode() bool {
return app.mode == ModeCmd
}
// Env running env
func (app *Application) Env() string {
return app.env
}
// Name appName, default is executable name
func (app *Application) Name() string {
return app.name
}
// BasePath base path, default is parent of executable
func (app *Application) BasePath() string {
return app.basePath
}
// RuntimePath runtime path, default is @app/runtime
func (app *Application) RuntimePath() string {
return app.runtimePath
}
// PublicPath public path, default is @app/public
func (app *Application) PublicPath() string {
return app.publicPath
}
// ViewPath view path, default is @app/view
func (app *Application) ViewPath() string {
return app.viewPath
}
// Config config component
func (app *Application) Config() config.IConfig {
return app.config
}
// Container container component
func (app *Application) Container() *Container {
return app.container
}
// Server server component
func (app *Application) Server() *Server {
return app.server
}
// Router router component
func (app *Application) Router() *Router {
if app.router == nil {
app.router = NewRouter(app.componentConf("router"))
}
return app.router
}
// Log log component
func (app *Application) Log() *logs.Log {
if app.log == nil {
app.log = logs.NewLog(app.RuntimePath(), app.componentConf("log"))
}
return app.log
}
// Status status component
func (app *Application) Status() iface.IStatus {
if app.status == nil {
app.status = NewStatus(app.componentConf("status"))
}
return app.status
}
// I18n i18n component
func (app *Application) I18n() iface.II18n {
if app.i18n == nil {
app.i18n = NewI18n(app.componentConf("i18n"))
}
return app.i18n
}
// View view component
func (app *Application) View() iface.IView {
if app.view == nil {
app.view = NewView(app.componentConf("view"))
}
return app.view
}
// StopBefore stopBefore component
func (app *Application) StopBefore() *StopBefore {
if app.stopBefore == nil {
app.stopBefore = NewStopBefore()
}
return app.stopBefore
}
// SetStatus set status component
func (app *Application) SetStatus(status iface.IStatus) {
app.status = status
}
// SetI18n set i18n component
func (app *Application) SetI18n(i18n iface.II18n) {
app.i18n = i18n
}
// SetView set view component
func (app *Application) SetView(view iface.IView) {
app.view = view
}
// SetView set view component
func (app *Application) SetConfig(config config.IConfig) {
app.config = config
}
// component conf by id
func (app *Application) componentConf(id string) map[string]interface{} {
conf := app.config.Get("app.components." + id)
if conf == nil {
return nil
}
if retConf, ok := conf.(map[string]interface{}); ok {
for k, v := range retConf {
if vv, ok := v.(string); ok == true {
retConf[k] = GetAlias(vv)
}
}
return retConf
}
return nil
}
// Get get component by id
func (app *Application) Component(id string, funcName iface.IComponentFunc, params ...map[string]interface{}) interface{} {
if _, ok := app.components[id]; !ok {
confConfig := app.componentConf(id)
if len(params) > 0 {
if confConfig == nil {
confConfig = make(map[string]interface{})
}
util.MapMerge(confConfig, params...)
}
obj, err := funcName(confConfig)
if err != nil {
panic("Component " + id + " err:" + err.Error())
}
app.setComponent(id, obj)
}
app.lock.RLock()
defer app.lock.RUnlock()
return app.components[id]
}
// Get get pool class object. name is class name, ctx is context,
func (app *Application) GetObjPool(name string, ctx iface.IContext, params ...interface{}) iface.IObject {
if name := GetAlias(name); len(name) > 0 {
return app.container.Get(name, ctx, params...).Interface().(iface.IObject)
}
panic("unknown class: " + name)
}
// Get get single class object. name is class name, ctx is context,
func (app *Application) GetObjSingle(name string, funcName iface.IObjSingleFunc, param ...interface{}) iface.IObject {
if _, ok := app.objects[name]; !ok {
app.setObject(name, funcName(param...))
}
app.lock.RLock()
defer app.lock.RUnlock()
return app.objects[name]
}
func (app *Application) setObject(name string, object iface.IObject, force ...bool) {
force = append(force, false)
app.lock.Lock()
defer app.lock.Unlock()
// avoid repeated loading
if _, ok := app.objects[name]; ok && force[0] == false {
return
}
app.objects[name] = object
}
func (app *Application) setComponent(id string, component interface{}, force ...bool) {
force = append(force, false)
app.lock.Lock()
defer app.lock.Unlock()
// avoid repeated loading
if _, ok := app.components[id]; ok && force[0] == false {
return
}
app.components[id] = component
}