-
Notifications
You must be signed in to change notification settings - Fork 1
/
plugin.go
55 lines (47 loc) · 1.39 KB
/
plugin.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
package core
import "github.com/go-chi/chi"
var plugins map[string]RouterPlugin
func init() {
plugins = make(map[string]RouterPlugin)
}
// RegisterRouterPlugin is used to register a plugin
// Notice: should called by package init()
func RegisterRouterPlugin(plugin RouterPlugin) {
x := plugin.PluginInfo()
plugins[x.Name] = plugin
}
// GetRouterPluginRegistry returns a list of all registered RouterPlugin
func GetRouterPluginRegistry() []RouterPlugin {
x := make([]RouterPlugin, len(plugins))
i := 0
for name := range plugins {
x[i] = plugins[name]
i++
}
return x
}
// GetRouterPlugin return a RouterPlugin from registry that matches the key
func GetRouterPlugin(name string) (p RouterPlugin, ok bool) {
p, ok = plugins[name]
return
}
// RouterPlugin is a type that is used as a p2pNG plugin.
// p2pNG will call PluginInfo first, so PluginInfo should returns static info,
// then fill the Config and call Init,
// at last GetRouter() to mount and init database buckets.
type RouterPlugin interface {
PluginInfo() *PluginInfo
GetRouter() chi.Router
Init() error
Config() interface{}
}
// PluginInfo is a type that describe a p2pNG plugin.
// Name is should be unique, usually use module path for it.
// Prefix indicates the http service path prefix.
// Buckets indicates which database bucket will use.
type PluginInfo struct {
Name string
Version string
Prefix string
Buckets []string
}