我已经使用了下面的代码,但是我不知道如何在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': invalid character '{' looking for beginning of object key string, offset: 55, row: 3, col: 12 exit status 1
configuration.json is below。设置的环境变量为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": []
}```
发布于 2020-12-17 12:55:58
在调试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文件,无论您是将其用作库还是官方编译的映像。
https://stackoverflow.com/questions/65105112
复制