Module: Kawaii::ServerMethods

Included in:
Base
Defined in:
lib/kawaii/server_methods.rb

Overview

Note:

At the moment hard-coded to use WEBrick.

Mixins for starting a self-contained server. To be included in a class derived from Base.

Constant Summary

WEBRICK =
'WEBrick'

Instance Method Summary (collapse)

Instance Method Details

- (Boolean) running?

Returns true if the server is running.

Returns:

  • (Boolean)


29
30
31
# File 'lib/kawaii/server_methods.rb', line 29

def running?
  !@server.nil?
end

- (Object) start!(port)

Starts serving the app.

Parameters:

  • port (Fixnum)

    port number to bind to



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/kawaii/server_methods.rb', line 10

def start!(port) # @todo Support other handlers http://www.rubydoc.info/github/rack/rack/Rack/Handler
  Rack::Handler.get(WEBRICK).run(self, Port: port) do |s|
    @server = s
    at_exit { stop! }
    [:INT, :TERM].each do |signal|
      old = trap(signal) do
        stop!
        old.call if old.respond_to?(:call)
      end
    end
  end
end

- (Object) stop!

Stops serving the app.



24
25
26
# File 'lib/kawaii/server_methods.rb', line 24

def stop!
  @server.stop if @server # NOTE: WEBrick-specific
end