tags:

views:

52

answers:

1

I have a Sinatra Application enclosed in Sinatra::Base and I'd like to run some code once the server has started, how should I go about doing this?

Here's an example:

require 'sinatra'
require 'launchy'

class MyServer < Sinatra::Base
  get '/' do
    "My server"
  end

  # This is the bit I'm not sure how to do
  after_server_running do
    # Launches a browser with this webapp in it upon server start
    Launchy.open("http://#{settings.host}:#{settings.port}/")
  end
end

Any ideas?

+1  A: 

You can put that code in a configure block which is run once at startup:

require 'sinatra'
require 'launchy'

class MyServer < Sinatra::Base
  get '/' do
    "My server"
  end

  configure do
     Launchy.open("http://#{settings.host}:#{settings.port}/")
  end
end
Trevor
Sneaky tip! I would have thought configure would have run before the server starts, not after! But it works perfectly, thanks!
JP