Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test load folder files #23

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Test load folder files #23

wants to merge 2 commits into from

Conversation

dhontecillas
Copy link
Contributor

@dhontecillas dhontecillas commented Aug 22, 2024

This is a showcase of how to use flexible config to use $ref instead of template to collect endpoint definitions from a set of files in the templates folder.

In order to be used with ref, the files MUST have a valid JSON object inside, somthing like this:

{
    "endpoints": [
        {
            "endpoint": "/public/from_a_file/first/{{.futurama.zoidberg}}",
            "backend": [
                {
                    "url_pattern": "/static/hotels/1.json"
                }
            ]
        },
        {
            "endpoint": "/public/from_a_file/second/{{.futurama.bender}}",
            "backend": [
                {
                    "url_pattern": "/static/hotels/1.json"
                }
            ]
        }
    ]
}

Since krakend cannot scan a folder and automatically add refs, we need to put into settings a list of the files that contains endpoints, and the number of endpoints it contains (this can be generated with an script, as the one provided at the end).

So, we want to have some settings file like this:

{
  "gatherendpoints": [
    {
      "name": "ref_collection_a.json",
      "num": 2
    },
    {
      "name": "ref_collection_b.json",
      "num": 3
    }
  ]
}

and in the endpoints definitions we can use a template block like this one:

 "endpoints": [
      {{ range .gatherendpoints }}
          {{$filename := .name}}

          {{ range $n, $k := until (int .num) }}
          {
            "$ref": "./templates/{{ $filename }}#/endpoints/{{ $n }}"
          }
          ,
          {{ end }}
      {{ end }}
      {{ template "endpoints_req_resp_transformations.json" . }},
      {{ template "endpoints_authentication.json" . }},
      {{ template "endpoints_traffic_management.json" . }},
      {{ template "endpoints_services_connectivity.json" . }}
  ],

⚠️ be careful with the , above, because if in the block there is only the range block, the last element should not be followed by a ,

Check the PR for more details files to have a more detailed view, and an example of gathering endopoints from a couple of files.

Script to gather the file names, and the number of endpoints they have defined inside:

#/bin/bash

export ENDPOINTS_FOLDER=./
export OUTPUT_FILE=./output.json

echo -e '{\n  "endpoints": [' > $OUTPUT_FILE

export COMMA='    '

# TODO: change here the pattern of the files you want to use:
for FILE in $ENDPOINTS_FOLDER/*.json; do 
    export NUM=$(cat $FILE | jq --raw-output '.endpoints | length')
    export F=$(echo -n "$FILE" | sed 's/\.\/\///g')
    if [ -n "${NUM}" ]; then
        echo -n "$COMMA" >> $OUTPUT_FILE
        export COMMA='   ,'
        echo -n '{ "name": "' >> $OUTPUT_FILE 
        echo -n "$F" >> $OUTPUT_FILE
        echo -n '", "num": "' >> $OUTPUT_FILE
        echo -n "$NUM" >> $OUTPUT_FILE
        echo '"}' >> $OUTPUT_FILE
    fi
done

echo -e "  ]\n}" >> $OUTPUT_FILE

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant