In your case I will use rack, this is a ruby:
- Rack includes handlers that connect Rack to all these web application servers (WEBrick, Mongrel etc.).
- Rack includes adapters that connect Rack to various web frameworks (Sinatra, Rails etc.).
- Between the server and the framework, Rack can be customized to your applications needs using middleware.
In order to use rack you must create rack application:
- must answer call method.
- The call mehtod is called by the server and must recevive an env variable with the CGI information.
- Must return an array with 3 elements:
a) status: integer
b) headers: hash
c) body: An object that responds to each and for every call to each retuns a String.
The fundamental idea behind Rack middleware is – come between the calling client and the server, process the HTTP request before sending it to the server, and processing the HTTP response before returning it to the client.
this is the basic, answer of your question:
test_rack.rb
class MyApp
def call env
[200, {"Content-Type" => "text/html"}, ["Hello Rack Participants"]]
end
end
config.ru
require './test_rack.rb'
run MyApp.new
then start up the application, and make a call to localhost:9292/anything
╭─toni@Antonios-MBP ~/learn/ruby/stackoverflow/scripting ‹ruby-2.2.3@stackoverflow› ‹1.7› ‹SBCL 1.3.5›
╰─$ rackup config.ru
[2016-05-08 11:17:41] INFO WEBrick 1.3.1
[2016-05-08 11:17:41] INFO ruby 2.2.3 (2015-08-18) [x86_64-darwin15]
[2016-05-08 11:17:41] INFO WEBrick::HTTPServer#start: pid=2610 port=9292
::1 - - [08/May/2016:11:18:04 +0200] "GET /patata HTTP/1.1" 200 - 0.0010
::1 - - [08/May/2016:11:18:04 +0200] "GET /favicon.ico HTTP/1.1" 200 - 0.0005
::1 - - [08/May/2016:11:18:09 +0200] "GET /patata/calimero HTTP/1.1" 200 - 0.0003
let's see rack working in console, see the multiple webservers and pass lambda for create the call function
require 'rack'
=> true
irb(main):010:0> Rack::Handler::constants
=> [:CGI, :FastCGI, :Mongrel, :EventedMongrel, :SwiftipliedMongrel, :WEBrick, :LSWS, :SCGI, :Thin]
irb(main):026:0> Rack::Handler::WEBrick.run lambda { |env| [200,{"Content-Type" => "text/plain"}, ["Hello. The time is #{Time.now}"]] }
[2016-05-08 11:22:39] INFO WEBrick 1.3.1
[2016-05-08 11:22:39] INFO ruby 2.2.3 (2015-08-18) [x86_64-darwin15]
[2016-05-08 11:22:39] INFO WEBrick::HTTPServer#start: pid=1798 port=8080
to go further:
https://blog.engineyard.com/2015/understanding-rack-apps-and-middleware
https://github.com/rack/rack