Class: Spider::HTTP::Server

Inherits:
Object show all
Defined in:
lib/spiderfw/http/server.rb

Direct Known Subclasses

CGIServer, FCGIServer, Mongrel, Thin, WEBrick

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.get_opts(server_name, options) ⇒ Object



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/spiderfw/http/server.rb', line 40

def self.get_opts(server_name, options)
    server_name ||= Spider.conf.get('http.server')
    options[:host] ||= Spider.conf.get('webserver.host')
    options[:port] ||= Spider.conf.get('webserver.port')
    opts = {
        :server => server_name,
        :Host => options[:host],
        :Port => options[:port],
        :config => File.join(Spider.paths[:root], 'config.ru')
    }
    case server_name
    when 'thin'
        opts[:threaded] = true unless options[:no_threads]
        options[:daemonize] = true
    end
    opts[:app] = Spider::HTTP::RackApplication.new unless File.file?(opts[:config])
    ssl_opts = nil
    if options[:ssl]
        require 'openssl'
        options[:ssl_cert] ||= Spider.conf.get('orgs.default.cert')
        options[:ssl_key] ||= Spider.conf.get('orgs.default.private_key')
        raise "SSL Certificate not set" unless options[:ssl_cert]
        raise "SSL Key not set" unless options[:ssl_key]
        raise "SSL Certificate (#{options[:ssl_cert]}) not found" unless File.file?(options[:ssl_cert])
        raise "SSL Key (#{options[:ssl_key]}) not found" unless File.file?(options[:ssl_key])
        ssl_opts = opts.clone
        ssl_opts[:Port] = options[:ssl]
        private_key = OpenSSL::PKey::RSA.new(File.open(options[:ssl_key]).read)
        certificate = OpenSSL::X509::Certificate.new(File.open(options[:ssl_cert]).read)
        case server_name
        when 'webrick'
            require 'webrick/https'
            ssl_opts[:SSLEnable] = true
            ssl_opts[:SSLVerifyClient] = ::OpenSSL::SSL::VERIFY_NONE
            ssl_opts[:SSLCertificate] = certificate
            ssl_opts[:SSLPrivateKey] = private_key
        # when 'thin'
        #     ssl_opts[:ssl] = true
        #     ssl_opts[:verify_peer] = ::OpenSSL::SSL::VERIFY_NONE
        #     ssl_opts[:ssl_key_file] = private_key
        #     ssl_opts[:ssl_cert_file] = certificate
        else
            raise "SSL not supported with #{server_name} server"
        end
    end
    return [opts, ssl_opts]
end

.start(server_name, options = {}) ⇒ Object



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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/spiderfw/http/server.rb', line 88

def self.start(server_name, options={})
    start = Proc.new{
        
        
        pid_file = File.join(Spider.paths[:var], 'run/server.pid')
        puts _("Using webserver %s") % server_name if options[:verbose]
        puts _("Listening on port %s") % opts[:Port] if options[:verbose]
        rack = nil
        ssl_rack = nil
        server = nil
        ssl_server = nil

        require 'spiderfw/init'
        Spider.logger.info{ "Routes: \n"+Spider::HomeController.print_app_routes }
        Spider.startup
        opts, ssl_opts = self.get_opts(server_name, options)
        
        if opts
            thread = Thread.new do
                rack = Rack::Server.start(opts)
                server = rack.server if rack
            end
            $stdout << "Spider server running on port #{opts[:Port]}\n"
        end
        
        if ssl_opts
            ssl_thread = Thread.new do
                ssl_rack = Rack::Server.start(ssl_opts)
                ssl_server = ssl_rack.server if ssl_rack
            end
        end
        do_shutdown = Proc.new{
            case RUBY_VERSION
            when /2/
                Byebug.post_mortem = false if defined?(Byebug)
            else
                Debugger.post_mortem = false if defined?(Debugger)
            end
            server.shutdown if server
            ssl_server.shutdown if ssl_server

            pid_file = File.join(Spider.paths[:var], 'run/server.pid')
            begin
                FileUtils.rm_f(pid_file)
            rescue Errno::ENOENT
                Spider.logger.info "Unlink del pid file non riuscito"
            end
        }
        Spider.on_shutdown(&do_shutdown)
        begin
            thread.join if thread
            ssl_thread.join if ssl_thread
        rescue SystemExit
            Spider.logger.error "Problema nella creazione di un thread"
        end
    }
    if options[:daemonize]
        require 'spiderfw/init'
        require 'spiderfw/utils/fork'
        pid_file = File.join(Spider.paths[:var], 'run/server.pid')
        process_name = (options[:daemonize] == true) ? 'spider-server' : options[:daemonize]
        forked = Spider.fork do
            File.open(pid_file, 'w') do |f|
                f.write(Process.pid)
            end
            $SPIDER_SCRIPT ||= $0
            $0 = process_name
            STDIN.reopen "/dev/null"       # Free file descriptors and
            STDOUT.reopen "/dev/null", "a" # point them somewhere sensible
            STDERR.reopen STDOUT           # STDOUT/STDERR should go to a logfile
            start.call
        end
        Process.detach(forked)
    else
        #questo fa Loads configuration, sets up Locale and GetText, sets paths and the default Logger.
        Spider.init_base 
        spawner_started = false
        if Spider.conf.get('webserver.respawn_on_change')
            Spider.start_loggers
            begin
                gemfile = File.join(Spider.paths[:root], 'Gemfile')
                gemfile_lock = File.join(Spider.paths[:root], 'Gemfile.lock')
                if File.file?(gemfile) && File.file?(gemfile_lock)
                    require 'bundler'
                    Bundler.require :default, Spider.runmode.to_sym
                end
                spawner = Spawner.new({'spawn' => start})
                spawner.run('spawn')
                spawner_started = true
            rescue LoadError => exc
                raise unless exc.message =~ /fssm/
                Spider.logger.error("Install 'fssm' gem to enable respawning")
            end
        end
        unless spawner_started
            #in devel qui non entra
            Spider.main_process_startup
            Spider.startup
            begin
                start.call 
            rescue Exception => exc
                Spider.logger.error(exc)
            end
        end
    end
end

.supports?(capability) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/spiderfw/http/server.rb', line 13

def self.supports?(capability)
    @supports[capability]
end

Instance Method Details

#options(opts = {}) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/spiderfw/http/server.rb', line 18

def options(opts={})
    defaults = {
        :host => '0.0.0.0',
        :port => 8080
    }
    return defaults.merge!(opts)
end

#request_receivedObject



37
38
# File 'lib/spiderfw/http/server.rb', line 37

def request_received
end

#shutdownObject



32
33
34
35
# File 'lib/spiderfw/http/server.rb', line 32

def shutdown
    Spider.logger.info("Webserver shutdown");
    shutdown_server            
end

#start(opts = {}) ⇒ Object



26
27
28
29
30
# File 'lib/spiderfw/http/server.rb', line 26

def start(opts={})
    @options = opts
    Spider.logger.info{ "Routes: \n"+Spider::HomeController.print_app_routes }
    start_server(opts)
end