How to enable flexible configuration of krakenD when used as a go library

2.3k views Asked by At

I have used the below code and I am unable to figure out how to enable the krakenD flexible configuration mentioned here in https://www.krakend.io/docs/configuration/flexible-config/ the documentation. Tried several ways by setting env variables before running the code and directly importing github.com/devopsfaith/krakend-flexibleconfig. But nothing worked for me. Can anyone please help me on this?

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)
}

The error message is: 'configuration.json': invalid character '{' looking for beginning of object key string, offset: 55, row: 3, col: 12 exit status 1 configuration.json is below. And the environment variables set are 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": []
}```
1

There are 1 answers

0
alo On

When debugging KrakenD's flexible configuration add the FC_OUT variable to see the compiled template. For instance:

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

Then you will be able to open this the-compiled-file.json and see any syntax errors. It's just that the resulting file is not a valid JSON file, no matter if you are using it as a library or the official compiled image.