Harman
Harman

Reputation: 1753

How can I execute Ruby code with WEBrick instead of dumping the code to my browser?

I'm facing a problem when I run my program in a browser with the WEBrick server. It shows me my code as written in the 2loop.rb file.

When I run ruby -run -e -httpd. -p 5000 at the command prompt, and load http://localhost:5000/2loop.rb in the browser, it shows the code from 2loop.rb instead of running it.

How can I execute the 2loop.rb program instead?

Upvotes: 3

Views: 1846

Answers (2)

Todd A. Jacobs
Todd A. Jacobs

Reputation: 84453

TL;DR

You're doing this to yourself by serving your current working directory as the root of your web server. You aren't actually running the code in your file; you're just telling WEBrick to serve any file you name in the URI. http://localhost:5000/2loop.rb will serve "2loop.rb" as text/html in your posted example.

Using un.rb

The flag you're using isn't actually "run." Instead, the -r flag actually loads a module, which in this case is the un.rb module. Using un.rb to start WEBrick is done like this:

$ ruby -run -e httpd . -p 5000

and starts a web server in the document root. In this case, the dot means to use the current working directory as the root. This is not really what you want to start code you've placed inside a Ruby file.

Running WEBrick Programmatically

Using some snippets from the WEBrick documentation, you will see that you can create a file named "2loop.rb" containing the following:

#!/usr/bin/env ruby

require 'webrick'

root   = File.path '/tmp/public_html'
server = WEBrick::HTTPServer.new :Port => 5000, :DocumentRoot => root

trap 'INT' do server.shutdown end
server.start

This will serve files out of the /tmp/public_html directory on port 5000, which you can reach at http://localhost:5000. You can then make the file executable and start the server with ./2loop.rb, or just run ruby 2loop.rb if you don't want to make your file executable for some reason.

If you don't want WEBrick just to serve files, you will have to add custom behavior to your web server inside the 2loop.rb script. This is a fairly low-level thing to do, but may suit your needs.

Sensible Alternatives

You should probably use a web framework like Ruby on Rails or Sinatra if you don't want to have write all the low-level behaviors yourself. Sinatra in particular is a very lightweight alternative. This example:

#!/usr/bin/env ruby

require 'sinatra'
set :port, 5000
get '/hello' do
  "Hello, World!"
end

will create a URL at http://localhost:5000/hello with a custom action that returns "Hello, World!" as an in-browser response.

Upvotes: 3

user2422869
user2422869

Reputation: 760

Well, I'd suggest you to use Common Gateway Interface (CGI). Let me provide you an example.

Firstly, create a file named server.rb:

require 'webrick'
server = WEBrick::HTTPServer.new(
  :Port => 6789, # a server's port
  :DocumentRoot => File.join(Dir.pwd, "/scripts") # a folder with scripts
)
server.start

Secondly, create a folder scripts and put the following file (the_best_program.cgi) into it. Note the .cgi extension. It matters. Look here for details on the first line of the script (shebang) if you are working under Windows.

#!/usr/bin/env ruby 
require 'cgi'
print "Content-type: text/plain\n\n" 
5.times { |i| puts "Hello world #{i}!"}
puts 'So many worlds there. :('

Finally,

  • Launch your server from command-line (ruby server.rb).
  • Start browser and go to localhost:6789/the_best_program.cgi (or 0.0.0.0:6789/the_best_program.cgi)
  • Enjoy!

Notes

  • You might need to change permissions to your scripts folder / script. On unix-like system do: chmod 755 scripts scripts/the_best_program.cgi.
  • You can launch not only ruby scripts this way.

Upvotes: 2

Related Questions