forked from luraproject/lura
/
main.go
57 lines (48 loc) · 1.13 KB
/
main.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
package main
import (
"bytes"
"fmt"
"time"
"github.com/devopsfaith/krakend/config"
"github.com/devopsfaith/krakend/plugin"
)
const pluginName = "supu"
func main() {
register := plugin.NewRegister()
fmt.Println(plugin.Load(config.Plugin{
Folder: "../plugins/",
Pattern: ".so",
}, register))
time.Sleep(5 * time.Second)
checkDecoder(register)
checkExternal(register)
}
func checkDecoder(register *plugin.Register) {
decoderFactory := register.Decoder.Get(pluginName)
decoder := decoderFactory(false)
d := bytes.NewBufferString("something")
m := &map[string]interface{}{}
if err := decoder(d, m); err != nil {
fmt.Println("error:", err.Error())
return
}
fmt.Println("encoding ok!")
}
func checkExternal(register *plugin.Register) {
n1Register, ok := register.External.Get("namespace1")
if !ok {
fmt.Println("namespace1 not registered")
return
}
v, ok := n1Register.Get(pluginName)
if !ok {
fmt.Println(pluginName, "not registered into namespace1")
return
}
f, ok := v.(func(int) int)
if !ok {
fmt.Println("unexpected registerd component into namespace1", v)
return
}
fmt.Println("f(2) =", f(2))
}