PageRenderTime 27ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/script/server_ssl

http://ovz-web-panel.googlecode.com/
Ruby | 75 lines | 59 code | 15 blank | 1 comment | 3 complexity | 73583cc7b753758052cf274ffb12dbeb MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0
  1. #!/usr/bin/env ruby
  2. require File.dirname(__FILE__) + '/../config/boot'
  3. require 'webrick'
  4. require 'webrick/https'
  5. OPTIONS = {
  6. :port => 3001,
  7. :ip => "0.0.0.0",
  8. :environment => (ENV['RAILS_ENV'] || "development").dup,
  9. :server_root => File.expand_path(RAILS_ROOT + "/public/"),
  10. :pkey => OpenSSL::PKey::RSA.new(File.open(RAILS_ROOT + "/config/certs/server.key").read),
  11. :cert => OpenSSL::X509::Certificate.new(File.open(RAILS_ROOT + "/config/certs/server.crt").read),
  12. :server_type => WEBrick::SimpleServer,
  13. :charset => "UTF-8",
  14. :mime_types => WEBrick::HTTPUtils::DefaultMimeTypes,
  15. :debugger => false,
  16. :detach => false
  17. }
  18. require 'optparse'
  19. ARGV.clone.options do |opts|
  20. opts.on("-p", "--port=port", Integer,
  21. "Runs Rails on the specified port.", "Default: 3001") { |v| OPTIONS[:port] = v }
  22. opts.on("-b", "--binding=ip", String,
  23. "Binds Rails to the specified ip.", "Default: 0.0.0.0") { |v| OPTIONS[:ip] = v }
  24. opts.on("-d", "--daemon", "Make server run as a Daemon.") { OPTIONS[:detach] = true }
  25. opts.on("-e", "--environment=name", String,
  26. "Specifies the environment to run this server under (test/development/production).",
  27. "Default: development") { |v| OPTIONS[:environment] = v }
  28. opts.separator ""
  29. opts.on("-h", "--help", "Show this help message.") { puts opts; exit }
  30. opts.parse!
  31. end
  32. ENV["RAILS_ENV"] = OPTIONS[:environment]
  33. RAILS_ENV.replace(OPTIONS[:environment]) if defined?(RAILS_ENV)
  34. puts "=> Booting WEBrick..."
  35. require RAILS_ROOT + "/config/environment"
  36. require 'webrick_server'
  37. OPTIONS['working_directory'] = File.expand_path(RAILS_ROOT)
  38. class SSLDispatchServlet < DispatchServlet
  39. def self.dispatch(options)
  40. WEBrick::Daemon.start if options[:detach]
  41. Socket.do_not_reverse_lookup = true
  42. server = WEBrick::HTTPServer.new(
  43. :Port => options[:port].to_i,
  44. :ServerType => options[:server_type],
  45. :BindAddress => options[:ip],
  46. :SSLEnable => true,
  47. :SSLVerifyClient => OpenSSL::SSL::VERIFY_NONE,
  48. :SSLCertificate => options[:cert],
  49. :SSLPrivateKey => options[:pkey],
  50. :SSLCertName => [ [ "CN", WEBrick::Utils::getservername ] ]
  51. )
  52. server.mount('/', DispatchServlet, options)
  53. trap("INT") { server.shutdown; exit! }
  54. trap("TERM") { server.shutdown; exit! }
  55. server.start
  56. end
  57. end
  58. puts "=> Rails application starting on https://#{OPTIONS[:ip]}:#{OPTIONS[:port]}"
  59. puts "=> Ctrl-C to shutdown server"
  60. SSLDispatchServlet.dispatch(OPTIONS)