Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-9 of 9 total  RSS 

Ruby Servlets

This WEBrick example demonstrates a basic servlet which displays the current time. Source code copied from At the Forge - Getting Started with Ruby [linuxjournal.com].

#!/sw/bin/ruby
require 'webrick'
include WEBrick

# ---------------------------------------------
# Define a new class
class CurrentTimeServlet
  < WEBrick::HTTPServlet::AbstractServlet

  def do_GET(request, response)
    response['Content-Type'] = 'text/plain'
    response.status = 200
    response.body = Time.now.to_s + "\n"
  end
end

# ----------------------------------------------
# Create an HTTP server
s = HTTPServer.new(
  :Port            => 8000,
  :DocumentRoot    => "/usr/local/apache/htdocs/"
)

s.mount("/time", CurrentTimeServlet)

# When the server gets a control-C, kill it
trap("INT"){ s.shutdown }

# Start the server
s.start

e.g. http://localhost:8001/time
output
Mon Mar 10 23:06:58 +0000 2008

Reference: http://www.webrick.org/

Runt.rb

// A tiny Ruby web server.

#!/usr/bin/env ruby

require "webrick"

s=WEBrick::HTTPServer.new(
        :BindAddress => "localhost",
        :Port => 8080,
        :DocumentRoot => File.dirname($0)+"/"+"www/"
)

trap("INT") { s.shutdown }

s.start

WEBrick servlet with HTTP authentication

// HTTP authentication for a directory listing


require 'webrick'
include WEBrick

dir = Dir::pwd
port = 1234

authenticate = Proc.new do |req, res|
  HTTPAuth.basic_auth(req, res, '') do |user, password|
    user == 'foo' && password == 'bar'
  end
end

s = HTTPServer.new(:Port => port, :ServerType => Daemon)
s.mount('/', HTTPServlet::FileHandler, dir,
  :FancyIndexing => true,
  :HandlerCallback => authenticate # Hook up the authentication proc.
)

trap('INT') { s.shutdown }
s.start

WEBrick servlet skeleton

// Skeleton code for a WEBrick servlet.

require 'webrick'
include WEBrick
class UberServlet < HTTPServlet::AbstractServlet

  def do_GET(req, res)
    id = req.query['id'] # Get GET/POST params like that.
    res['content-type'] = 'text/html'
    res.status = 200
    res.body = some_text
  end
  alias :do_POST :do_GET

end

# "Mount" the servlet.
server = HTTPServer.new(:Port => 1234)
server.mount('/some/path', UberServlet)

# Handle signals.
%w(INT TERM).each do |signal|
  trap(signal) { server.shutdown }
end

server.start

Stop WeBRICK Server in RadRails (if IDE doesn't)

From: http://lists.radrails.org/pipermail/radrails/2006-February/000313.html
Just hit Restart and it will actually be started. Then you can stop it. If
all else fails, delete the file

workspace/.metadata/.plugins/org.radrails.server.core/.servers.

Mongrel on Win32 as Service

Mongrel now has support for running as a Win32 service right out of the box. The support is still rough but works well enough that we decided to release it. You can thank Luis Lavena for working on this and making it so nice.

After you do the gem install, find a Rails application you want to run and do:
$ mongrel_rails_service install -n myapp -r c:\my\path\to\myapp -p 4000 -e production
$ mongrel_rails_service start -n myapp

Now hit the port and poof, works. Stopping the app is just done with:
$ mongrel_rails_service stop -n myapp

And, you can even set the CPU processor affinity for the service when yourun the install command. Can’t even do that on POSIX yet. Now that’s hot.

If you run into an app that’s not running right, my suggestion is to run it with the regular mongrel_rails runner:
$ cd c:\my\path\to\myapp
$ mongrel_rails start -p 4500

Since that will spit out error messages and stuff to the console. Use CTRL-Pause/Break to stop.

Ruby WEBrick as WebDAV Server

From: http://sg.validcode.at/articles/2006/02/25/vision-everywhere

gem install webrick-webdav


If you have done this dowload webdav.rb.txt rename it to webdav.rb put it into the Vision folder and start it with
ruby PATH/TO/VISION/webdav.rb

Apache2 proxy to local port

Apache as the receptionist, forwarding requests to and from an internal server (e.g. webrick or lighttpd).

<VirtualHost *>
        ServerName www.example.com
        ProxyPass / http://localhost:3000/
        ProxyPassReverse / http://localhost:3000/
        ProxyPreserveHost On
</VirtualHost>


Change 3000 to whatever port you need and make sure the internal server is set up to answer requests on that port (not port 80). The ProxyPreserveHost line is critical to keep all your URLs working correctly.

How to fix a leaky WEBrick

If WEBrick is leaking out of control, which it does do all the time, shut it down and restart it.
Ctrl-c + Up + Enter

WEBrick can slow your computer down to a crawl so it is reccomended that you only use it when it's needed. Otherwise just runs tests, WEBrick's leaks are just another great reason to adapt to test driven development.
« Newer Snippets
Older Snippets »
Showing 1-9 of 9 total  RSS