【问题标题】:How to enable flexible configuration of krakenD when used as a go library用作 go 库时如何启用 krakenD 的灵活配置
【发布时间】:2020-12-02 09:12:22
【问题描述】:

我使用了以下代码,但无法弄清楚如何启用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": []
}```

【问题讨论】:

  • 配置.json 文件中的语法看起来有问题。介意在错误提及的地方张贴行吗? (117,所以 115-119 会很好:))
  • @Aliics 我已经更新了 configuration.json 文件内容。请看一下

标签: go krakend


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2022-08-23
    • 2018-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多