Sha256: 29a267e3a6a96b2c15497e8ee2b41d2050b595d2535ccebaf4e4ab60df1e74d4

Contents?: true

Size: 1.02 KB

Versions: 3

Compression:

Stored size: 1.02 KB

Contents

module Grenache
  class Http < Grenache::Base
    def listen(key, port,  opts={}, &block)
      start_http_service(port,&block)

      announce(key, port, opts) do |res|
        puts "#{key} announced #{res}"
      end
    end

    def start_http_service(port, &block)
      EM.defer {
        app = -> (env) {
          req = Message.parse(env['rack.input'].read)
          resp = block.call(req)
          [200,nil, Message.response_to(req,resp).to_json]
        }
        server = Thin::Server.start('0.0.0.0', port, app, {signals: false})
      }
    end

    def request(key, payload)
      services = lookup(key)
      if services.size > 0
        json = Message.req(key,payload).to_json
        service = services.sample.sub("tcp://","http://")
        service.prepend("http://") unless service.start_with?("http://")
        resp = HTTParty.post(service,{body: json})
        return [false, Message.parse(resp.body)]
      else
        return ["NoPeerFound",nil]
      end
    rescue Exception => e
      return [e, nil]
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
grenache-ruby-http-0.1.10 lib/grenache/http.rb
grenache-ruby-http-0.1.9 lib/grenache/http.rb
grenache-ruby-http-0.1.8 lib/grenache/http.rb