diff --git a/.gitignore b/.gitignore index ee6fde6..b31ffe1 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,5 @@ .bundle Gemfile.lock pkg/* - +/log/ /irc_machine.json diff --git a/README.md b/README.md index adcd89f..131b7ad 100644 --- a/README.md +++ b/README.md @@ -9,11 +9,19 @@ Design philosophy: simple to the point of under-engineered, make it work for the git clone git://github.com/pda/irc_machine cd irc_machine cp example.json irc_machine.json - ./bin/irc_machine + + # run it + ./bin/irc_machined run + # ctrl+c + + # daemonize it + ./bin/irc_machined start + # stop the daemon + ./bin/irc_machined stop # or maybe even this (chances aren't good, though) gem install irc_machine - irc_machine + irc_machined run HTTP interface diff --git a/bin/irc_machine b/bin/irc_machine index 7a35192..86e4b12 100755 --- a/bin/irc_machine +++ b/bin/irc_machine @@ -1,8 +1,6 @@ #!/usr/bin/env ruby +$: << File.expand_path("../../lib", __FILE__) -require "json" -params = JSON.load(open("./irc_machine.json")). - inject({}) { |h, (k,v)| h[k.to_sym] = v; h } +require 'irc_machine/cli_shared' -require File.join(File.dirname(__FILE__), "../lib/irc_machine") -IrcMachine::Session.new(params).start +IrcMachine::Session.new(IRC_MACHINE).start diff --git a/bin/irc_machined b/bin/irc_machined new file mode 100755 index 0000000..dd4f78e --- /dev/null +++ b/bin/irc_machined @@ -0,0 +1,26 @@ +#!/usr/bin/env ruby +$: << File.expand_path("../../lib", __FILE__) + +require 'rubygems' +require 'daemons' + +require 'irc_machine/cli_shared' + +daemon_defaults = { + :app_name => 'irc_machine', + :dir_mode => :normal, + :dir => ".", + :multiple => false, + :backtrace => true, + :monitor => false, + :log_dir => File.expand_path("../../log", __FILE__), + :log_output => true +} + +IRC_MACHINE[:daemon] ||= {} + +daemon_params = daemon_defaults.merge(IRC_MACHINE[:daemon]) + +FileUtils.mkdir_p(daemon_params[:log_dir]) + +Daemons.run(File.expand_path("../irc_machine", __FILE__), daemon_params) \ No newline at end of file diff --git a/irc_machine.gemspec b/irc_machine.gemspec index d816ba5..a322a1b 100644 --- a/irc_machine.gemspec +++ b/irc_machine.gemspec @@ -19,6 +19,7 @@ Gem::Specification.new do |s| s.add_dependency "eventmachine" s.add_dependency "eventmachine_httpserver" s.add_dependency "rack" + s.add_dependency "daemons" s.add_development_dependency "bundler" s.add_development_dependency "rake" diff --git a/lib/irc_machine.rb b/lib/irc_machine.rb index c3124c0..a4e7295 100644 --- a/lib/irc_machine.rb +++ b/lib/irc_machine.rb @@ -1,5 +1,3 @@ -$LOAD_PATH << File.dirname(__FILE__) - %w{ ostruct eventmachine diff --git a/lib/irc_machine/cli_shared.rb b/lib/irc_machine/cli_shared.rb new file mode 100644 index 0000000..6a171bf --- /dev/null +++ b/lib/irc_machine/cli_shared.rb @@ -0,0 +1,6 @@ +require "json" +require 'irc_machine/monkey_patches' +require 'irc_machine' + +ENV['IRC_MACHINE_CONF'] ||= File.expand_path("./irc_machine.json") +IRC_MACHINE = JSON.load(open(ENV['IRC_MACHINE_CONF'])).symbolize_keys \ No newline at end of file diff --git a/lib/irc_machine/monkey_patches.rb b/lib/irc_machine/monkey_patches.rb new file mode 100644 index 0000000..a8dd509 --- /dev/null +++ b/lib/irc_machine/monkey_patches.rb @@ -0,0 +1,13 @@ +class Hash + def symbolize_keys + inject({}) do |h, (k,v)| + h[k.to_sym] = case v + when Hash + v.symbolize_keys + else + v + end + h + end + end +end \ No newline at end of file