Class: Gopher::Server

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

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(a) ⇒ Server

constructor

Parameters:

  • a (Application)

    instance of Gopher::Application we want to run



13
14
15
# File 'lib/gopher2000/server.rb', line 13

def initialize(a)
  @app = a
end

Instance Attribute Details

#appObject

Returns the value of attribute app.



7
8
9
# File 'lib/gopher2000/server.rb', line 7

def app
  @app
end

Instance Method Details

#envString

Returns environment specified in config.

Returns:

  • (String)

    environment specified in config



34
35
36
# File 'lib/gopher2000/server.rb', line 34

def env
  @app.config[:env] || 'development'
end

#hostString

Returns name of the host specified in our config.

Returns:

  • (String)

    name of the host specified in our config



20
21
22
# File 'lib/gopher2000/server.rb', line 20

def host
  @app.config[:host] ||= '0.0.0.0'
end

#portInteger

Returns port specified in our config.

Returns:

  • (Integer)

    port specified in our config



27
28
29
# File 'lib/gopher2000/server.rb', line 27

def port
  @app.config[:port] ||= 70
end

#run!Object

main app loop. called via at_exit block defined in DSL



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
# 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