Module: Poisol::Server

Extended by:
Server
Included in:
Server
Defined in:
lib/poisol/server.rb

Instance Method Summary collapse

Instance Method Details

#access_logObject



39
40
41
42
43
44
# File 'lib/poisol/server.rb', line 39

def access_log
  log_file = File.open 'log/poisol_webrick.log', 'a+'
  [
    [log_file, WEBrick::AccessLog::COMBINED_LOG_FORMAT],
  ]
end

#attach_request_handlingObject



20
21
22
23
24
25
26
27
# File 'lib/poisol/server.rb', line 20

def attach_request_handling
  @server.mount_proc '/' do |req, res|
    stub_response = ResponseMapper.map(req)
    res.status = stub_response.status
    res.body = stub_response.body.to_json
    res.content_type = 'application/json'
  end
end

#attach_shutdownObject



29
30
31
# File 'lib/poisol/server.rb', line 29

def attach_shutdown 
  trap 'INT' do @server.shutdown end
end

#base_urlObject



16
17
18
# File 'lib/poisol/server.rb', line 16

def base_url
  "http://localhost:#{@port}"
end

#logObject



33
34
35
36
37
# File 'lib/poisol/server.rb', line 33

def log 
  FileUtils.mkdir_p "log" unless File.exists?("log")
  log_file = File.open 'log/poisol_webrick.log', 'a+'
  WEBrick::Log.new log_file
end

#start(port) ⇒ Object



5
6
7
8
9
10
11
12
13
14
# File 'lib/poisol/server.rb', line 5

def start port
  PoisolLog.info "Starting server...  as http://localhost:#{port}"
  require 'webrick'
  @server =  WEBrick::HTTPServer.new :Port => port, :Logger => log, :AccessLog => access_log
  @port = port
  attach_shutdown
  attach_request_handling
  Thread.new{@server.start}
  PoisolLog.info "Server Started at http://localhost:#{port}"
end

#stopObject



46
47
48
49
50
51
# File 'lib/poisol/server.rb', line 46

def stop 
  if @server.present?
    @server.shutdown 
    PoisolLog.info "Server is shutdown"
  end
end