-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.go
87 lines (69 loc) · 1.52 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package nako
import (
"github.com/gookit/cache"
"github.com/gookit/config/v2"
"github.com/gookit/config/v2/yaml"
"github.com/gookit/event"
"github.com/gookit/rux"
"github.com/gookit/slog"
"github.com/gookit/view"
)
// Application instance
type Application struct {
*event.Manager
Name string
params map[string]interface{}
BootLoaders []BootLoader
// components
components map[string]interface{}
// components
View *view.Renderer
Cache cache.Cache
Config *config.Config
Router *rux.Router
Logger *slog.Logger
}
// NewApp new application instance
func NewApp() *Application {
app := &Application{
params: make(map[string]interface{}),
// services
Router: rux.New(),
Config: config.New("lako"),
// events
Manager: event.NewManager("gweb"),
}
// add yaml support
app.Config.AddDriver(yaml.Driver)
return app
}
// Run the application
// Usage:
// app.Run()
func (a *Application) Run() {
a.MustFire(OnBeforeBoot, event.M{"app": a})
a.bootstrap()
a.MustFire(OnAfterBoot, event.M{"app": a})
}
// Bootstrap application init.
func (a *Application) bootstrap() {
for _, loader := range a.BootLoaders {
if err := loader.Boot(a); err != nil {
panic(err)
}
}
if a.Name == "" {
a.Name = a.Config.String("name", "")
}
}
// Set component to app.components
func (a *Application) Set(name string, val interface{}) {
a.components[name] = val
}
// Get component from app.components
func (a *Application) Get(name string) interface{} {
if val, ok := a.components[name]; ok {
return val
}
return nil
}