Execute code once Sinatra server is running

后端 未结 4 927
陌清茗
陌清茗 2021-01-18 14:33

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?

Her

4条回答
  •  执笔经年
    2021-01-18 15:05

    Using the configure block is not the correct way to do this. Whenever you load the file the commands will be run.

    Try extending run!

    require 'sinatra'
    require 'launchy'
    
    class MyServer < Sinatra::Base
    
      def self.run!
        Launchy.open("http://#{settings.host}:#{settings.port}/")
        super
      end
    
      get '/' do
        "My server"
      end
    end
    

提交回复
热议问题