Class: Mizuno::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/mizuno/server.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject

Returns the value of attribute logger.



23
24
25
# File 'lib/mizuno/server.rb', line 23

def logger
  @logger
end

Class Method Details

.loggerObject



43
44
45
# File 'lib/mizuno/server.rb', line 43

def Server.logger
    Logger.logger
end

.run(app, options = {}) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/mizuno/server.rb', line 27

def Server.run(app, options = {})
    @lock.synchronize do
        return if @server
        @server = new
        @server.run(app, options)
    end
end

.stopObject



35
36
37
38
39
40
41
# File 'lib/mizuno/server.rb', line 35

def Server.stop
    @lock.synchronize do 
        return unless @server
        @server.stop
        @server = nil
    end
end

Instance Method Details

#rewindable(request) ⇒ Object

Wraps the Java InputStream for the level of Rack compliance desired.



145
146
147
148
149
150
151
# File 'lib/mizuno/server.rb', line 145

def rewindable(request)
    input = request.getInputStream

    @options[:rewindable] ?
        Rack::RewindableInput.new(input.to_io.binmode) :
        RewindableInputStream.new(input).to_io.binmode
end

#run(app, options = {}) ⇒ Object

Start up an instance of Jetty, running a Rack application. Options can be any of the follwing, and are not case-sensitive:

:host

String specifying the IP address to bind to; defaults to 0.0.0.0.

:port

String or integer with the port to bind to; defaults to 9292.



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
# File 'lib/mizuno/server.rb', line 60

def run(app, options = {})
    # Symbolize and downcase keys.
    @options = options = Hash[options.map { |k, v| 
        [ k.to_s.downcase.to_sym, v ] }]
    options[:quiet] ||= true if options[:embedded]

    # The Jetty server
    Logger.configure(options)
    @logger = Logger.logger
    @server = Java.org.eclipse.jetty.server.Server.new
    @server.setSendServerVersion(false)

    # Thread pool
    threads = options[:threads] || 50
    thread_pool = QueuedThreadPool.new
    thread_pool.min_threads = options.fetch(:min_threads,
        [ threads.to_i / 10, 5 ].max)
    thread_pool.max_threads = [ threads.to_i, 10 ].max
    @server.set_thread_pool(thread_pool)

    # Connector
    connector = SelectChannelConnector.new
    connector.setReuseAddress(options.fetch(:reuse_address, false))
    connector.setPort(options[:port].to_i)
    connector.setHost(options[:host])
    max_header_size = options.fetch(:max_header_size, 32768)
    connector.setRequestHeaderSize(max_header_size)
    
    @server.addConnector(connector)

    # SSL Connector
    configure_https(options) if options[:ssl_port]

    # Switch to a different user or group if we were asked to.
    Runner.setgid(options) if options[:group]
    Runner.setuid(options) if options[:user]

    # Optionally wrap with Mizuno::Reloader.
    threshold = (ENV['RACK_ENV'] == 'production' ? 10 : 1)
    app = Mizuno::Reloader.new(app, threshold) \
        if options[:reloadable]

    # The servlet itself.
    rack_handler = RackHandler.new(self)
    rack_handler.rackup(app)

    # Add the context to the server and start.
    @server.set_handler(rack_handler)
    @server.start
    $stderr.printf("%s listening on %s:%s\n", version,
        connector.host, connector.port) unless options[:quiet]

    # If we're embedded, we're done.
    return if options[:embedded]

    # Stop the server when we get The Signal.
    trap("SIGTERM") { @server.stop and exit }

    # Join with the server thread, so that currently open file
    # descriptors don't get closed by accident.
    # http://www.ruby-forum.com/topic/209252
    @server.join
end

#stopObject

Shuts down an embedded Jetty instance.



127
128
129
130
131
132
# File 'lib/mizuno/server.rb', line 127

def stop
    return unless @server
    $stderr.print "Stopping Jetty..." unless @options[:quiet]
    @server.stop
    $stderr.puts "done." unless @options[:quiet]
end

#versionObject

Returns the full version string.



137
138
139
# File 'lib/mizuno/server.rb', line 137

def version
    "Mizuno #{Mizuno::VERSION} (Jetty #{Java.org.eclipse.jetty.server.Server.getVersion})"
end