Module: SolrLikeRackServer

Defined in:
lib/solr_like_rack_server.rb,
lib/solr_like_rack_server/version.rb,
lib/solr_like_rack_server/response_writer_wrapper.rb

Defined Under Namespace

Classes: MySolrParams, MySolrQueryRequest, ResponseWriterWrapper

Constant Summary collapse

VERSION =
"0.0.1"

Class Method Summary collapse

Class Method Details

.start(mount_procs) ⇒ Object Also known as: server



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/solr_like_rack_server.rb', line 10

def start mount_procs
  wakeupProc = nil
  server = WEBrick::HTTPServer.new(
    Port: 12345,
    Logger: WEBrick::Log.new('/dev/null'),
    AccessLog: [],
    StartCallback: Proc.new {
      wakeupProc.call unless wakeupProc.nil?
    }
  )
  mount_procs.each {|path, proc|
    server.mount_proc(path) {|req, res|
      res["Content-Type"] = "application/octet-stream"
      data = if Array === proc
        proc
      elsif Hash === proc
        [proc]
      elsif proc.arity == 0
        proc.call
      else
        proc.call(req.query)
      end
      res.body = SolrLikeRackServer::ResponseWriterWrapper.new.write data
    }
  }
  if block_given?
    wakeupProc = Proc.new { Thread.main.wakeup }
    server_thread = Thread.new do
      Thread.current[:server] = server
      server.start
    end
    Thread.stop
    begin
      yield
    ensure
      server_thread[:server].shutdown
    end
  else
    server
  end
end