Sha256: 3a58f10b0c3ad43ab1f5b6d39559661618cfd6dd6c475275b29ae4525a23d059

Contents?: true

Size: 613 Bytes

Versions: 1

Compression:

Stored size: 613 Bytes

Contents

require 'hi/request'
require 'hi/logger'

module Hi
  # Main app; http server that prints requests as they come in
  class App
    attr_reader :port, :logger

    DEFAULT_PORT = 3000

    def initialize(port = nil, logger = Hi::Logger.new)
      @port = (port = port.to_i) > 0 ? port : DEFAULT_PORT
      @logger = logger
    end

    def call(env)
      request = Hi::Request.new(env).to_h

      log "#{request[:request_method]} #{request[:url]} (#{Time.now})"
      log request

      [ 200, { 'Content-Type' => 'text/plain' }, ['hi'] ]
    end

    def log(message)
      logger.log message
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hi-1.2.2 lib/hi/app.rb