class Gopher::Server

main class which will listen on a specified port, and pass requests to an Application class

Attributes

app[RW]

Public Class Methods

new(a) click to toggle source

constructor @param [Application] a instance of Gopher::Application we want to run

# File lib/gopher2000/server.rb, line 13
def initialize(a)
  @app = a
end

Public Instance Methods

env() click to toggle source

@return [String] environment specified in config

# File lib/gopher2000/server.rb, line 34
def env
  @app.config[:env] || 'development'
end
host() click to toggle source

@return [String] name of the host specified in our config

# File lib/gopher2000/server.rb, line 20
def host
  @app.config[:host] ||= '0.0.0.0'
end
port() click to toggle source

@return [Integer] port specified in our config

# File lib/gopher2000/server.rb, line 27
def port
  @app.config[:port] ||= 70
end
run!() click to toggle source

main app loop. called via at_exit block defined in DSL

# File lib/gopher2000/server.rb, line 41
def run!
  EventMachine::run do
    Signal.trap("INT") {
      puts "It's a trap!"
      EventMachine.stop
    }
    Signal.trap("TERM") {
      puts "It's a trap!"
      EventMachine.stop
    }

    EventMachine.kqueue = true if EventMachine.kqueue?
    EventMachine.epoll = true if EventMachine.epoll?


    STDERR.puts "start server at #{host} #{port}"
    if @app.non_blocking?
      STDERR.puts "Not blocking on requests"
    end


    EventMachine::start_server(host, port, Gopher::Dispatcher) do |conn|
      #
      # check if we should reload any scripts before moving along
      #
      @app.reload_stale

      #
      # roughly matching sinatra's style of duping the app to respond
      # to requests, @see http://www.sinatrarb.com/intro#Request/Instance%20Scope
      #
      # this essentially means we have 'one instance per request'
      #
      conn.app = @app.dup
    end
  end
end