-
-
Save AlexKalinin/66682105917fc246f4703df095c656e5 to your computer and use it in GitHub Desktop.
Create an Sinatra SSL Server
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Generate a self-signed Certificate and a Private Key | |
openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout pkey.pem -out cert.crt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'sinatra' | |
require './sinatra_ssl' | |
set :ssl_certificate, "cert.crt" | |
set :ssl_key, "pkey.pem" | |
set :port, 9494 | |
get '/try' do | |
"helloworld" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'webrick/ssl' | |
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), | |
# 123456 is the Private Key Password | |
:SSLPrivateKey => OpenSSL::PKey::RSA.new(key_content,"123456") | |
} | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment