Sha256: 9601d274bec0c34640f63b1babc7423ce1d92afe1c4cda44706d0b51af5a0256

Contents?: true

Size: 857 Bytes

Versions: 9

Compression:

Stored size: 857 Bytes

Contents

module FactoryBurgers
  module Middleware
    # Serve static assets build for the UI
    class Static
      def call(env)
        return slashpath_redirect(env["REQUEST_PATH"]) if slashpath_redirect?(env)

        rack_static.call(env)
      end

      def slashpath_redirect?(env)
        env["REQUEST_PATH"] == env["SCRIPT_NAME"] && env["REQUEST_PATH"][-1] != "/"
      end

      def slashpath_redirect(path)
        return [
          302,
          {'Location' => "#{path}/", 'Content-Type' => 'text/html', 'Content-Length' => '0'},
          [],
        ]
      end

      def rack_static
        Rack::Static.new(nil, static_options)
      end

      def static_options
        {urls: [""], root: asset_path, index: 'index.html'}
      end

      def asset_path
        @asset_path ||= FactoryBurgers.root.join("assets/")
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
factory_burgers-1.1.3 lib/factory_burgers/middleware/static.rb
factory_burgers-1.1.2 lib/factory_burgers/middleware/static.rb
factory_burgers-1.1.1 lib/factory_burgers/middleware/static.rb
factory_burgers-1.1.0 lib/factory_burgers/middleware/static.rb
factory_burgers-1.0.0 lib/factory_burgers/middleware/static.rb
factory_burgers-0.1.5 lib/factory_burgers/middleware/static.rb
factory_burgers-0.1.4 lib/factory_burgers/middleware/static.rb
factory_burgers-0.1.2 lib/factory_burgers/middleware/static.rb
factory_burgers-0.1.0 lib/factory_burgers/middleware/static.rb