Skip to content

Mounting Endpoints

Janko Marohnić edited this page May 20, 2019 · 3 revisions

Several Shrine plugins – upload_endpoint, presign_endpoint, derivation_endpoint, and download_endpoint – come with Rack apps, which can be "mounted" inside your router to handle incoming requests. Since these apps are built on top of Rack, they can be used with any Rack-based Ruby web framework.

Shrine.upload_endpoint(:cache)
Shrine.presign_endpoint(:cache)
Shrine.download_endpoint
Shrine.derivation_endpoint

However, different web frameworks offer a different API for mounting Rack apps. This document attempts to provide a comperhensive list of how this is done in all major Ruby web frameworks.

Rails

In Rails you can use #mount in config/routes.rb:

# config/routes.rb
Rails.application.routes.draw do
  mount ImageUploader.derivation_endpoint => "derivations/image"
end

Sinatra

Sinatra doesn't offer an dedicated API, but you can still do it in your config.ru:

# config.ru
map "/derivations/image" do
  run ImageUploader.derivation_endpoint
end

run MyApp

Hanami

In Hanami you can use #mount in config/environment.rb:

Hanami.configure do
  mount ImageUploader.derivation_endpoint, at: "/derivations/image"
end

Grape

In Grape you can use #mount inside your app:

class MyApp < Grape::API
  mount Shrine.derivation_endpoint => "/derivations/image"
end

Roda

In Roda you can use #run inside a routing tree:

class MyApp < Roda
  route do |r|
    r.on "derivations/image" do
      r.run ImageUploader.derivation_endpoint
    end
  end
end

Cuba

In Cuba you can use #run inside a routing tree:

class MyApp < Cuba
  define do
    on "derivations/image" do
      run ImageUploader.derivation_endpoint
    end
  end
end

Rack

If your web framework doesn't support mounting Rack applications, you can always mount it in your config.ru:

# config.ru
map "/derivations/image" do
  run ImageUploader.derivation_endpoint
end

run MyApp
You can’t perform that action at this time.