Sha256: 601a6e948adf79596092930534fd44285b8dc749cfd25e47ab1f7f136b980d2c

Contents?: true

Size: 1.38 KB

Versions: 116

Compression:

Stored size: 1.38 KB

Contents

#!/usr/bin/env ruby

$KCODE='UTF8'
require 'webrick'
include WEBrick
$:.unshift 'ext'
$:.unshift 'lib'
require 'json'

class JSONServlet < HTTPServlet::AbstractServlet
  @@count = 1

  def do_GET(req, res)
    obj = {
      "TIME" => Time.now.strftime("%FT%T"),
      "foo" => "Bär",
      "bar" => "© ≠ €!",
      'a' => 2,
      'b' => 3.141,
      'COUNT' => @@count += 1,
      'c' => 'c',
      'd' => [ 1, "b", 3.14 ],
      'e' => { 'foo' => 'bar' },
      'g' => "松本行弘",
      'h' => 1000.0,
      'i' => 0.001,
      'j' => "\xf0\xa0\x80\x81",
    }
    res.body = JSON.generate obj
    res['Content-Type'] = "application/json"
  end
end

def create_server(err, dir, port)
  dir = File.expand_path(dir)
  err.puts "Surf to:", "http://#{Socket.gethostname}:#{port}"

  s = HTTPServer.new(
    :Port         => port,
    :DocumentRoot => dir,
    :Logger       => WEBrick::Log.new(err),
    :AccessLog    => [
      [ err, WEBrick::AccessLog::COMMON_LOG_FORMAT  ],
      [ err, WEBrick::AccessLog::REFERER_LOG_FORMAT ],
      [ err, WEBrick::AccessLog::AGENT_LOG_FORMAT   ]
    ]
  )
  s.mount("/json", JSONServlet)
  s
end

default_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', 'data'))
dir = ARGV.shift || default_dir
port = (ARGV.shift || 6666).to_i
s = create_server(STDERR, dir, 6666)
t = Thread.new { s.start }
trap(:INT) do
  s.shutdown
  t.join
  exit
end
sleep

Version data entries

116 entries across 110 versions & 16 rubygems

Version Path
benschwarz-smoke-0.5.2 vendor/json-1.1.3/tools/server.rb
benschwarz-smoke-0.5.3 vendor/json-1.1.3/tools/server.rb
benschwarz-smoke-0.5.4 vendor/json-1.1.3/tools/server.rb
benschwarz-smoke-0.5.5 vendor/json-1.1.3/tools/server.rb
benschwarz-smoke-0.5.6 vendor/json-1.1.3/tools/server.rb
benschwarz-smoke-0.5.7 vendor/gems/gems/json-1.1.3/tools/server.rb
fairchild-poolparty-1.3.17 vendor/gems/json/tools/server.rb
fairchild-poolparty-1.3.5 vendor/gems/json/tools/server.rb
genki-json-1.1.4.1 tools/server.rb
masover-castronaut-0.4.4.4 vendor/json/tools/server.rb
masover-castronaut-0.4.4.5 vendor/json/tools/server.rb
masover-castronaut-0.5.0.1 vendor/json/tools/server.rb
p8-castronaut-0.6.1.1 vendor/json/tools/server.rb
relevance-castronaut-0.4.1 vendor/json/tools/server.rb
relevance-castronaut-0.4.2 vendor/json/tools/server.rb
relevance-castronaut-0.4.3 vendor/json/tools/server.rb
relevance-castronaut-0.4.4 vendor/json/tools/server.rb
relevance-castronaut-0.4.5 vendor/json/tools/server.rb
relevance-castronaut-0.4.6 vendor/json/tools/server.rb
relevance-castronaut-0.5.0 vendor/json/tools/server.rb