Can I enable SSL in Sinatra with Thin?

后端 未结 3 2055
情话喂你
情话喂你 2021-01-02 13:57

I\'m looking for a simple way to enable SSL in a standalone Sinatra application running via Thin without having to pass the --ssl, --ssl-key-file a

3条回答
  •  别那么骄傲
    2021-01-02 14:22

    I just spent a few hours trying to figure this one out myself.

    It turns out that Thin::Server.initialize discards ssl options during its initialization method (it delegates to its instance of Backend, which immediately sets its ssl to nil, ignoring any ssl options you've passed into Thin::Server.new. This means you have to to set ssl options after you've instantiated a server.)

    Here's how to do it:

    class App < Sinatra::Base
    
      # ...
    
      def self.run!
        rack_handler_config = {}
    
        ssl_options = {
          :private_key_file => '/path/to/foo.key',
          :cert_chain_file => '/path/to/bar.crt',
          :verify_peer => false,
        }
    
        Rack::Handler::Thin.run(self, rack_handler_config) do |server|
          server.ssl = true
          server.ssl_options = ssl_options
        end
      end
    end
    
    App.run!
    

提交回复
热议问题