Add route to dashing sinatra application?

519 Views Asked by At

I'm trying to add a route to my dashing application that will receive data from a webhook.

I tried using the solution here, and while that did create the route, it broke the dashing dashboard at '/sample'.

Any ideas?

Here is my lib/app.rb:

require 'sinatra/base'

class App < Sinatra::Base

  get '/callback' do
    "Callback route."
  end

end

Here is my config.ru:

require 'dashing'

configure do
  set :auth_token, 'YOUR_AUTH_TOKEN'

  helpers do
    def protected!
      # Put any authentication code you want in here.
      # This method is run before accessing any resource.
    end
  end
end

map Sinatra::Application.assets_prefix do
  run Sinatra::Application.sprockets
end

run Sinatra::Application
run App

UPDATE:
I changed the route name to something obscure (so that the mount for sure doesn't use it). It looks like whichever run command I put last is the one that takes effect. The route works if run App is last, the dashboard works if run Sinatra::Application is last. But when one works, the other doesn't

1

There are 1 best solutions below

0
On

You can run different apps inside your config.ru.

Inside your config.ru, replace

map Sinatra::Application.assets_prefix do
  run Sinatra::Application.sprockets
end

run Sinatra::Application
run App

with

map('/') { run App }
map('/sample') { run Sinatra::Application }
map(Sinatra::Application.assets_prefix) { run Sinatra::Application.sprockets }

It's important to note that by doing this, rack is going to be handling the route prefixes. So if you navigated to /sample, The sinatra app run under that route will see that as /.

config.ru is actually run in a Rack::Builder context. So the above is equivalent to

apps = Rack::Builder.new do 
     map('/') { run App }
     map('/sample') { run Sinatra::Application }
     map(Sinatra::Application.assets_prefix) { run Sinatra::Application.sprockets }
end
run apps