Module: CASServer::Postambles

Defined in:
lib/casserver/postambles.rb

Instance Method Summary collapse

Instance Method Details

#cgiObject



133
134
135
136
# File 'lib/casserver/postambles.rb', line 133

def cgi
  CASServer.create
  puts CASServer.run
end

#fastcgiObject



124
125
126
127
128
129
130
# File 'lib/casserver/postambles.rb', line 124

def fastcgi
  require 'camping/fastcgi'
  Dir.chdir('/srv/www/camping/casserver/')

  CASServer.create
  Camping::FastCGI.start(CASServer)
end

#mongrelObject



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/casserver/postambles.rb', line 69

def mongrel
  require 'rubygems'
  require 'mongrel/camping'

  if $DAEMONIZE
    # check if log and pid are writable before daemonizing, otherwise we won't be able to notify
    # the user if we run into trouble later (since once daemonized, we can't write to stdout/stderr)
    check_pid_writable if $PID_FILE
    check_log_writable
  end

  CASServer.create

  puts "\n** CASServer is starting. Look in '#{$CONF.log[:file]}' for further notices."

  settings = {:host => "0.0.0.0", :log_file => $CONF.log[:file], :cwd => $CASSERVER_HOME}

  # need to close all IOs before daemonizing
  $LOG.close if $DAEMONIZE

  begin
    config = Mongrel::Configurator.new settings  do
      daemonize :log_file => $CONF.log[:file], :cwd => $CASSERVER_HOME if $DAEMONIZE

      listener :port => $CONF.port do
        uri $CONF.uri_path, :handler => Mongrel::Camping::CampingHandler.new(CASServer)
        setup_signals
      end
    end
  rescue Errno::EADDRINUSE
    exit 1
  end

  config.run

  CASServer.init_logger
  CASServer.init_db_logger

  if $DAEMONIZE && $PID_FILE
    write_pid_file
    unless File.exists? $PID_FILE
      $LOG.error "CASServer could not start because pid file '#{$PID_FILE}' could not be created."
      exit 1
    end
  end

  puts "\n** CASServer is running at http://localhost:#{$CONF.port}#{$CONF.uri_path} and logging to '#{$CONF.log[:file]}'"
  config.join

  clear_pid_file

  puts "\n** CASServer is stopped (#{Time.now})"
end

#webrickObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/casserver/postambles.rb', line 4

def webrick
  require 'webrick/httpserver'
  require 'webrick/https'
  require 'camping/webrick'

  # TODO: verify the certificate's validity
  # example of how to do this is here: http://pablotron.org/download/ruri-20050331.rb

  cert_path = $CONF.ssl_cert
  key_path = $CONF.ssl_key || $CONF.ssl_cert
    # look for the key in the ssl_cert if no ssl_key is specified

  webrick_options = {:BindAddress => "0.0.0.0", :Port => $CONF.port}

  unless cert_path.nil? && key_path.nil?
    raise "'#{cert_path}' is not a valid ssl certificate. Your 'ssl_cert' configuration" +
      " setting must be a path to a valid ssl certificate file." unless
        File.exists? cert_path

    raise "'#{key_path}' is not a valid ssl private key. Your 'ssl_key' configuration" +
      " setting must be a path to a valid ssl private key file." unless
        File.exists? key_path

    cert = OpenSSL::X509::Certificate.new(File.read(cert_path))
    key = OpenSSL::PKey::RSA.new(File.read(key_path))

    webrick_options[:SSLEnable] = true
    webrick_options[:SSLVerifyClient] = ::OpenSSL::SSL::VERIFY_NONE
    webrick_options[:SSLCertificate] = cert
    webrick_options[:SSLPrivateKey] = key
  end

  begin
    s = WEBrick::HTTPServer.new(webrick_options)
  rescue Errno::EACCES
    puts "\nThe server could not launch. Are you running on a privileged port? (e.g. port 443) If so, you must run the server as root."
    exit 2
  end

  CASServer.create
  s.mount "#{$CONF.uri_path}", WEBrick::CampingHandler, CASServer

  puts "\n** CASServer is running at http#{webrick_options[:SSLEnable] ? 's' : ''}://#{Socket.gethostname}:#{$CONF.port}#{$CONF.uri_path} and logging to '#{$CONF.log[:file]}'\n\n"

  # This lets Ctrl+C shut down your server
  trap(:INT) do
    s.shutdown
  end
  trap(:TERM) do
    s.shutdown
  end

  if $DAEMONIZE
    WEBrick::Daemon.start do
      write_pid_file if $PID_FILE
      s.start
      clear_pid_file
    end
  else
    s.start
  end
end