forked from GoAdminGroup/go-admin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.go
76 lines (63 loc) · 1.93 KB
/
engine.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
// Copyright 2018 ChenHonggui. All rights reserved.
// Use of this source code is governed by a MIT style
// license that can be found in the LICENSE file.
package engine
import (
"github.com/chenhg5/go-admin/adapter"
"github.com/chenhg5/go-admin/modules/config"
"github.com/chenhg5/go-admin/plugins"
"github.com/chenhg5/go-admin/template/types"
)
// Engine is the core components of goAdmin. It has two attributes.
// PluginList is an array of plugin. Adapter is the adapter of
// web framework context and goAdmin context. The relationship of adapter and
// plugin is that the adapter use the plugin which contains routers and
// controller methods to inject into the framework entity and make it work.
type Engine struct {
PluginList []plugins.Plugin
Adapter adapter.WebFrameWork
}
// Default return the default engine instance.
func Default() *Engine {
return &Engine{
Adapter: DefaultAdapter,
}
}
// Use enable the adapter.
func (eng *Engine) Use(router interface{}) error {
if eng.Adapter == nil {
panic("adapter is nil, import the default adapter or use AddAdapter method add the adapter")
}
return eng.Adapter.Use(router, eng.PluginList)
}
// AddPlugins add the plugins and initialize them.
func (eng *Engine) AddPlugins(plugs ...plugins.Plugin) *Engine {
for _, plug := range plugs {
plug.InitPlugin()
}
eng.PluginList = append(eng.PluginList, plugs...)
return eng
}
// AddConfig set the global config.
func (eng *Engine) AddConfig(cfg config.Config) *Engine {
config.Set(cfg)
return eng
}
// AddAdapter add the adapter of engine.
func (eng *Engine) AddAdapter(ada adapter.WebFrameWork) *Engine {
eng.Adapter = ada
return eng
}
var DefaultAdapter adapter.WebFrameWork
func Register(ada adapter.WebFrameWork) {
if ada == nil {
panic("adapter is nil")
}
DefaultAdapter = ada
}
func Content(ctx interface{}, panel types.GetPanel) {
if DefaultAdapter == nil {
panic("adapter is nil")
}
DefaultAdapter.Content(ctx, panel)
}