-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathomniplug.go
51 lines (43 loc) · 1021 Bytes
/
omniplug.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
package main
import (
"fmt"
"github.com/Kong/go-pdk"
)
type Data map[string]interface{}
type Omniplug struct {
Datasources []*Datasource `json:"datasources" yaml:"datasources"`
Injector Injector `json:"injector" yaml:"injector"`
}
func (r *Omniplug) Init() (err error) {
m := map[string]struct{}{}
for _, d := range r.Datasources {
if err = d.Init(); err != nil {
return
}
if _, ok := m[d.ID]; ok {
return fmt.Errorf("duplicate datasource id: %s", d.ID)
}
m[d.ID] = struct{}{}
}
return
}
func (r *Omniplug) Access(kong *pdk.PDK) {
data, err := r.ResolvData(kong)
if err != nil {
kong.Response.Exit(500, []byte(err.Error()), nil)
}
err = r.Injector.Inject(kong, data)
if err != nil {
kong.Response.Exit(500, []byte(err.Error()), nil)
}
}
func (r *Omniplug) ResolvData(kong *pdk.PDK) (data Data, err error) {
data = map[string]interface{}{}
for _, d := range r.Datasources {
data[d.ID], err = d.ToData(kong, data)
if err != nil {
return data, err
}
}
return
}