Module: Poisol::Server

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

Instance Method Summary collapse

Instance Method Details

#access_logObject



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

def access_log
  log_file = File.open 'log/poisol_webrick.log', 'a+'
  [
    [log_file, "#{WEBrick::AccessLog::COMBINED_LOG_FORMAT} %T"],
  ]
end

#attach_request_handlingObject



32
33
34
# File 'lib/poisol/server.rb', line 32

def attach_request_handling
  @server.mount '/', ExtendedServer
end

#attach_shutdownObject



36
37
38
# File 'lib/poisol/server.rb', line 36

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

#base_urlObject



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

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

#logObject



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

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



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

def start port
  PoisolLog.info "Starting server...  as http://localhost:#{port}"
  @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



53
54
55
56
57
58
# File 'lib/poisol/server.rb', line 53

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