Based By http://www.bigbold.com/snippets/posts/show/303
1
2
3
4 require 'optparse'
5 require 'fileutils'
6 require 'tmpdir'
7
8 OPTIONS = {
9 :port => 8000,
10 :ip => "0.0.0.0",
11 :daemon => false,
12 :environment => "development",
13 :app_name => Process::pid.to_s,
14 :max_procs => 3,
15 :min_procs => 1,
16 }
17
18 ARGV.options do |opts|
19 script_name = File.basename($0)
20 opts.banner = "Usage: ruby #{script_name} [options]"
21
22 opts.separator ""
23
24 opts.on("-p", "--port=port", Integer,
25 "Runs Rails on the specified port.",
26 "Default: 8000") { |OPTIONS[:port]| }
27 opts.on("-b", "--binding=ip", String,
28 "Binds Rails to the specified ip.",
29 "Default: 0.0.0.0") { |OPTIONS[:ip]| }
30 opts.on("-e", "--environment=name", String,
31 "Specifies the environment to run this server under (test/development/production).",
32 "Default: development") { |OPTIONS[:environment]| }
33 opts.on("-a", "--app-name=name", String,
34 "Specifies the application name.",
35 "Default: process_id") { |OPTIONS[:app_name]| }
36 opts.on("-d", "--daemon",
37 "Make lighttpd / Rails run as a Daemon (only works if fork is available -- meaning on *nix)."
38 ) { OPTIONS[:daemon] = true }
39 opts.on("-n", "--min-procs=number", Integer,
40 "Minimum number of FastCGI processes allowed.",
41 "Default: 1") { |OPTIONS[:min_procs]| }
42 opts.on("-m", "--max-procs=number", Integer,
43 "Maximum number of FastCGI processes allowed.",
44 "Default: 3") { |OPTIONS[:max_procs]| }
45
46 opts.separator ""
47
48 opts.on("-h", "--help",
49 "Show this help message.") { puts opts; exit }
50
51 opts.parse!
52
53 end
54
55 ENV["RAILS_ENV"] = OPTIONS[:environment]
56 RAILS_ROOT = Dir.pwd + "/./"
57 TMP_DIR = Dir.tmpdir
58 LIGHTTPD_CONF_FILE = TMP_DIR + "/lighttpd.#{OPTIONS[:app_name]}.conf"
59
60 conf = DATA.read
61 conf.gsub!('__PORT__', OPTIONS[:port].to_s)
62 conf.gsub!('__BINDING__', OPTIONS[:ip])
63 conf.gsub!('__RAILS_ROOT__', File.expand_path(RAILS_ROOT))
64 conf.gsub!('__APP_NAME__', OPTIONS[:app_name])
65 conf.gsub!('__MIN_PROCS__', OPTIONS[:min_procs].to_s)
66 conf.gsub!('__MAX_PROCS__', OPTIONS[:max_procs].to_s)
67 conf.gsub!('__RAILS_ENV__', ENV['RAILS_ENV'])
68 conf.gsub!('__TMP_DIR__', TMP_DIR)
69 File.open(LIGHTTPD_CONF_FILE, "w") { |output| output.write(conf) }
70
71 CMD = "lighttpd -f #{LIGHTTPD_CONF_FILE}"
72 CMD << " -D" if not OPTIONS[:daemon]
73
74 puts "=> Rails application started on http://#{OPTIONS[:ip]}:#{OPTIONS[:port]}"
75 puts "=> Ctrl-C to shutdown server; call with --help for options" if not OPTIONS[:daemon]
76
77 puts CMD
78 `#{CMD}`
79
80 FileUtils.rm Dir.glob(TMP_DIR + "/lighttpd.#{OPTIONS[:app_name]}.*") if not OPTIONS[:daemon]
81
82 __END__
83 server.port = __PORT__
84 server.bind = "__BINDING__"
85 server.pid-file = "__TMP_DIR__/lighttpd.__APP_NAME__.pid"
86
87
88
89 server.modules = ( "mod_rewrite", "mod_redirect", "mod_access", "mod_fastcgi", "mod_accesslog" )
90 server.document-root = "__RAILS_ROOT__/public/"
91 server.indexfiles = ( "index.html" ,"dispatch.fcgi")
92 accesslog.filename = "__RAILS_ROOT__/log/lighttpd.__RAILS_ENV__.access.log"
93 server.errorlog = "__RAILS_ROOT__/log/lighttpd.__RAILS_ENV__.error.log"
94 server.error-handler-404 = "/dispatch.fcgi"
95
96
97
98
99 fastcgi.server = (
100 ".fcgi" => (
101 "__APP_NAME__" => (
102 "socket" => "__TMP_DIR__/lighttpd.__APP_NAME__.fcgi.socket",
103 "bin-path" => "__RAILS_ROOT__/public/dispatch.fcgi",
104 "min-procs" => __MIN_PROCS__,
105 "max_procs" => __MAX_PROCS__
106 )
107 )
108 )
109
110
111 mimetype.assign = (
112 ".rpm" => "application/x-rpm",
113 ".pdf" => "application/pdf",
114 ".sig" => "application/pgp-signature",
115 ".spl" => "application/futuresplash",
116 ".class" => "application/octet-stream",
117 ".ps" => "application/postscript",
118 ".torrent" => "application/x-bittorrent",
119 ".dvi" => "application/x-dvi",
120 ".gz" => "application/x-gzip",
121 ".pac" => "application/x-ns-proxy-autoconfig",
122 ".swf" => "application/x-shockwave-flash",
123 ".tar.gz" => "application/x-tgz",
124 ".tgz" => "application/x-tgz",
125 ".tar" => "application/x-tar",
126 ".zip" => "application/zip",
127 ".mp3" => "audio/mpeg",
128 ".m3u" => "audio/x-mpegurl",
129 ".wma" => "audio/x-ms-wma",
130 ".wax" => "audio/x-ms-wax",
131 ".ogg" => "audio/x-wav",
132 ".wav" => "audio/x-wav",
133 ".gif" => "image/gif",
134 ".jpg" => "image/jpeg",
135 ".jpeg" => "image/jpeg",
136 ".png" => "image/png",
137 ".xbm" => "image/x-xbitmap",
138 ".xpm" => "image/x-xpixmap",
139 ".xwd" => "image/x-xwindowdump",
140 ".css" => "text/css",
141 ".html" => "text/html",
142 ".htm" => "text/html",
143 ".js" => "text/javascript",
144 ".asc" => "text/plain",
145 ".c" => "text/plain",
146 ".conf" => "text/plain",
147 ".text" => "text/plain",
148 ".txt" => "text/plain",
149 ".dtd" => "text/xml",
150 ".xml" => "text/xml",
151 ".mpeg" => "video/mpeg",
152 ".mpg" => "video/mpeg",
153 ".mov" => "video/quicktime",
154 ".qt" => "video/quicktime",
155 ".avi" => "video/x-msvideo",
156 ".asf" => "video/x-ms-asf",
157 ".asx" => "video/x-ms-asf",
158 ".wmv" => "video/x-ms-wmv",
159 ".bz2" => "application/x-bzip",
160 ".tbz" => "application/x-bzip-compressed-tar",
161 ".tar.bz2" => "application/x-bzip-compressed-tar"
162 )