-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathApplication.go
294 lines (242 loc) · 7.27 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
package pgo
import (
"flag"
"fmt"
"os"
"path/filepath"
"reflect"
"runtime"
"strings"
"sync"
)
// Application the pgo app,
// initialization steps:
// 1. import pgo: pgo.init()
// 2. customize app: optional
// 3. pgo.Run(): serve
//
// configuration:
// name: "app-name"
// GOMAXPROCS: 2
// runtimePath: "@app/runtime"
// publicPath: "@app/public"
// viewPath: "@viewPath"
// container: {}
// server: {}
// components: {}
type Application struct {
mode int // running mode, WEB or CMD
env string // running env, eg. develop/online/testing-dev/testing-qa
name string // application name
basePath string // base path of application
runtimePath string // runtime path for log etc.
publicPath string // public path for web assets
viewPath string // path for view template
config *Config
container *Container
server *Server
components map[string]interface{}
lock sync.RWMutex
router *Router
log *Log
status *Status
i18n *I18n
view *View
stopBefore *StopBefore // 服务停止前执行 [{"obj":"func"}]
}
func (app *Application) getBasePath(exeDir string) string {
basePath := os.Getenv("PgoTestAppBasePath")
if basePath == "" {
basePath, _ = filepath.Abs(filepath.Join(exeDir, ".."))
}
return basePath
}
func (app *Application) Construct() {
exeBase := filepath.Base(os.Args[0])
exeExt := filepath.Ext(os.Args[0])
exeDir := filepath.Dir(os.Args[0])
app.env = DefaultEnv
app.mode = ModeWeb
app.name = strings.TrimSuffix(exeBase, exeExt)
app.basePath = app.getBasePath(exeDir)
app.config = &Config{}
app.container = &Container{}
app.server = &Server{}
app.components = make(map[string]interface{})
app.stopBefore = &StopBefore{}
}
func (app *Application) Init() {
env := flag.String("env", "", "set running env, eg. --env online")
cmd := flag.String("cmd", "", "set running cmd, eg. --cmd /foo/bar")
base := flag.String("base", "", "set base path, eg. --base /base/path")
flag.Parse()
// overwrite running env
if len(*env) > 0 {
app.env = *env
}
// overwrite running mode
if len(*cmd) > 0 {
app.mode = ModeCmd
}
// overwrite base path
if 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("@pgo", strings.TrimPrefix(pkgPath, VendorPrefix))
// initialize config object
ConstructAndInit(app.config, nil)
// initialize container object
cntConf, _ := app.config.Get("app.container").(map[string]interface{})
ConstructAndInit(app.container, cntConf)
// initialize server object
svrConf, _ := app.config.Get("app.server").(map[string]interface{})
ConstructAndInit(app.server, svrConf)
// overwrite app name
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)
// set core components
for id, class := range app.coreComponents() {
key := fmt.Sprintf("app.components.%s.class", id)
app.config.Set(key, class)
}
// 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))
}
}
}
// GetMode get running mode, web:1, cmd:2
func (app *Application) GetMode() int {
return app.mode
}
// GetEnv get running env
func (app *Application) GetEnv() string {
return app.env
}
// GetName get app name, default is executable name
func (app *Application) GetName() string {
return app.name
}
// GetBasePath get base path, default is parent of executable
func (app *Application) GetBasePath() string {
return app.basePath
}
// GetRuntimePath get runtime path, default is @app/runtime
func (app *Application) GetRuntimePath() string {
return app.runtimePath
}
// GetPublicPath get public path, default is @app/public
func (app *Application) GetPublicPath() string {
return app.publicPath
}
// GetViewPath get view path, default is @app/view
func (app *Application) GetViewPath() string {
return app.viewPath
}
// GetConfig get config component
func (app *Application) GetConfig() *Config {
return app.config
}
// GetContainer get container component
func (app *Application) GetContainer() *Container {
return app.container
}
// GetServer get server component
func (app *Application) GetServer() *Server {
return app.server
}
// GetRouter get router component
func (app *Application) GetRouter() *Router {
if app.router == nil {
app.router = app.Get("router").(*Router)
}
return app.router
}
// GetLog get log component
func (app *Application) GetLog() *Log {
if app.log == nil {
app.log = app.Get("log").(*Log)
}
return app.log
}
// GetStatus get status component
func (app *Application) GetStatus() *Status {
if app.status == nil {
app.status = app.Get("status").(*Status)
}
return app.status
}
// GetI18n get i18n component
func (app *Application) GetI18n() *I18n {
if app.i18n == nil {
app.i18n = app.Get("i18n").(*I18n)
}
return app.i18n
}
// GetView get view component
func (app *Application) GetView() *View {
if app.view == nil {
app.view = app.Get("view").(*View)
}
return app.view
}
// GetStopBefore get stopBefore component
func (app *Application) GetStopBefore() *StopBefore {
return app.stopBefore
}
// Get get component by id
func (app *Application) Get(id string) interface{} {
if _, ok := app.components[id]; !ok {
app.loadComponent(id)
}
app.lock.RLock()
defer app.lock.RUnlock()
return app.components[id]
}
func (app *Application) loadComponent(id string) {
app.lock.Lock()
defer app.lock.Unlock()
// avoid repeated loading
if _, ok := app.components[id]; ok {
return
}
conf := app.config.Get("app.components." + id)
if conf == nil {
panic("component not found: " + id)
}
app.components[id] = CreateObject(conf)
}
func (app *Application) coreComponents() map[string]string {
return map[string]string{
"router": "@pgo/Router",
"log": "@pgo/Log",
"status": "@pgo/Status",
"i18n": "@pgo/I18n",
"view": "@pgo/View",
"gzip": "@pgo/Gzip",
"file": "@pgo/File",
"http": "@pgo/Client/Http/Client",
}
}