/
plugin.go
168 lines (139 loc) · 3.37 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
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
package plugin
import (
"encoding/json"
"fmt"
"sort"
"sync"
"github.com/riposo/riposo/pkg/api"
"go.uber.org/multierr"
)
// A Plugin interface.
type Plugin interface {
// IDs must be lowercase and only contain alphanumeric characters and hyphens.
ID() string
// Meta exposes plugin metadata as key-value pairs.
// Commonly used keys are: "url" and "description".
Meta() map[string]interface{}
// Init callback is called on init.
Init(*api.Routes) error
// Close callback is called on shutdown.
Close() error
}
type simple struct {
id string
meta map[string]interface{}
init func(*api.Routes) error
close func() error
}
// New inits a simple plugin.
func New(id string, meta map[string]interface{}, init func(*api.Routes) error, close func() error) Plugin {
return &simple{
id: id,
meta: meta,
init: init,
close: close,
}
}
func (s *simple) ID() string { return s.id }
func (s *simple) Meta() map[string]interface{} { return s.meta }
func (s *simple) Init(rts *api.Routes) error {
if s.init != nil {
return s.init(rts)
}
return nil
}
func (s *simple) Close() error {
if s.close != nil {
return s.close()
}
return nil
}
// --------------------------------------------------------------------
// Set contains the set of loaded plugins.
type Set struct {
pins []Plugin
meta map[string]map[string]interface{}
}
// Close closes all plugins.
func (s *Set) Close() error {
var err error
for _, pin := range s.pins {
err = multierr.Append(err, pin.Close())
}
return err
}
// MarshalJSON encodes the set as JSON.
func (s *Set) MarshalJSON() ([]byte, error) {
if s.meta == nil {
return []byte{'{', '}'}, nil
}
return json.Marshal(s.meta)
}
// --------------------------------------------------------------------
var (
registry = make(map[string]Plugin)
registryMu sync.RWMutex
)
// Register registers a new plugin.
// It will panic if multiple plugin are registered under the same ID.
func Register(pin Plugin) {
registryMu.Lock()
defer registryMu.Unlock()
id := pin.ID()
if id == "" {
panic("plugin ID cannot be blank")
}
for _, c := range id {
if (c < 'a' || c > 'z') && (c < '0' || c > '9') && c != '-' {
panic("plugin ID '" + id + "' is invalid")
}
}
if _, ok := registry[id]; ok {
panic("plugin " + id + " is already registered")
}
registry[id] = pin
}
// EachMeta iterates over each ID and meta.
func EachMeta(iter func(id string, meta map[string]interface{})) {
registryMu.RLock()
defer registryMu.RUnlock()
ids := make([]string, 0, len(registry))
for id := range registry {
ids = append(ids, id)
}
sort.Strings(ids)
for _, id := range ids {
pin := registry[id]
iter(id, pin.Meta())
}
}
// Init initializes registered plugins.
func Init(rts *api.Routes, enabled []string) (*Set, error) {
registryMu.RLock()
defer registryMu.RUnlock()
set := &Set{
pins: make([]Plugin, 0, len(enabled)),
meta: make(map[string]map[string]interface{}, len(enabled)),
}
for _, id := range enabled {
// skip if already enabled
if _, ok := set.meta[id]; ok {
continue
}
// check if available
pin, ok := registry[id]
if !ok {
_ = set.Close()
return nil, fmt.Errorf("plugin %q is not available", id)
}
// enable plugin
set.pins = append(set.pins, pin)
set.meta[id] = pin.Meta()
// init plugin
if err := pin.Init(rts); err != nil {
_ = set.Close()
return nil, err
}
}
return set, nil
}