# # Project BivouacSample # # Created using bivouac on Sun Mar 23 20:30:13 +0100 2008. # Copyright (c) 2008 __My__. All rights reserved. # windows_process = false if /Windows/.match( ENV['OS'] ) begin require 'win32/process' windows_process = true rescue LoadError => e warn "`win32-process' is not installed!" end end require 'simple-daemon' DIRNAME = File.expand_path( File.dirname(__FILE__) ) SimpleDaemon::WORKING_DIRECTORY = DIRNAME + "/../log/" class BivouacSampleDaemon < SimpleDaemon::Base @@server = nil @@use = nil def self.use=(x) @@use=x end def self.start config = Bivouac::Environment.new( ) database_connection = config.environment.db if database_connection[:adapter] =~ /sqlite/ database_connection[:database] = DIRNAME + "/../" + database_connection[:database] end BivouacSample::Models::Base.establish_connection database_connection BivouacSample::Models::Base.logger = Logger.new(DIRNAME + "/../log/BivouacSample.log") # -- DON'T WORK WITH RAILS 2 -- # BivouacSample::Models::Base.threaded_connections = false BivouacSample.create if BivouacSample.respond_to? :create trap(:INT) do stop end begin if @@use.nil? begin require 'thin' Rack::Handler::Thin.run Rack::Adapter::Camping.new( BivouacSample ), :Host => config.environment.address, :Port => config.environment.port puts "** BivouacSample is running at http://#{config.environment.address}:#{config.environment.port}" rescue LoadError require 'mongrel/camping' @@server = Mongrel::Camping.start( config.environment.address, config.environment.port, "/", BivouacSample) puts "** BivouacSample is running at http://#{config.environment.address}:#{config.environment.port}" @@server.run.join end else raise LoadError, "I want to use WEBrick please!" end rescue LoadError => e require 'webrick/httpserver' require 'camping/webrick' @@server = WEBrick::HTTPServer.new :BindAddress => config.environment.address, :Port => config.environment.port puts "** BivouacSample is running at http://#{config.environment.address}:#{config.environment.port}" @@server.mount "/", WEBrick::CampingHandler, BivouacSample @@server.start end end def self.stop begin @@server.stop rescue @@server.shutdown end @@server = nil end end while ARGV.size > 0 deamonize = ARGV.shift case deamonize when 'webrick' BivouacSampleDaemon.use='webrick' when '-c' ARGV.clear include BivouacSample::Models config = Bivouac::Environment.new( ) database_connection = config.environment.db if database_connection[:adapter] =~ /sqlite/ database_connection[:database] = DIRNAME + "/../" + database_connection[:database] end BivouacSample::Models::Base.establish_connection database_connection BivouacSample.create if BivouacSample.respond_to? :create require 'irb' require 'irb/completion' if File.exists? ".irbrc" ENV['IRBRC'] = ".irbrc" end IRB.start break when '-d' if /Windows/.match( ENV['OS'] ) and windows_process == false warn "You must install `win32-process' to daemonize this app." exit 1 end BivouacSampleDaemon.daemonize break when '-h' begin require 'mongrel/camping' puts "=> Booting Mongrel (use 'script/server webrick [options]' to force WEBrick)" rescue LoadError => e puts "=> Booting WEBrick" end puts "script/server [-d start|stop|restart] [-h]" exit when '--' BivouacSampleDaemon.start break else puts "Ignore unknown option '#{deamonize}' !" end end