forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoplugin.go
56 lines (45 loc) · 1.3 KB
/
goplugin.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
//go:build goplugin
// +build goplugin
package goplugin
import (
"errors"
"net/http"
"plugin"
)
func GetSymbol(modulePath string, symbol string) (interface{}, error) {
// try to load plugin
loadedPlugin, err := plugin.Open(modulePath)
if err != nil {
return nil, err
}
// try to lookup function symbol
funcSymbol, err := loadedPlugin.Lookup(symbol)
if err != nil {
return nil, err
}
return funcSymbol, nil
}
func GetHandler(modulePath string, symbol string) (http.HandlerFunc, error) {
funcSymbol, err := GetSymbol(modulePath, symbol)
if err != nil {
return nil, err
}
// try to cast symbol to real func
pluginHandler, ok := funcSymbol.(func(http.ResponseWriter, *http.Request))
if !ok {
return nil, errors.New("could not cast function symbol to http.HandlerFunc")
}
return pluginHandler, nil
}
func GetResponseHandler(modulePath string, symbol string) (func(rw http.ResponseWriter, res *http.Response, req *http.Request), error) {
funcSymbol, err := GetSymbol(modulePath, symbol)
if err != nil {
return nil, err
}
// try to cast symbol to real func
respPluginHandler, ok := funcSymbol.(func(rw http.ResponseWriter, res *http.Response, req *http.Request))
if !ok {
return nil, errors.New("could not cast function symbol to TykResponseHandler")
}
return respPluginHandler, nil
}