Last active
March 11, 2025 10:24
-
-
Save TakahikoKawasaki/aefb0b9e4306b6dfbcc5 to your computer and use it in GitHub Desktop.
Sinatra + Thin + SSL
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
#!/usr/bin/env ruby | |
# | |
# This code snippet shows how to enable SSL in Sinatra+Thin. | |
# | |
require 'sinatra' | |
require 'thin' | |
class MyThinBackend < ::Thin::Backends::TcpServer | |
def initialize(host, port, options) | |
super(host, port) | |
@ssl = true | |
@ssl_options = options | |
end | |
end | |
configure do | |
set :environment, :production | |
set :bind, '0.0.0.0' | |
set :port, 443 | |
set :server, "thin" | |
class << settings | |
def server_settings | |
{ | |
:backend => MyThinBackend, | |
:private_key_file => File.dirname(__FILE__) + "/server.key", | |
:cert_chain_file => File.dirname(__FILE__) + "/server.crt", | |
:verify_peer => false | |
} | |
end | |
end | |
end | |
get '/' do | |
"Hello, SSL." | |
end |
Aside from generating the key, csr, and certificate, is there anything else that needs to be configured (perhaps on the OS level) to get this to work? I can get the sinatra script to execute just fine..it says it's listening on 0.0.0.0:443 but Chrome is saying the site cannot be reached...
Anyone knows how to do the same thing with puma?
Thanks for this!
How should I configure this if I've got more than one subclasses of Sinatra::Base
and using config.ru
?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Works great! However, how can I have it send the Access-Control-Allow-Origin header? I'm new to all this ruby stuff, going to go back to googling in the meantime...
edit: got it to work something like this:
get '/blah' do
@BLAH = # code ...
code...
headers['Access-Control-Allow-Origin'] = '*'
return @blah.to_json
end