Sha256: 79c70db4bda2cf63924e97bbbb240e1a30cde264f0936b36e7e82d2b4b1bf8db

Contents?: true

Size: 1.01 KB

Versions: 1

Compression:

Stored size: 1.01 KB

Contents

# encoding: utf-8

require 'hatetepe'

class EchoServer
  def serve(request, served)
    response = streaming_response
    served.fulfill(response)

    serialize(request, response)
    request.closed { response.close }
  end

  private

  def streaming_response
    Hatetepe::Response.new(200, { 'Content-Type' => 'text/plain' },
                           Hatetepe::Body.new)
  end

  def serialize(request, response)
    writer     = response.body.closed.method(:progress)
    serializer = Hatetepe::Serializer.new(request, writer)
    serializer.serialize
  end

  def writer(response)
    proc { |data| response.body.closed.progress(data) }
  end
end

Hatetepe.run do
  Hatetepe::Server.start(address:  ENV.fetch('ADDRESS', '127.0.0.1'),
                         port:     ENV.fetch('PORT', 3000).to_i,
                         handlers: [Hatetepe::Server::KeepAlive, EchoServer])

  terminated = Hatetepe::Promise.new
  Signal.trap(:INT)  { terminated.fulfill }
  Signal.trap(:TERM) { terminated.fulfill }
  terminated.sync
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hatetepe-0.6.0.pre.2 examples/echo_server.rb