lib/bivouac/template/application/postamble.rb in bivouac-0.2.1 vs lib/bivouac/template/application/postamble.rb in bivouac-0.2.2
- old
+ new
@@ -5,10 +5,12 @@
# Copyright (c) <%= Time.now.year %> __My__. All rights reserved.
#
# DO NOT EDIT THIS FILE OR YOU REALLY KNOW WHAT YOU ARE DOING
# USE script/generate helper my_helper
#
+
+## If the app run on Windows, check if win32/process is installed
windows_process = false
if /Windows/.match( ENV['OS'] )
begin
require 'win32/process'
windows_process = true
@@ -21,58 +23,107 @@
DIRNAME = File.expand_path( File.dirname(__FILE__) )
SimpleDaemon::WORKING_DIRECTORY = DIRNAME + "/../log/"
class <%= @conf.appname %>Daemon < SimpleDaemon::Base
+ begin
+ begin
+ require 'thin'
+ @@loaded_server = 'thin'
+ rescue LoadError
+ require 'mongrel/camping'
+ @@loaded_server = 'mongrel'
+ end
+ rescue LoadError
+ require 'webrick/httpserver'
+ require 'camping/webrick'
+ @@loaded_server = 'webrick'
+ end
+
@@server = nil
- @@use = nil
+ @@force_use = nil
+ @@server_ip = nil
+ @@server_port = nil
def self.use=(x)
- @@use=x
+ @@force_use=x
end
+ def self.ip=(x)
+ @@server_ip=x
+ end
+
+ def self.port=(x)
+ @@server_port=x
+ end
+
def self.start
+ ## Load configuration
config = Bivouac::Environment.new( )
+ ## Initialize database connection
database_connection = config.environment.db
if database_connection[:adapter] =~ /sqlite/
database_connection[:database] = DIRNAME + "/../" + database_connection[:database]
end
<%= @conf.appname %>::Models::Base.establish_connection database_connection
+
+ ## Define log
<%= @conf.appname %>::Models::Base.logger = Logger.new(DIRNAME + "/../log/<%= @conf.appname %>.log")
- # -- DON'T WORK WITH RAILS 2 -- # <%= @conf.appname %>::Models::Base.threaded_connections = false
+
+ ## Migrate database if needed
<%= @conf.appname %>.create if <%= @conf.appname %>.respond_to? :create
+ ## Initialize trap
trap(:INT) do
stop
end
+ ## Set server to use
+ if @@force_use.nil?
+ @@use_server = @@loaded_server
+ else
+ @@use_server = @@force_use
+ end
+
+ ## Set ip and port
+ @@server_port = config.environment.port if @@server_port.nil?
+ @@server_ip = config.environment.address if @@server_ip.nil?
+
begin
- if @@use.nil?
- begin
- require 'thin'
-
- Rack::Handler::Thin.run Rack::Adapter::Camping.new( <%= @conf.appname %> ), :Host => config.environment.address, :Port => config.environment.port
- puts "** <%= @conf.appname %> is running at http://#{config.environment.address}:#{config.environment.port}"
- rescue LoadError
+ case @@use_server
+ when 'thin'
+ if @@loaded_server != @@use_server
+ require 'thin'
+ end
+
+ # Rack::Handler::Thin.run Rack::Adapter::Camping.new( <%= @conf.appname %> ), :Host => @@server_ip, :Port => @@server_port
+ @@server = Thin::Server.new( @@server_ip, @@server_port.to_i, Rack::Adapter::Camping.new( <%= @conf.appname %> ) )
+ puts "** <%= @conf.appname %> is running at http://#{@@server_ip}:#{@@server_port}"
+ @@server.start
+ when 'mongrel'
+ if @@loaded_server != @@use_server
require 'mongrel/camping'
-
- @@server = Mongrel::Camping.start( config.environment.address, config.environment.port, "/", <%= @conf.appname %>)
- puts "** <%= @conf.appname %> is running at http://#{config.environment.address}:#{config.environment.port}"
- @@server.run.join
end
+
+ @@server = Mongrel::Camping.start( @@server_ip, @@server_port, "/", <%= @conf.appname %>)
+ puts "** <%= @conf.appname %> is running at http://#{@@server_ip}:#{@@server_port}"
+ @@server.run.join
+ when 'webrick'
+ if @@loaded_server != @@use_server
+ require 'webrick/httpserver'
+ require 'camping/webrick'
+ end
+
+ @@server = WEBrick::HTTPServer.new :BindAddress => @@server_ip, :Port => @@server_port
+ puts "** <%= @conf.appname %> is running at http://#{@@server_ip}:#{@@server_port}"
+ @@server.mount "/", WEBrick::CampingHandler, <%= @conf.appname %>
+ @@server.start
else
- raise LoadError, "I want to use WEBrick please!"
+ puts "Don't know how to use server `#{@@use_server}'!"
+ exit 0
end
- rescue LoadError => e
- require 'webrick/httpserver'
- require 'camping/webrick'
-
- @@server = WEBrick::HTTPServer.new :BindAddress => config.environment.address, :Port => config.environment.port
- puts "** <%= @conf.appname %> is running at http://#{config.environment.address}:#{config.environment.port}"
- @@server.mount "/", WEBrick::CampingHandler, <%= @conf.appname %>
- @@server.start
end
end
def self.stop
begin
@@ -80,20 +131,12 @@
rescue
@@server.shutdown
end
@@server = nil
end
-end
-
-while ARGV.size > 0
- deamonize = ARGV.shift
- case deamonize
- when 'webrick'
- <%= @conf.appname %>Daemon.use='webrick'
- when '-c'
- ARGV.clear
-
+
+ def self.console
include <%= @conf.appname %>::Models
config = Bivouac::Environment.new( )
database_connection = config.environment.db
if database_connection[:adapter] =~ /sqlite/
@@ -106,27 +149,42 @@
require 'irb/completion'
if File.exists? ".irbrc"
ENV['IRBRC'] = ".irbrc"
end
IRB.start
+ end
+end
+
+while ARGV.size > 0
+ deamonize = ARGV.shift
+ case deamonize
+ when 'webrick'
+ <%= @conf.appname %>Daemon.use='webrick'
+ when 'mongrel'
+ <%= @conf.appname %>Daemon.use='mongrel'
+ when 'thin'
+ <%= @conf.appname %>Daemon.use='thin'
+ when '-c'
+ ARGV.clear
+ <%= @conf.appname %>Daemon.console
break
+ when '-h'
+ puts "Usage: server [thin|mongrel|webrick] [option]"
+ puts " -p port : Runs Bivouac on the specified port (default 3301)"
+ puts " -b ip : Binds Bivouac to the specified ip (default 0.0.0.0)"
+ puts " -d start|stop|restart : Make server run as a Daemon."
+ break
+ when '-p'
+ <%= @conf.appname %>Daemon.port=ARGV.shift
+ when '-b'
+ <%= @conf.appname %>Daemon.ip=ARGV.shift
when '-d'
if /Windows/.match( ENV['OS'] ) and windows_process == false
warn "You must install `win32-process' to daemonize this app."
exit 1
end
-
<%= @conf.appname %>Daemon.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 '--'
<%= @conf.appname %>Daemon.start
break
else
puts "Ignore unknown option '#{deamonize}' !"