From 2926e8a3f0dc3cc3a6e4808673380c5dc7460f1f Mon Sep 17 00:00:00 2001 From: berga Date: Wed, 14 Nov 2012 02:44:45 +0100 Subject: [PATCH] fix rvm issue pyromaniac/hoof#13 listen to 0.0.0.0 for xip.io compat http xip.io compatible touch tmp/restart.txt to reload unicorn --- README.rdoc | 1 + VERSION | 2 +- hoof.gemspec | 9 +- lib/hoof.rb | 6 +- lib/hoof/application.rb | 43 ++++++--- lib/hoof/http_server.rb | 19 +++- lib/templates/not_found.html.erb | 145 +++++++++++++++++++++++++++++++ 7 files changed, 202 insertions(+), 23 deletions(-) create mode 100644 lib/templates/not_found.html.erb diff --git a/README.rdoc b/README.rdoc index 3a5f496..2a6fdc5 100644 --- a/README.rdoc +++ b/README.rdoc @@ -2,6 +2,7 @@ Linux version of https://github.com/37signals/pow/ +Compatible with Ruby 1.8.7 - ree == Installation diff --git a/VERSION b/VERSION index 5c4511c..7d6b3eb 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.0.7 \ No newline at end of file +0.0.8 \ No newline at end of file diff --git a/hoof.gemspec b/hoof.gemspec index cccfb84..f05ef51 100644 --- a/hoof.gemspec +++ b/hoof.gemspec @@ -5,14 +5,14 @@ Gem::Specification.new do |s| s.name = %q{hoof} - s.version = "0.0.7" + s.version = "0.0.8" s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= - s.authors = ["pyromaniac"] - s.date = %q{2011-06-08} + s.authors = ["pyromaniac","berga"] + s.date = %q{2012-11-14} s.default_executable = %q{hoof} s.description = %q{Hoof is linux variant of pow. It's based on nss, eventmachine and unicorn} - s.email = %q{kinwizard@gmail.com} + s.email = %q{airberga@gmail.com} s.executables = ["hoof"] s.extra_rdoc_files = [ "LICENSE.txt", @@ -39,6 +39,7 @@ Gem::Specification.new do |s| "lib/hoof/http_server.rb", "lib/hoof/https_server.rb", "lib/hoof/unicorn_config.rb", + "lib/templates/not_found.erb", "test/helper.rb", "test/test_hoof.rb" ] diff --git a/lib/hoof.rb b/lib/hoof.rb index 7d344ad..8fdb71b 100644 --- a/lib/hoof.rb +++ b/lib/hoof.rb @@ -1,6 +1,7 @@ require 'eventmachine' require 'unicorn/launcher' require 'evma_httpserver' +require 'erb' require 'hoof/http_server' require 'hoof/https_server' @@ -28,8 +29,9 @@ def self.start trap("TERM") { stop } trap("INT") { stop } - EventMachine::start_server "127.0.0.1", http_port, Hoof::HttpServer - EventMachine::start_server "127.0.0.1", https_port, Hoof::HttpsServer + #default: EventMachine::start_server "127.0.0.1", http_port, Hoof::HttpServer + EventMachine::start_server "0.0.0.0", http_port, Hoof::HttpServer + EventMachine::start_server "0.0.0.0", https_port, Hoof::HttpsServer EventMachine::start_server sock, Hoof::ControlServer end end diff --git a/lib/hoof/application.rb b/lib/hoof/application.rb index ad5161b..9a2b8ed 100644 --- a/lib/hoof/application.rb +++ b/lib/hoof/application.rb @@ -1,30 +1,38 @@ module Hoof class Application - attr_accessor :root, :app, :name + attr_accessor :root, :app, :name, :mtime, :tmp_restart def initialize name @name = name @root = File.readlink(File.expand_path(File.join("~/.hoof/", name))) + @tmp_restart = File.join(@root,"tmp","restart.txt") + @mtime = File.stat(tmp_restart).mtime if File.exists?(tmp_restart) + puts "original mtime:#{@mtime}" end def start + + if running? && need_reload? + puts "stopping Application..." + stop + sleep(1) if running? + sleep(2) if running? + sleep(3) if running? + puts "Application stopped?...#{!running?}" + end + unless running? - rvmrc = "" - if File.exists?(root + '/.rvmrc') - rvmrc = File.read(root + '/.rvmrc').chomp - rvmrc << " exec " - else - load_rvm - end - system "cd #{root} && #{rvmrc}bundle exec unicorn_rails -c #{File.join(File.dirname(__FILE__), 'unicorn_config.rb')} -l #{sock} -D" + puts "starting Application... running?: #{running?}" + load_rvm + system "cd #{root} && bundle exec unicorn_rails -c #{File.join(File.dirname(__FILE__), 'unicorn_config.rb')} -l #{sock} -D" end end def load_rvm if ENV['MY_RUBY_HOME'] && ENV['MY_RUBY_HOME'].include?('rvm') - rvm_path = File.dirname(File.dirname(ENV['MY_RUBY_HOME'])) - rvm_lib_path = File.join(rvm_path, 'lib') - $LOAD_PATH.unshift rvm_lib_path + #rvm_path = File.dirname(File.dirname(ENV['MY_RUBY_HOME'])) + #rvm_lib_path = File.join(rvm_path, 'lib') + #$LOAD_PATH.unshift rvm_lib_path require 'rvm' RVM.use_from_path! root @@ -39,6 +47,17 @@ def stop Process.kill 'TERM', pid if running? end + def need_reload? + if File.exists?(tmp_restart) + new_mtime = File.stat(tmp_restart).mtime + if new_mtime != mtime + @mtime = new_mtime + return true + end + end + false + end + def static_file? path File.file? File.join(root, 'public', path) end diff --git a/lib/hoof/http_server.rb b/lib/hoof/http_server.rb index fa06eea..7562fff 100644 --- a/lib/hoof/http_server.rb +++ b/lib/hoof/http_server.rb @@ -17,10 +17,10 @@ def process_http_request p 'DATA' p @buffer begin - host = @http_headers.scan(/Host:\s*([-a-zA-z.]*)\000/)[0][0].gsub(/:\d+$/, '') - close_connection and return unless host =~ /.dev$/ + host,port = @http_headers.scan(/Host:\s*([-a-zA-z\.\d]*):?(\d+)?\000/).flatten #[0][0].gsub(/:\d+$/, '') + close_connection and return unless host =~ /.((dev)|(xip\.io))$/ - name = host.split('.')[-2] + name = host.gsub(/\.((dev)|(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b\.xip\.io))$/, '') application = Hoof.find name if application @@ -39,11 +39,22 @@ def process_http_request }) end else + if name + renderer = ERB.new(File.read(File.join(File.dirname(__FILE__),"..","templates","not_found.html.erb"))) + @title = "Application not found
\"#{name}\"" + puts @title + @application = application + @host = host + @port = port + send_data renderer.result(binding) + close_connection_after_writing + return + end close_connection end rescue => e puts e.message - puts e.backtrace.join("\n") + puts e.backtrace[0..10].join("\n") close_connection end end diff --git a/lib/templates/not_found.html.erb b/lib/templates/not_found.html.erb new file mode 100644 index 0000000..dbf0c91 --- /dev/null +++ b/lib/templates/not_found.html.erb @@ -0,0 +1,145 @@ + + + + + <%= @title %> + + + +
+ +

<%= @title %>

+ +

Symlink your app to ~/.hoof/ first.

+
+

When you access http://<%= @host %><%= ":#{@port}" if @port %>/, Hoof looks for a Rack application at + ~/.hoof/. To run your app at this domain:

+
+        $ cd ~/.hoof$ ln -s /path/to/myapp
+        $ open http://<%= @host %><%= ":#{@port}" if @port %>/
+      
+
+ + + + + + + +
+ + \ No newline at end of file