From Rails to KrakenD proxy: config generation for your routes

require "action_dispatch/routing/inspector"

begin
  namespace :krakend do
    task :routes => :environment do

      inspector = ActionDispatch::Routing::RoutesInspector.new(Rails.application.routes.routes)
      formatter = CustomRoutesFormatter.new
      routes_filter = {}

      puts inspector.format(formatter, routes_filter)
    end
  end
end

class CustomRoutesFormatter < ActionDispatch::Routing::ConsoleFormatter::Expanded
  def section_title(title)
  end

  def section(routes)
    @buffer << draw_expanded_section(routes)
  end

  private

  def draw_expanded_section(routes)
    routes.map.each_with_index do |r, i|
      path = r[:path].gsub("(.:format)", "").gsub(/:([a-z_]*)/, "{\\1}")

      <<~MESSAGE.chomp
        {
          "endpoint": "#{path}",
          "method": "#{r[:verb]}",
          "querystring_params": ["*"],
          "extra_config": {},
          "output_encoding": "no-op",
          "concurrent_calls": 1,
          "headers_to_pass": ["*"],
          "backend": [
            {
              "url_pattern": "#{path}",
              "encoding": "no-op",
              "extra_config": {},
              "sd": "static",
              "host": [
                "http://rails-app"
              ],
              "disable_host_sanitize": true
            }
          ]
        },
      MESSAGE
    end
  end
end

This approach can help you easily build krakend.json for your rails application.

KrakenD config example

What is KrakenD?

KrakenD is your self-designed REST API Gateway that feeds from your existing data services (Your APIs, legacy, cloud, IoT…) Just define visually or in a configuration file the endpoints your applications will use and the KrakenD will fetch and transform the content at your will for you.

https://www.krakend.io

Configuration example(proxy for POST, GET requests):

{
  "version": 2,
  "extra_config": {
    "github_com/devopsfaith/krakend-gologging": {
      "level": "WARNING",
      "prefix": "[KRAKEND]",
      "syslog": false,
      "stdout": true
    },
    "github_com/devopsfaith/krakend-metrics": {
      "collection_time": "60s",
      "proxy_disabled": false,
      "router_disabled": false,
      "backend_disabled": false,
      "endpoint_disabled": false,
      "listen_address": ":8090"
    },
    "github_com/devopsfaith/krakend-cors": {
      "allow_origins": [ "*" ],
      "allow_methods": [ "POST", "GET", "PUT", "DELETE" ],
      "allow_headers": [
        "Origin",
        "Authorization",
        "Content-Type",
        "Accept",
        "X-Auth-Token"
      ],
      "expose_headers": [ "Content-Length" ],
      "max_age": "12h"
    }
  },
  "timeout": "3000ms",
  "cache_ttl": "300s",
  "output_encoding": "no-op",
  "name": "api-proxy",
  "port": 80,
  "endpoints": [
    {
      "endpoint": "/",
      "extra_config": {},
      "output_encoding": "no-op",
      "concurrent_calls": 1,
      "backend": [
        {
          "url_pattern": "/",
          "encoding": "no-op",
          "extra_config": {},
          "sd": "static",
          "host": [
            "http://some-landing"
          ],
          "disable_host_sanitize": false
        }
      ]
    },
    {
      "method": "GET",
      "endpoint": "/api/v1/endpoint}",
      "extra_config": {},
      "output_encoding": "no-op",
      "concurrent_calls": 1,
      "backend": [
        {
          "method": "POST",
          "url_pattern": "/api/v1/{endpoint}",
          "encoding": "no-op",
          "extra_config": {},
          "sd": "static",
          "host": [
            "http://backend-service-getter"
          ],
          "disable_host_sanitize": false
        }
      ]
    },
    {
      "method": "POST",
      "endpoint": "/api/v1/endpoint}",
      "extra_config": {},
      "output_encoding": "no-op",
      "concurrent_calls": 1,
      "backend": [
        {
          "method": "POST",
          "url_pattern": "/api/v1/{endpoint}",
          "encoding": "no-op",
          "extra_config": {},
          "sd": "static",
          "host": [
            "http://backend-service-poster"
          ],
          "disable_host_sanitize": false
        }
      ]
    },
}