Class: Mizuno::Server

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.loggerObject



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

def Server.logger
    Logger.logger
end

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



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

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

.stopObject



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

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.



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

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.



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

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]

    # 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

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

#            @server.set_thread_pool(thread_pool)

    # Connector
    connector = Java.org.eclipse.jetty.server.ServerConnector.new(@server)
    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.



129
130
131
132
133
134
# File 'lib/mizuno/server.rb', line 129

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.



139
140
141
# File 'lib/mizuno/server.rb', line 139

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