如何为独立的 Sinatra 应用程序启用 SSL? [英] How to enable SSL for a standalone Sinatra app?
本文介绍了如何为独立的 Sinatra 应用程序启用 SSL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在 Sinatra 中编写一个快速的服务器应用程序.它必须是独立的(即不使用 apache/nginx/passenger),但还必须支持 SSL.
I want to write a quick server app in Sinatra. It has to be self-contained (i.e. not use apache/nginx/passenger) but also has to support SSL.
是否有一种简单的方法可以为 Sinatra 启用 SSL 支持(例如使用 WEBRick)?
Is there an easy way to enable SSL support for Sinatra (using WEBRick for example)?
推荐答案
要使用 MRI ruby 执行此操作,请使用以下monkeypatch:
To do this with MRI ruby, use the following monkeypatch:
sinatra_ssl.rb:
require 'webrick/https'
module Sinatra
class Application
def self.run!
certificate_content = File.open(ssl_certificate).read
key_content = File.open(ssl_key).read
server_options = {
:Host => bind,
:Port => port,
:SSLEnable => true,
:SSLCertificate => OpenSSL::X509::Certificate.new(certificate_content),
:SSLPrivateKey => OpenSSL::PKey::RSA.new(key_content)
}
Rack::Handler::WEBrick.run self, server_options do |server|
[:INT, :TERM].each { |sig| trap(sig) { server.stop } }
server.threaded = settings.threaded if server.respond_to? :threaded=
set :running, true
end
end
end
end
然后,在您的独立应用程序中:
Then, in your standalone application:
app.rb
require 'sinatra'
require 'sinatra_ssl'
set :port, 8443
set :ssl_certificate, "server.crt"
set :ssl_key, "server.key"
get "/" do
"Hello world!"
end
这篇关于如何为独立的 Sinatra 应用程序启用 SSL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文