Sha256: b5ab97de140c693eddb3f606d4ed1c1497561c126daf55e9301e322ebaf8dc0d

Contents?: true

Size: 1.04 KB

Versions: 2

Compression:

Stored size: 1.04 KB

Contents

module Rswag3
  module Ui
    class Middleware < Rack::Static

      def initialize(app, config)
        @config = config
        super(app, urls: [ '' ], root: config.assets_root )
      end

      def call(env)
        if base_path?(env) 
          redirect_uri = env['SCRIPT_NAME'].chomp('/') + '/index.html'
          return [ 301, { 'Location' => redirect_uri }, [ ] ]
        end

        if index_path?(env)
          return [ 200, { 'Content-Type' => 'text/html' }, [ render_template ] ]
        end

        super
      end

      private

      def base_path?(env)
        env['REQUEST_METHOD'] == "GET" && env['PATH_INFO'] == "/"
      end

      def index_path?(env)
        env['REQUEST_METHOD'] == "GET" && env['PATH_INFO'] == "/index.html"
      end

      def render_template
        file = File.new(template_filename)
        template = ERB.new(file.read)
        template.result(@config.get_binding)
      end

      def template_filename
        @config.template_locations.find { |filename| File.exists?(filename) }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rswag3-ui-0.0.2 lib/rswag3/ui/middleware.rb
rswag3-ui-0.0.1 lib/rswag3/ui/middleware.rb