当前位置: > > > > krakenD用作go库时如何启用灵活配置
来源:stackoverflow
2024-04-21 15:00:28
0浏览
收藏
怎么入门Golang编程?需要学习哪些知识点?这是新手们刚接触编程时常见的问题;下面就来给大家整理分享一些知识点,希望能够给初学者一些帮助。本篇文章就来介绍《krakenD用作go库时如何启用灵活配置》,涉及到,有需要的可以收藏一下
问题内容
我使用了下面的代码,但我无法弄清楚如何启用 https://www.krakend.io/docs/configuration/flexible-config/ 文档中提到的 krakend 灵活配置。 尝试了多种方法,在运行代码之前设置环境变量并直接导入 github.com/devopsfaith/krakend-flexibleconfig。但对我来说没有任何作用。 谁能帮我解决这个问题吗?
package main import ( "flag" "log" "os" "github.com/devopsfaith/krakend/config" "github.com/devopsfaith/krakend/logging" "github.com/devopsfaith/krakend/proxy" "github.com/devopsfaith/krakend/router/gin" ) func main() { port := flag.int("p", 0, "port of the service") loglevel := flag.string("l", "error", "logging level") debug := flag.bool("d", false, "enable the debug") configfile := flag.string("c", "/etc/krakend/configuration.json", "path to the configuration filename") flag.parse() parser := config.newparser() serviceconfig, err := parser.parse(*configfile) if err != nil { log.fatal("error:", err.error()) } serviceconfig.debug = serviceconfig.debug || *debug if *port != 0 { serviceconfig.port = *port } logger, _ := logging.newlogger(*loglevel, os.stdout, "[krakend]") routerfactory := gin.defaultfactory(proxy.defaultfactory(logger), logger) routerfactory.new().run(serviceconfig) }
错误消息是:'configuration.json':无效字符'{'查找对象键字符串的开头,偏移量:55,行:3,列:12退出状态1
configuration.json 如下。设置的环境变量为 fc_enable=1 \ fc_settings="$pwd/config/settings"
"version": 2, "name": "api gateway", "port": {{ .service.port }}, "cache_ttl": "3600s", "timeout": "10s", "github_com/devopsfaith/krakend-cors": { "allow_origins": [ "http://192.168.99.100:3000", "http://localhost:3000", "http://9.30.161.212:30077", "http://9.30.161.212:30072", "http://localhost:8080" ], "allow_methods": [ "POST", "GET", "PUT" ], "allow_headers": [ "Origin", "Authorization", "Content-Type", "refresh-token" ], "expose_headers": [ "Content-Length" ], "max_age": "12h" }, "extra_config": { {{ marshal .service.extra_config }} }, "endpoints": [] }```
解决方案
在调试krakend的灵活配置时,添加fc_out
变量以查看编译后的模板。例如:
FC_ENABLE=1 \ FC_OUT=the-compiled-file.json \ FC_PARTIALS="$PWD/config/partials" \ FC_SETTINGS="$PWD/config/settings/$TARGET_ENV" \ FC_TEMPLATES="$PWD/config/templates" \ krakend check -c krakend.tmpl
然后您将能够打开此 the-compiled-file.json
并查看任何语法错误。只是生成的文件不是有效的 json 文件,无论您是将其用作库还是官方编译的映像。
终于介绍完啦!小伙伴们,这篇关于《krakenD用作go库时如何启用灵活配置》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~公众号也会发布Golang相关知识,快来关注吧!