Search code examples
routessinatradashing

Add routes to Dashing dashboard


How do I add a route in my dashboard that I can access, for example...

get '/:id' do
  protected!
  return params[:'id']
end

Which I can call from http://localhost:3030?id=1234


Solution

  • The easiest way to do this would be to define a new application and call it inside the config.ru that gets created by Dashing. For instance, I created a new file called my_app.rb in a dashing repo with the following contents:

    # my_app.rb
    
    require 'sinatra/base'
    
    class MyApp < Sinatra::Base
      get '/:id' do
        "My own custom route! And the id is #{params[:id]}"
      end
    end
    

    And included that app inside the config.ru like so:

    # Created by dashing until Sinatra::Application
    …
    run Sinatra::Application
    
    # added by us
    run MyApp
    

    And then when you run dashing start, the route we defined in our app gets called. But there's a problem with this approach in that you need to ensure that the routes being defined in MyApp won't conflict with those already defined by dashing. The other way to solve this is to let dashing run on a path other than the default /. There is a bit of documentation for this approach in the Wiki.