Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: puma/puma-express
base: f75d29c080
...
head fork: puma/puma-express
compare: 426d881dd6
Checking mergeability… Don't worry, you can still create the pull request.
  • 2 commits
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 77 additions and 33 deletions.
  1. +21 −19 lib/puma/express.rb
  2. +56 −14 lib/puma/express/app.rb
View
40 lib/puma/express.rb
@@ -14,7 +14,7 @@ class Puma::Express
}
def initialize
- @servers = {}
+ @running = {}
@root = ENV['PUMA_EXPRESS_ROOT'] || File.expand_path("~/.puma_express")
@apps = {}
@@ -37,7 +37,7 @@ def monitor_apps
@apps.delete_if do |host, app|
if app.expired?
app.stop
- @servers.delete host
+ @running.delete host
true
else
false
@@ -49,13 +49,13 @@ def monitor_apps
end
end
- def find_host(env)
+ def find_app(env)
key = env['HTTP_HOST']
- host, path = @servers[key]
- if host
- @apps[key].hit
- [host, path]
+ if app = @running[key]
+ app.hit
end
+
+ app
end
def start(env)
@@ -65,17 +65,15 @@ def start(env)
path = File.join @root, base
- socket = File.join @unix_socket_dir, host
-
- puts "Starting #{base} on #{socket}"
-
if File.exists? path
- app = App.new path, socket, 5.0
+ app = App.new host, path, @unix_socket_dir, 5.0
app.run
+ puts "Starting #{base} on #{app.connection}"
+
@apps[host] = app
- @servers[host] = ["localhost", socket]
+ @running[host] = app
else
nil
end
@@ -86,7 +84,7 @@ def error(env, message="Unconfigured host")
[501, {}, ["#{host}: #{message}"]]
end
- def proxy_unix(env, host, path)
+ def proxy_unix(env, path)
request = Rack::Request.new(env)
method = request.request_method.downcase
@@ -191,16 +189,20 @@ def proxy_tcp(env, host, port)
end
def call(env)
- host, path = find_host(env)
+ app = find_app(env)
- host, path = start(env) unless host
+ app = start(env) unless app
- return error(env) unless host
+ return error(env) unless app
begin
- proxy_unix env, host, path
+ if sock = app.unix_socket
+ proxy_unix env, sock
+ else
+ proxy_tcp env, "localhost", app.tcp_port
+ end
rescue SystemCallError => e
- error env, "Error with #{host}: #{e.message} (#{e.class})"
+ error env, "Error: #{e.message} (#{e.class})"
rescue Exception => e
error env, "Unknown error: #{e.message} (#{e.class})"
end
View
70 lib/puma/express/app.rb
@@ -6,28 +6,53 @@ class Puma::Express
class App
Starter = File.expand_path "../starter.rb", __FILE__
- def initialize(path, socket, idle_limit)
+ def initialize(host, path, tmp_dir, idle_limit)
+ @host = host
@path = path
- @socket = socket
+ @tmp_dir = tmp_dir
@idle_limit = idle_limit
@last_hit = Time.now
@pid = nil
+ @shell = false
+ @ruby = nil
+ @shell = false
- cf = "#{path}.yml"
+ @unix_socket = nil
+ @tcp_port = nil
- @ruby = nil
- @exec = false
+ load_config
+ end
+
+ attr_reader :unix_socket, :tcp_port
+
+ def load_config
+ cf = "#{@path}.yml"
if File.exists?(cf)
@config = YAML.load File.read(cf)
- @ruby = @config['ruby']
else
- @config = nil
+ @config = {}
+ end
+
+ if cmd = @config['command']
+ @command = cmd
+ else
+ @ruby = @config['ruby']
+ @shell = @config['full_shell'] ||
+ File.exists?(File.join(@path, ".rvmrc")) ||
+ File.exists?(File.join(@path, ".rbenv-version"))
+ end
+
+ unless @tcp_port = @config['port']
+ @unix_socket = File.join @tmp_dir, @host
end
+ end
- unless @ruby
- @exec = File.exists?(File.join(path, ".rvmrc")) ||
- File.exists?(File.join(path, ".rbenv-version"))
+ def connection
+ if @tcp_port
+ "tcp://0.0.0.0:#{@tcp_port}"
+ else
+ "unix://#{@unix_socket}"
end
end
@@ -40,20 +65,37 @@ def hit
end
def run
+ if @command
+ @pid = fork do
+ ENV['PORT'] = @tcp_port.to_s if @tcp_port
+
+ Dir.chdir @path
+ exec "bash", "-c", @command
+ end
+
+ sleep 1
+ else
+ run_ruby
+ end
+ end
+
+ def run_ruby
r, w = IO.pipe
@pid = fork do
+ ENV['PORT'] = @tcp_port.to_s if @tcp_port
+
r.close
Dir.chdir @path
if @ruby
- exec @ruby, Starter, @socket, w.to_i.to_s
- elsif @exec
- exec "bash", "-l", "-c", "ruby #{Starter} #{@socket} #{w.to_i}"
+ exec @ruby, Starter, @unix_socket, w.to_i.to_s
+ elsif @shell
+ exec "bash", "-l", "-c", "ruby #{Starter} #{@unix_socket} #{w.to_i}"
else
ARGV.unshift w.to_i.to_s
- ARGV.unshift @socket
+ ARGV.unshift @unix_socket
load Starter
end

No commit comments for this range

Something went wrong with that request. Please try again.