Sha256: 655bc62f1a6e1d3ab91d4be2f90fe14af60a17695a5efe6a0e77832dfc8fb392

Contents?: true

Size: 1.35 KB

Versions: 1

Compression:

Stored size: 1.35 KB

Contents

require "rack/file"
require "rack/utils"

module SwaggerUiWrapper
  module Rack
    class MiddleWare
      def initialize(app, options)
        @app = app
        @api_url_base = options[:api_url_base] # http://test-server.com
        @path = options[:path] || '/swagger/'
        swagger_path = File.expand_path('../../../vendor/assets', File.dirname(__FILE__))
        @swagger_assets_handler = ::Rack::Static.new @app, root: swagger_path,
                                                          urls: ['/swagger'] #,
                                                          # index: 'v1.html' # switched to redirect instead of render
      end

      def call(env)
        if env['REQUEST_PATH'] && env['REQUEST_PATH'].start_with?(@path)
          if env['REQUEST_PATH'] == '/swagger/'
            [302, {'Content-Type' => 'text',
                   'Location' => (@api_url_base || (env['rack.url_scheme'] + "://" + env["HTTP_HOST"].to_s)) + @path + 'v1.html',
                   'Content-Type' => 'text/html; charset=UTF-8'}, ['302 found']]
          else
            # some hack for GRAPE:
            # _env = env.merge("QUERY_STRING" => "url=http://localhost:9292/v1/swagger_doc")
            # @swagger_assets_handler.call(_env)
            @swagger_assets_handler.call(env)
          end
        else
          @app.call(env)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
efigence-swagger_ui_wrapper-0.0.8 lib/swagger_ui_wrapper/rack/middleware.rb