Skip to content

Commit

Permalink
fix rvm issue pyromaniac#13
Browse files Browse the repository at this point in the history
listen to 0.0.0.0 for xip.io compat
http xip.io compatible
touch tmp/restart.txt to reload unicorn
  • Loading branch information
berga committed Nov 14, 2012
1 parent 018ba68 commit 2926e8a
Show file tree
Hide file tree
Showing 7 changed files with 202 additions and 23 deletions.
1 change: 1 addition & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Linux version of https://github.com/37signals/pow/

Compatible with Ruby 1.8.7 - ree

== Installation

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.7
0.0.8
9 changes: 5 additions & 4 deletions hoof.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
]
Expand Down
6 changes: 4 additions & 2 deletions lib/hoof.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'eventmachine'
require 'unicorn/launcher'
require 'evma_httpserver'
require 'erb'

require 'hoof/http_server'
require 'hoof/https_server'
Expand Down Expand Up @@ -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
Expand Down
43 changes: 31 additions & 12 deletions lib/hoof/application.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down
19 changes: 15 additions & 4 deletions lib/hoof/http_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<br/>\"#{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
Expand Down
145 changes: 145 additions & 0 deletions lib/templates/not_found.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title><%= @title %></title>
<style> body {
margin: 0;
padding: 0;
background: #e0e0d8;
line-height: 18px;
}

div.page {
margin: 72px auto;
margin: 36px auto;
background: #fff;
border-radius: 18px;
-webkit-box-shadow: 0px 2px 7px #999;
-moz-box-shadow: 0px 2px 7px #999;
padding: 36px 90px;
width: 480px;
position: relative;
}

.big div.page {
width: 720px;
}

h1, h2, p, li {
font-family: Helvetica, sans-serif;
font-size: 13px;
}

h1 {
line-height: 45px;
font-size: 36px;
margin: 0;
}

h1:before {
font-size: 66px;
line-height: 42px;
position: absolute;
right: 576px;
}

.big h1:before {
right: 819px;
}

h1.ok {
color: #060;
}

h1.ok:before {
content: "✓";
color: #090;
}

h1.err {
color: #600;
}

h1.err:before {
content: "✗";
color: #900;
}

h2 {
line-height: 27px;
font-size: 18px;
font-weight: normal;
margin: 0;
}

a, pre span {
color: #776;
}

h2, p, pre {
color: #222;
}

pre {
white-space: pre-wrap;
font-size: 13px;
}

pre, code {
font-family: Menlo, Monaco, monospace;
}

p code {
font-size: 12px;
}

pre.breakout {
border-top: 1px solid #ddd;
border-bottom: 1px solid #ddd;
background: #fafcf4;
margin-left: -90px;
margin-right: -90px;
padding: 8px 0 8px 90px;
}

pre.small_text {
font-size: 10px;
}

pre.small_text strong {
font-size: 13px;
}

ul {
padding: 0;
}

li {
list-style-type: none;
} </style>
</head>
<body class="">
<div class="page">
<!--CONTENTS-->
<h1 class="err"><%= @title %></h1>

<h2>Symlink your app to <code>~/.hoof/</code> first.</h2>
<section>
<p>When you access <code>http://<%= @host %><%= ":#{@port}" if @port %>/</code>, Hoof looks for a Rack application at
<code>~/.hoof/</code>. To run your app at this domain:</p>
<pre>
<span>$</span> cd ~/.hoof<span>$</span> ln -s /path/to/myapp
<span>$</span> open http://<%= @host %><%= ":#{@port}" if @port %>/
</pre>
</section>
<!--END CONTENTS-->
<!--<ul>-->
<!--<li><a href="http://pow.cx/manual">Pow User&rsquo;s Manual</a></li>-->
<!--<li><a href="https://github.com/37signals/pow/wiki/Troubleshooting">Troubleshooting</a></li>-->
<!--<li><a href="https://github.com/37signals/pow/wiki/FAQ">Frequently Asked Questions</a></li>-->
<!--<li><a href="https://github.com/37signals/pow/issues">Issue Tracker</a></li>-->
<!--</ul>-->
</div>
</body>
</html>

0 comments on commit 2926e8a

Please sign in to comment.