public
Description: A very fast & simple Ruby web server
Homepage: http://code.macournoyer.com/thin/
Clone URL: git://github.com/macournoyer/thin.git
commit  1a2268550c691256a40f8ab1f80a052069fdce9c
tree    edc47b0a1a44e7969edfb658d30b75b916f289cc
parent  e6ff51a05d896b34bd5cc3b2de315f22ab524635
thin / bin / thin
100755 104 lines (79 sloc) 3.2 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env ruby
# <tt>thin start</tt>: Starts the Rails app in the current directory.
# Run <tt>thin -h</tt> to get more usage.
require File.dirname(__FILE__) + '/../lib/thin'
require 'optparse'
 
COMMANDS = %w(start stop restart)
 
options = {
  :root => Dir.pwd,
  :env => 'development',
  :host => '0.0.0.0',
  :port => 3000,
  :timeout => 60,
  :log_file => 'log/thin.log',
  :pid_file => 'tmp/pids/thin.pid'
}
 
opts = OptionParser.new do |opts|
  opts.banner = "Usage: thin [options] #{COMMANDS.join('|')}"
 
  opts.separator ""
  opts.separator "Server options:"
 
  opts.on("-a", "--address HOST", "bind to HOST address (default: 0.0.0.0)") { |host| options[:host] = host }
  opts.on("-p", "--port PORT", "use PORT (default: 3000)") { |port| options[:port] = port }
  opts.on("-e", "--environment ENV", "Rails environment (default: development)") { |env| options[:env] = env }
  opts.on("-c", "--chdir PATH", "Change to dir before starting") { |dir| options[:root] = File.expand_path(dir) }
  opts.on("-d", "--daemonize", "Run daemonized in the background") { options[:daemonize] = true }
  opts.on("-l", "--log FILE", "File to redirect output",
                              "(default: #{options[:log_file]})") { |file| options[:log_file] = file }
  opts.on("-P", "--pid FILE", "File to store PID",
                              "(default: #{options[:pid_file]})") { |file| options[:pid_file] = file }
  opts.on("-t", "--timeout SEC", "Request or command timeout in sec",
                                 "(default: #{options[:timeout]})") { |sec| options[:timeout] = sec }
  opts.on("-u", "--user NAME", "User to run daemon as (use with -g)") { |user| options[:user] = user }
  opts.on("-g", "--group NAME", "Group to run daemon as (use with -u)") { |group| options[:group] = group }
  
  opts.separator ""
  opts.separator "Common options:"
 
  opts.on_tail("-D", "--debug", "Set debbuging on") { $DEBUG = true }
 
  opts.on_tail("-h", "--help", "Show this message") do
    puts opts
    exit
  end
 
  opts.on_tail('-v', '--version', "Show version") do
    puts Thin::SERVER
    exit
  end
 
  opts.parse! ARGV
end
 
 
# Commands definitions
 
def start(options)
  server = Thin::Server.new(options[:host], options[:port])
  
  server.pid_file = options[:pid_file]
  server.log_file = options[:log_file]
  server.timeout = options[:timeout]
  
  if options[:daemonize]
    server.change_privilege options[:user], options[:group] if options[:user] && options[:group]
    server.daemonize
  end
  
  server.app = Rack::Adapter::Rails.new(options)
 
  server.start!
end
 
def stop(options)
  Thin::Server.kill options[:pid_file], options[:timeout]
end
 
def restart(options)
  # Restart only make sense when running as a daemon
  options.update :daemonize => true
  
  stop(options)
  start(options)
end
 
 
# Runs the command
 
Dir.chdir(options[:root])
command = ARGV[0]
 
if COMMANDS.include?(command)
  send(command, options)
elsif command.nil?
  puts "Command required"
  puts opts
  exit 1
else
  abort "Invalid command : #{command}"
end