0

我使用了以下代码,但无法弄清楚如何启用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 在下面。并且设置的环境变量是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": []
}```
4

1 回答 1

0

在调试 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 文件,不管你是用它作为库还是官方编译的镜像。

于 2020-12-17T12:55:58.197 回答