Created
December 13, 2016 01:31
-
-
Save edwindotcom/800ec77d4d23bb6ced7db9f8f4d20b51 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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.make (email, type) | |
system 'ruby make_user -n ' + type + ' -e ' + email | |
end | |
def self.kill (a, b) | |
a.to_i - b.to_i | |
end | |
end | |
class MyServlet < WEBrick::HTTPServlet::AbstractServlet | |
def do_GET (request, response) | |
if request.query["email"] && request.query["type"] | |
email = request.query["email"] | |
type = request.query["type"] | |
response.status = 200 | |
response.content_type = "text/plain" | |
result = nil | |
case request.path | |
when "/create" | |
result = MyNormalClass.make(email, type) | |
when "/kill" | |
result = MyNormalClass.kill(email, type) | |
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 => 5000) | |
server.mount "/", MyServlet | |
trap("INT") { | |
server.shutdown | |
} | |
server.start |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment