Skip to content

Instantly share code, notes, and snippets.

@simongregory
Created November 6, 2012 15:26
Show Gist options
  • Save simongregory/4025405 to your computer and use it in GitHub Desktop.
Save simongregory/4025405 to your computer and use it in GitHub Desktop.
A quick stub server using Webrick
require 'webrick'
stats_calls = 0
#http://microjet.ath.cx/webrickguide/html/
s = WEBrick::HTTPServer.new(
:Port => '4579',
:BindAddress => 'localhost',
:Logger => WEBrick::Log.new('/dev/null', WEBrick::BasicLog::DEBUG),
:AccessLog => [nil,nil]
)
s.mount_proc("/stats"){|req, res|
res.body = 'Stats Call Made'
stats_calls += 1
}
s.mount_proc("/stats/count"){|req, res|
res['Content-Type'] = "text/plain"
res.body = "#{stats_calls}"
}
s.mount_proc("/status"){|req, res|
res['Content-Type'] = "text/plain"
res.body = 'UP'
}
s.mount_proc("/kill"){|req, res|
res.body = "SHUT DOWN"
s.shutdown
}
s.mount_proc("/quit"){|req, res|
res.body = "SHUT DOWN"
s.shutdown
}
s.mount_proc("/exit"){|req, res|
res.body = "SHUT DOWN"
s.shutdown
}
s.mount_proc("/shutdown"){|req, res|
res.body = "SHUT DOWN"
s.shutdown
}
s.mount_proc("/"){|req, res|
res['content-type'] = 'text/html'
res.body = "<h2>Stub Server</h2>"
res.body << "<a href='/shutdown'>shutdown</a><br/>"
res.body << "<h4>Stats</h4><br/>"
res.body << "/stats #{stats_calls}<br/>"
}
trap("INT"){
s.shutdown
}
trap("TERM"){
s.shutdown
}
trap("KILL"){
s.shutdown
}
s.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment