Class: Spider::HTTP::Server

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

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) start(server_name, port, options = {})



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
66
67
68
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
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
# File 'lib/spiderfw/http/server.rb', line 37

def self.start(server_name, port, options={})
    servers = {
        'webrick'   => :WEBrick,
        'mongrel'   => :Mongrel,
        'thin'      => :Thin
    }
    
    start = lambda{
        $SPIDER_WEB_SERVER = true
        begin
            require 'spiderfw/init'
        rescue Exception => exc
            Spider.logger.error(exc)
            return
        end
        require 'spiderfw/controller/http_controller'
        
        port ||= Spider.conf.get('webserver.port')
        server_name ||= Spider.conf.get('http.server')
        pid_file = File.join(Spider.paths[:var], 'run/server.pid')
        puts _("Using webserver %s") % server_name if options[:verbose]
        puts _("Listening on port %s") % port if options[:verbose]
        server = Spider::HTTP.const_get(servers[server_name]).new
        ssl_server = nil
        Spider.startup
        begin
            if Spider.conf.get('devel.trace.extended')
                require 'ruby-debug'
                require 'spiderfw/utils/monkey/debugger'
                Debugger.start
                Debugger.post_mortem
            end
        rescue Exception => exc
            Spider.logger.warn "Unable to start debugger"
        end
        
        thread = Thread.new do
            server.start(:port => port, :cgi => options[:cgi])
        end
        $stdout << "Spider server running on port #{port}\n"
        if options[:ssl]
            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_thread = Thread.new do
                ssl_server = Spider::HTTP.const_get(servers[server_name]).new
                ssl_server.start(:port => options[:ssl], :ssl => true, 
                    :ssl_cert => options[:ssl_cert], :ssl_private_key => options[:ssl_key])
            end
        end
        do_shutdown = lambda{
            Debugger.post_mortem = false if defined?(Debugger)
            # debugger
            server.shutdown
            ssl_server.shutdown if ssl_server
            pid_file = File.join(Spider.paths[:var], 'run/server.pid')
            begin
                File.unlink(pid_file)
            rescue Errno::ENOENT
            end
        }
        Spider.on_shutdown(&do_shutdown)
        
        thread.join
        ssl_thread.join if ssl_thread
    }
    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
        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
            Spider.main_process_startup
            Spider.startup
            begin
                start.call 
            rescue Exception => exc
                Spider.logger.error(exc)
            end
        end
    end
end

+ (Boolean) supports?(capability)



10
11
12
# File 'lib/spiderfw/http/server.rb', line 10

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

Instance Method Details

- (Object) options(opts = {})



15
16
17
18
19
20
21
# File 'lib/spiderfw/http/server.rb', line 15

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

- (Object) request_received



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

def request_received
end

- (Object) shutdown



29
30
31
32
# File 'lib/spiderfw/http/server.rb', line 29

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

- (Object) start(opts = {})



23
24
25
26
27
# File 'lib/spiderfw/http/server.rb', line 23

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