examples/bivouac_sample/config/postamble.rb in bivouac-0.1.4 vs examples/bivouac_sample/config/postamble.rb in bivouac-0.1.5
- old
+ new
@@ -1,44 +1,76 @@
#
# Project BivouacSample
#
-# Created using bivouac on Mon Sep 10 21:47:54 +0200 2007.
-# Copyright (c) 2007 __My__. All rights reserved.
+# 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::WorkingDirectory = DIRNAME + "/../log/"
+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( )
- BivouacSample::Models::Base.establish_connection :adapter => 'sqlite3', :database => DIRNAME + "/../db/BivouacSample.db"
+ 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")
- # BivouacSample::Models::Base.threaded_connections = false
+ # -- 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
- require 'mongrel/camping'
+ 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
+ @@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
@@ -48,15 +80,54 @@
end
@@server = nil
end
end
-deamonize = ARGV.shift
-case deamonize
-when '-d'
- BivouacSampleDaemon.daemonize
-when '-h'
- puts "script/server [-d start|stop|restart] [-h]"
- exit
-else
- BivouacSampleDaemon.start
+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