...
1
9
10 package config
11
12 import (
13 "errors"
14 "plugin"
15
16 "gitlab.hexacode.org/go-libs/hctypes"
17 )
18
19 var (
20 plugins map[string]*plugin.Plugin = map[string]*plugin.Plugin{}
21 )
22
23 type ConfigCallback struct {
24 Plugin string `json:"plugin,omitempty"`
25 Function string `json:"function,omitempty"`
26 plugin *plugin.Plugin `json:"-"`
27 function func(*ConfigContext) (int, hctypes.Dict, *ReturnHTTPError) `json:"-"`
28 }
29
30 func (config *Config) LoadPlugin() error {
31 apis := config.APIs
32 if apis != nil {
33 for _, api := range apis {
34 for _, param := range api.Params {
35 if param.Name == "" {
36 panic(errors.New("api.params[].name is required"))
37 }
38 }
39 callback := api.Callback
40
41 if callback == nil {
42 api.Callback = &ConfigCallback{
43 function: DefaultCallbackAPIFunction,
44 }
45 } else {
46 plg, err := plugin.Open(callback.Plugin)
47 if err != nil {
48 panic(err)
49 return err
50 }
51
52 callback.plugin = plg
53 function, err := plg.Lookup(callback.Function)
54 if err != nil {
55 panic(err)
56 return err
57 }
58 callback.function = function.(func(*ConfigContext) (int, hctypes.Dict, *ReturnHTTPError))
59 }
60 }
61 }
62
63 templates := config.Templates
64 if templates != nil {
65 for _, template := range templates {
66 callback := template.Callback
67
68 if callback == nil {
69 template.Callback = &ConfigTemplateCallback{
70 function: DefaultCallbackTemplateFunction,
71 }
72 } else {
73 plg, err := plugin.Open(callback.Plugin)
74 if err != nil {
75 panic(err)
76 return err
77 }
78
79 callback.plugin = plg
80 function, err := plg.Lookup(callback.Function)
81 if err != nil {
82 panic(err)
83 return err
84 }
85 callback.function = function.(func(*ConfigTemplateContext) hctypes.Dict)
86 }
87 }
88 }
89
90 return nil
91 }
92
View as plain text