# # Project <%= @conf.appname %> # # Created using bivouac on <%= Time.now %>. # 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 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 <%= @conf.appname %>Daemon < SimpleDaemon::Base begin begin require 'mongrel/camping' @@loaded_server = 'mongrel' rescue LoadError require 'thin' @@loaded_server = 'thin' end rescue LoadError require 'webrick/httpserver' require 'camping/webrick' @@loaded_server = 'webrick' end @@server = nil @@force_use = nil @@server_ip = nil @@server_port = nil def self.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( ) if config.environment.server_force @@force_use = config.environment.server_force end ## 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") ## 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 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' 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 puts "Don't know how to use server `#{@@use_server}'!" exit 0 end end end def self.stop begin @@server.stop rescue @@server.shutdown end @@server = nil end def self.console include <%= @conf.appname %>::Models config = Bivouac::Environment.new( ) 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 <%= @conf.appname %>.create if <%= @conf.appname %>.respond_to? :create require 'irb' require 'irb/completion' ENV['IRBRC'] = DIRNAME + "/console.rc" 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 '--' <%= @conf.appname %>Daemon.start break else puts "Ignore unknown option '#{deamonize}' !" end end