【发布时间】:2020-09-10 08:59:17
【问题描述】:
我用 GO 语言为 KrakenD Gateway 编写了一个简单的服务器插件。它会在路由开始之前注入代码。
我正在尝试从我的krakend.json 读取配置设置,以便在启动时将设置传递给我的插件。下面我以设置mysetting为例(请看cmets之间的代码HERE THE READ OF MY SETTING STARTS/ENDS)。
如何使用插件内部配置中mysetting 的值?
这是我的krakend.json 配置文件,在启动时用作-c 参数:
{
"version": 2,
"timeout": "3000ms",
"cache_ttl": "300s",
"output_encoding": "json",
"name": "Gateway",
"plugin": {
"pattern": ".so",
"folder": "./plugins/"
},
"extra_config": {
"github_com/devopsfaith/krakend/transport/http/server/handler": {
"name": "testPlugin",
"mysetting": "Hello"
}
},
"endpoints": [
...
],
"port": 9010,
}
下面是 registerHandlers 函数的代码:
func (r registerer) registerHandlers(ctx context.Context, extra map[string]interface{}, _ http.Handler) (http.Handler, error) {
// check the passed configuration and initialize the plugin
name, ok := extra["name"].(string)
if !ok {
return nil, errors.New("wrong config")
}
if name != string(r) {
return nil, fmt.Errorf("unknown register %s", name)
}
//************ HERE THE READ OF MY SETTING STARTS ************
setting, ok := extra["mysetting"].(string)
if !ok {
return nil, errors.New("mysetting missing in config")
}
fmt.Printf("PLUGIN: My custom setting: %s\n", setting)
//************ HERE THE READ OF MY SETTING ENDS ************
// return the actual handler wrapping or your custom logic so it can be used as a replacement for the default http handler
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
fmt.Fprintf(w, "%s, %q", setting, html.EscapeString(req.URL.Path))
}), nil
}
【问题讨论】:
-
请停止一遍又一遍地转发同一个问题,改为编辑它并要求重新打开它。如果你继续这样做,你最终会被禁止。而且您需要提供更多详细信息,“它不起作用”没有帮助。