about summary refs log tree commit homepage
path: root/lib
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2013-10-30 23:52:38 +0000
committerEric Wong <normalperson@yhbt.net>2013-10-31 05:08:19 +0000
commit885b2f28f3b31539140a8466e6205903bc7cf1d2 (patch)
treed47c0b5cf4e8b0f29c883945d68fca674e089c7f /lib
parent2710180a00635dbdf7faebc339016a045b21c863 (diff)
downloadyahns-885b2f28f3b31539140a8466e6205903bc7cf1d2.tar.gz
This should make it easier for Rack users to get started with yahns.
Diffstat (limited to 'lib')
-rw-r--r--lib/yahns.rb3
-rw-r--r--lib/yahns/daemon.rb2
-rw-r--r--lib/yahns/rackup_handler.rb57
3 files changed, 61 insertions, 1 deletions
diff --git a/lib/yahns.rb b/lib/yahns.rb
index 51a6c4c..b493dc2 100644
--- a/lib/yahns.rb
+++ b/lib/yahns.rb
@@ -1,5 +1,7 @@
 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
+$stdout.sync = $stderr.sync = true
+
 require 'unicorn' # pulls in raindrops, kgio, fcntl, etc, stringio, and logger
 require 'sleepy_penguin'
 
@@ -68,7 +70,6 @@ require_relative 'yahns/config'
 require_relative 'yahns/tmpio'
 require_relative 'yahns/worker'
 require_relative 'yahns/sigevent'
-require_relative 'yahns/daemon'
 require_relative 'yahns/socket_helper'
 require_relative 'yahns/server'
 require_relative 'yahns/fdmap'
diff --git a/lib/yahns/daemon.rb b/lib/yahns/daemon.rb
index df8b573..f6cdf47 100644
--- a/lib/yahns/daemon.rb
+++ b/lib/yahns/daemon.rb
@@ -1,6 +1,8 @@
 # -*- encoding: binary -*-
 # Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
 # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
+require 'yahns'
+
 module Yahns::Daemon # :nodoc:
   # We don't do a lot of standard daemonization stuff:
   #   * umask is whatever was set by the parent process at startup
diff --git a/lib/yahns/rackup_handler.rb b/lib/yahns/rackup_handler.rb
new file mode 100644
index 0000000..9fbebbf
--- /dev/null
+++ b/lib/yahns/rackup_handler.rb
@@ -0,0 +1,57 @@
+# Copyright (C) 2013, Eric Wong <normalperson@yhbt.net> and all contributors
+# License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt)
+require 'yahns'
+
+module Yahns::RackupHandler # :nodoc:
+  def self.default_host
+    environment  = ENV['RACK_ENV'] || 'development'
+    environment == 'development' ? '127.0.0.1' : '0.0.0.0'
+  end
+
+  def self.run(app, o)
+    cfg = Yahns::Config.new
+    cfg.instance_eval do
+      app(:rack, app) do
+        addr = o[:listen] || "#{o[:Host]||default_host}:#{o[:Port]||8080}"
+        # allow listening to multiple addresses
+        if addr =~ /,/
+          addr.split(/,/).each { |l| listen(l) }
+        else
+          listen addr
+        end
+
+        val = o[:client_timeout] and client_timeout(val)
+      end
+
+      queue do
+        wt = o[:worker_threads] and worker_threads(wt)
+      end
+
+      %w(stderr_path stdout_path).each do |x|
+        x = x.to_sym
+        val = o[x] and __send__(x, val)
+      end
+
+      # we need this because "rackup -D" sends us to "/", which might be
+      # fine for most apps, but we have SIGUSR2 restarts to support
+      working_directory(Yahns::START[:cwd])
+    end
+    Yahns::Server.new(cfg).start.join
+  end
+
+  def self.valid_options
+    # these should be the most common options
+    {
+      "listen=ADDRESS" => "address(es) to listen on (e.g. /tmp/sock)",
+      "worker_threads=NUM" => "number of worker threads to run",
+
+      # this affects how quickly graceful shutdown goes
+      "client_timeout=SECONDS" => "timeout for idle clients",
+
+      # I don't want these here, but rackup supports daemonize and
+      # we lose useful information when that sends stdout/stderr to /dev/null
+      "stderr_path=PATH" => "stderr destination",
+      "stdout_path=PATH" => "stdout destination",
+    }
+  end
+end