Execute code once Sinatra server is running

后端 未结 4 1029
陌清茗
陌清茗 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 14:53

    If you're using Rack (which you probably are) I just found out there's a function you can call in config.ru (it's technically an instance method of Rack::Builder) that lets you run a block of code after the server has been started. It's called warmup, and here's the documented usage example:

    warmup do |app|
      client = Rack::MockRequest.new(app)
      client.get('/')
    end
    
    use SomeMiddleware
    run MyApp
    

提交回复
热议问题