Postambles
Normally you would start a Camping application with the camping commandline script, this starts a slow but very rewarding webserver. If you would like some more speed, use one of these PostAmbles in your script. You would normally put them at the end of the script, hence PostAmble.
If you are using Camping.goes, replace the Camping constant in each postamble with your application's module.
Standard CGI Postamble
Very slow, but simple.
#!ruby
if __FILE__ == $0
Blog::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'blog3.db'
Blog::Models::Base.logger = Logger.new('camping.log')
Blog.create if Blog.respond_to? :create
puts Blog.run
end
FastCGI Postamble
Where /var/camping/blog/ is the working directory for your script, owned by the FastCGI user. At the bottom of blog.rb:
#!ruby
if __FILE__ == $0
require 'camping/fastcgi'
Dir.chdir('/var/camping/blog/')
Blog::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'blog3.db'
Blog::Models::Base.logger = Logger.new('camping.log')
Blog.create if Blog.respond_to? :create
Camping::FastCGI.start(Blog)
end
Sample Lighttpd.conf
server.port = 3044
server.bind = "127.0.0.1"
server.modules = ( "mod_fastcgi" )
server.document-root = "/var/camping/blog/"
server.errorlog = "/var/camping/blog/error.log"
#### fastcgi module
fastcgi.server = ( "/" => (
"localhost" => (
"socket" => "/tmp/camping-blog.socket",
"bin-path" => "/var/camping/blog/blog.rb",
"check-local" => "disable",
"max-procs" => 1 ) ) )
Mongrel Postamble
Mongrel is a fast Ruby web server, competitive with Webrick. This postamble will work with the 0.3.10 release of Mongrel that has new Camping support directly inside. This example is taken from the tepee.rb example in the Mongrel project and is pretty much the same for the blog.rb example. -- Zed A. Shaw
Note: Looks like Mongrel::Camping::start returns an instance of Mongrel::HttpServer?, instead of a thread now. I had to call server.run.join instead of just server.join.
#!ruby
Tepee::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'tepee.db'
Tepee::Models::Base.logger = Logger.new('camping.log')
Tepee.create
server = Mongrel::Camping::start("0.0.0.0",3000,"/tepee",Tepee)
puts "** Tepee example is running at http://localhost:3000/tepee"
server.run.join
Don't forget to:
require 'rubygems' require 'mongrel' require 'camping'
WEBrick PostAmble
If you just want to run your Camping app as a standalone, with the simpler and more familiar syntax "ruby blog.rb" or "./blog.rb", you can make a small WEBrick PostAmble to do that.
if __FILE__ == $0
require 'webrick/httpserver'
require 'camping/webrick'
Camping::Models::Base.establish_connection :adapter => "sqlite3", :database => "blog.db"
s = WEBrick::HTTPServer.new :BindAddress => "0.0.0.0", :Port => 3301
s.mount "/", WEBrick::CampingHandler, Blog
# This lets Ctrl+C shut down your server
trap(:INT) do
s.shutdown
end
s.start
end
