Skip to content

Instantly share code, notes, and snippets.

@lucasmarqs
Created May 4, 2018 16:51
Show Gist options
  • Save lucasmarqs/137229e6172970a55471219603f50d1f to your computer and use it in GitHub Desktop.
Save lucasmarqs/137229e6172970a55471219603f50d1f to your computer and use it in GitHub Desktop.
Ruby web server from std lib
require 'webrick'
require 'json'
server = WEBrick::HTTPServer.new Port: 9000
server.mount_proc '/' do |request, response|
response.body = 'Hello World from WEBrick'
end
server.mount_proc '/ping' do |request, response|
response.body = JSON.generate({pong: true})
response.content_type = 'application/json'
response.status = 200
end
puts "Listening on http://localhost:8000"
puts "Press Ctrl+C to interrupt"
trap 'INT' do
puts 'Bye!'
server.shutdown
end
server.start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment