module Phlegethon::Exec

Constants

DEFAULTS

Attributes

exchange[RW]

Public Instance Methods

config() click to toggle source
# File lib/phlegethon/exec.rb, line 84
def config
  @config ||= DEFAULTS.merge(YAML.load(File.read(config_path)))
end
config_path() click to toggle source
# File lib/phlegethon/exec.rb, line 88
def config_path
  config_path_candidates.each do |f|
    if File.exist?(f)
      puts "Reading config from #{f}"
      return f
    end
  end
  warn 'config file not found'
  exit
end
config_path_candidates() click to toggle source
# File lib/phlegethon/exec.rb, line 99
def config_path_candidates
  [ './phlegethon.yml',
    ENV['HOME'] + '/.phlegethon.yml' ]
end
deep_encode(data, enc='UTF-8') click to toggle source

TODO move to gem trickery

# File lib/phlegethon/exec.rb, line 70
def deep_encode(data, enc='UTF-8')
  case data
  when String
    data.encode(enc)
  when Array
    data.map { |e| deep_encode(e, enc) }
  when Hash
    data.inject({}) do |r, a|
      r.merge deep_encode(a[0], enc) => deep_encode(a[1], enc)
    end
  else data
  end
end
handler() click to toggle source
# File lib/phlegethon/exec.rb, line 36
def handler
  ->(env) {
    pp env
    req = Rack::Request.new(env)

    # perform some business logic on posted data
    message = deep_encode({
      'params'      => req.params,
      'method'      => req.request_method,
      'url'         => req.url,
      'user_agent'  => req.user_agent
    })
    case req.content_type
    when 'application/json'
      message['payload'] = JSON.parse(req.body.read)
    else
      message = deep_encode(env)
    end
    exchange.publish(JSON.unparse(message))
    # TODO make debug response configurable
    [200, {'Content-Type' => 'text/plain'}, message.to_yaml]
  }
end
init_bunny() click to toggle source

TODO steal recoonect from simon or monitoring in vr_dev

# File lib/phlegethon/exec.rb, line 61
def init_bunny
  # TODO make bunny configurable
  bunny = Bunny.new read_timeout: 10, heartbeat: 10
  bunny.start
  bunny_channel = bunny.create_channel
  self.exchange = bunny_channel.fanout(config['rabbitmq']['exchange'])
end
run(args) click to toggle source
# File lib/phlegethon/exec.rb, line 28
def run(args)
  init_bunny
  server = Thin::Server.new(config['server']['ip'],
                            config['server']['port'],
                            handler)
  server.start
end