Class: Goliath::Server

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

Constant Summary

DEFAULT_PORT =
9000
DEFAULT_ADDRESS =
'0.0.0.0'

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Server) initialize(address = DEFAULT_ADDRESS, port = DEFAULT_PORT)

A new instance of Server



12
13
14
15
16
17
18
19
# File 'lib/goliath/server.rb', line 12

def initialize(address = DEFAULT_ADDRESS, port = DEFAULT_PORT)
  @address = address
  @port = port

  @status = {}
  @config = {}
  @plugins = []
end

Instance Attribute Details

- (Object) address

Returns the value of attribute address



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

def address
  @address
end

- (Object) app

Returns the value of attribute app



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

def app
  @app
end

- (Object) config

Returns the value of attribute config



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

def config
  @config
end

- (Object) logger

Returns the value of attribute logger



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

def logger
  @logger
end

- (Object) options

Returns the value of attribute options



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

def options
  @options
end

- (Object) plugins

Returns the value of attribute plugins



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

def plugins
  @plugins
end

- (Object) port

Returns the value of attribute port



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

def port
  @port
end

- (Object) status

Returns the value of attribute status



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

def status
  @status
end

Instance Method Details

- (Object) config_dir



50
51
52
# File 'lib/goliath/server.rb', line 50

def config_dir
  "#{File.expand_path(File.dirname($0))}/config"
end

- (Object) environment(type, &blk)



59
60
61
# File 'lib/goliath/server.rb', line 59

def environment(type, &blk)
  blk.call if type.to_sym == Goliath.env.to_sym
end

- (Object) import(name)



54
55
56
57
# File 'lib/goliath/server.rb', line 54

def import(name)
  file = "#{config_dir}/#{name}.rb"
  load_config(file)
end

- (Object) load_config(file = nil)



43
44
45
46
47
48
# File 'lib/goliath/server.rb', line 43

def load_config(file = nil)
  file ||= "#{config_dir}/#{File.basename($0)}"
  return unless File.exists?(file)

  eval(IO.read(file))
end

- (Object) load_plugins



63
64
65
66
67
68
69
70
# File 'lib/goliath/server.rb', line 63

def load_plugins
  @plugins.each do |(name, args)|
    logger.info("Loading #{name.to_s}")

    plugin = name.new(port, config, status, logger)
    plugin.run(*args)
  end
end

- (Object) start



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/goliath/server.rb', line 21

def start
  EM.synchrony do
    trap("INT")  { EM.stop }
    trap("TERM") { EM.stop }

    EM.epoll

    load_config
    load_plugins

    EM.start_server(address, port, Goliath::Connection) do |conn|
      conn.app = app
      conn.logger = logger
      conn.status = status
      conn.config = config
      conn.options = options
    end

    EM.set_effective_user("nobody") if Goliath.prod?
  end
end