Skip to content
Henrique Zambon edited this page Mar 17, 2014 · 1 revision

Here's an example application that enables SSL on WEBrick.

config.ru:

require "rubygems"
require "webrick"
require "webrick/https"
require "geminabox"
 
# Set Geminabox' data directory
Geminabox.data = "path/to/data"
 
# Helpers for basic authentication
Geminabox::Server.helpers do
  def protected!
    unless authorized?
      response["WWW-Authenticate"] = %(Basic realm="Restricted Area")
      halt 401, "No pushing or deleting without auth.\n"
    end
  end
 
  def authorized?
    @auth ||= Rack::Auth::Basic::Request.new request.env
    @auth.provided? && @auth.basic? && @auth.credentials &&
      @auth.credentials == ["username", "password"]
  end
end
 
# Require authentication for uploading and deleting gems
Geminabox::Server.before("/upload") { protected! }
Geminabox::Server.before { protected! if request.delete? }
 
# Web server SSL configuration
ssl_certificate = File.read("path/to/ssl/certificate")
ssl_private_key = File.read("path/to/ssl/private_key")
 
webrick_opts = {
  :Port             => 9292,
  :SSLEnable        => true,
  :SSLVerifyClient  => OpenSSL::SSL::VERIFY_NONE,
  :SSLCertificate   => OpenSSL::X509::Certificate.new(ssl_certificate),
  :SSLPrivateKey    => OpenSSL::PKey::RSA.new(ssl_private_key),
  :SSLCertName      => [["CN", WEBrick::Utils::getservername]],
  :app              => Geminabox::Server
}
 
# Start the Web server
Rack::Server.start webrick_opts

source