Skip to content

Instantly share code, notes, and snippets.

@Integralist
Created June 3, 2012 10:16
Show Gist options
  • Save Integralist/2862917 to your computer and use it in GitHub Desktop.
Save Integralist/2862917 to your computer and use it in GitHub Desktop.
Create basic Web Server in Ruby (using WEBrick)
#!/usr/bin/env ruby
require "webrick"
=begin
WEBrick is a Ruby library that makes it easy to build an HTTP server with Ruby.
It comes with most installations of Ruby by default (it’s part of the standard library),
so you can usually create a basic web/HTTP server with only several lines of code.
The following code creates a generic WEBrick server on the local machine on port 1234,
shuts the server down if the process is interrupted (often done with Ctrl+C).
This example lets you call the URL's: "add" and "subtract" and pass through arguments to them
Example usage:
http://localhost:1234/ (this will show the specified error message)
http://localhost:1234/add?a=10&b=10
http://localhost:1234/subtract?a=10&b=9
=end
class MyNormalClass
def self.add (a, b)
a.to_i + b.to_i
end
def self.subtract (a, b)
a.to_i - b.to_i
end
end
class MyServlet < WEBrick::HTTPServlet::AbstractServlet
def do_GET (request, response)
if request.query["a"] && request.query["b"]
a = request.query["a"]
b = request.query["b"]
response.status = 200
response.content_type = "text/plain"
result = nil
case request.path
when "/add"
result = MyNormalClass.add(a, b)
when "/subtract"
result = MyNormalClass.subtract(a, b)
else
result = "No such method"
end
response.body = result.to_s + "\n"
else
response.status = 200
response.body = "You did not provide the correct parameters"
end
end
end
server = WEBrick::HTTPServer.new(:Port => 1234)
server.mount "/", MyServlet
trap("INT") {
server.shutdown
}
server.start
@s2t2
Copy link

s2t2 commented Nov 21, 2015

Thanks for this example.

@5up3rD4n1
Copy link

We were trying to use this code with ajax, just in case someone get this error:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

This lines at the beginning of the do_GET method will fix it:

response.header['Access-Control-Allow-Origin'] = '*'
response.header['Access-Control-Request-Method'] = '*'

Also if it's needed the ajax call should have url: 'http://127.0.0.1:port' not 'http://localhost:port'

Hope this can help someone.

@xxjapp
Copy link

xxjapp commented Feb 3, 2017

good example!

@Beyarz
Copy link

Beyarz commented Jun 3, 2020

We were trying to use this code with ajax, just in case someone get this error:

No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.

This lines at the beginning of the do_GET method will fix it:

response.header['Access-Control-Allow-Origin'] = '*'
response.header['Access-Control-Request-Method'] = '*'

Also if it's needed the ajax call should have url: 'http://127.0.0.1:port' not 'http://localhost:port'

Hope this can help someone.

It did, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment