/
application.go
47 lines (38 loc) · 932 Bytes
/
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
package application
import (
log "log"
"runtime/debug"
)
//Application is your service
type Application struct {
//Context
context *ApplicationContext
//When application start
onStart func(*ApplicationContext)
//When appliction stop
onStop func(*ApplicationContext)
}
//Setup application context
func (self *Application) SetContext(c ApplicationContext) {
self.context = &c
}
//Setup application start event
func (self *Application) SetOnStart(c func(*ApplicationContext)) {
self.onStart = c
}
//Setup application stop event
func (self *Application) SetOnStop(c func(*ApplicationContext)) {
self.onStop = c
}
//Run application
func (self *Application) Run(c func(*ApplicationContext)) {
defer func() {
if r := recover(); r != nil {
log.Println("Application Uncaught Error Occour:", r.(error).Error())
debug.PrintStack()
}
}()
self.onStart(self.context)
c(self.context)
self.onStop(self.context)
}