Sha256: b007007679fbda6fb221c2174f27904a34b095362234f824512a9c6755e631b8

Contents?: true

Size: 1.08 KB

Versions: 3

Compression:

Stored size: 1.08 KB

Contents

require 'rack'

module Swagui

  class Middleware

    def initialize(app, options={})
      @app = app

      @url_path = options[:url]
      @doc_path = options[:path] || @url_path
      @url_regex = Regexp.new("^#{@url_path}")
      @assets_url_regex = Regexp.new("^\/swagger-ui")

      app_doc_dir = File.expand_path(@doc_path, Dir.pwd)
      @app_file_server = Rack::File.new(app_doc_dir)
      swagger_ui_dir = File.expand_path('../../swagger-ui', File.dirname(__FILE__))
      @swagger_ui_file_server = Rack::File.new(swagger_ui_dir)
    end

    def call(env)
      path = env["PATH_INFO"].strip

      if @assets_url_regex.match(path)
        env["PATH_INFO"] = path.gsub(@assets_url_regex, '')
        @swagger_ui_file_server.call(env)
      elsif @url_regex.match(path)
        path = path.gsub(@url_regex, '')
        if path == '' || path == '/'
          env["PATH_INFO"] = 'index.html'
          @swagger_ui_file_server.call(env)
        else
          env["PATH_INFO"] = path
          @app_file_server.call(env)
        end
      else
        @app.call(env)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
swagui-0.0.5 lib/swagui/middleware.rb
swagui-0.0.4 lib/swagui/middleware.rb
swagui-0.0.3 lib/swagui/middleware.rb