Class: Heel::Server
- Inherits:
-
Object
- Object
- Heel::Server
- Defined in:
- lib/heel/server.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Returns the value of attribute options.
-
#parsed_options ⇒ Object
Returns the value of attribute parsed_options.
-
#stderr ⇒ Object
readonly
Returns the value of attribute stderr.
-
#stdin ⇒ Object
readonly
Returns the value of attribute stdin.
-
#stdout ⇒ Object
readonly
Returns the value of attribute stdout.
Class Method Summary collapse
-
.home_directory ⇒ Object
thank you Jamis - from Capistrano.
Instance Method Summary collapse
- #default_directory ⇒ Object
- #default_options ⇒ Object
-
#ensure_not_running ⇒ Object
make sure that if we are daemonizing the process is not running.
-
#error_version_help_kill ⇒ Object
if Version or Help options are set, then output the appropriate information instead of running the server.
- #heel_app ⇒ Object
-
#initialize(argv = []) ⇒ Server
constructor
A new instance of Server.
- #java? ⇒ Boolean
-
#kill_existing_proc ⇒ Object
kill an already running background heel process.
- #launch_browser ⇒ Object
- #log_file ⇒ Object
- #merge_options ⇒ Object
- #option_parser ⇒ Object
- #pid_file ⇒ Object
-
#run ⇒ Object
run the heel server with the current options.
- #server_options ⇒ Object
-
#set_io(stdin = $stdin, stdout = $stdout, setderr = $stderr) ⇒ Object
set the IO objects in a single method call.
-
#setup_heel_dir ⇒ Object
setup the directory that heel will use as the location to run from, where its logs will be stored and its PID file if backgrounded.
- #start_background_server ⇒ Object
- #start_foreground_server ⇒ Object
-
#start_server ⇒ Object
If we are daemonizing the fork and wait for the child to launch the server If we are not daemonizing, throw the ::Rackup::Server in a background thread.
- #win? ⇒ Boolean
Constructor Details
#initialize(argv = []) ⇒ Server
Returns a new instance of Server.
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/heel/server.rb', line 33 def initialize(argv = []) argv ||= [] set_io @options = @parsed_options = ::OpenStruct.new @parser = option_parser @error_message = nil begin @parser.parse!(argv) rescue ::OptionParser::ParseError => pe msg = ["#{@parser.program_name}: #{pe}", "Try `#{@parser.program_name} --help` for more information"] @error_message = msg.join("\n") end end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
16 17 18 |
# File 'lib/heel/server.rb', line 16 def @options end |
#parsed_options ⇒ Object
Returns the value of attribute parsed_options.
17 18 19 |
# File 'lib/heel/server.rb', line 17 def @parsed_options end |
#stderr ⇒ Object (readonly)
Returns the value of attribute stderr.
20 21 22 |
# File 'lib/heel/server.rb', line 20 def stderr @stderr end |
#stdin ⇒ Object (readonly)
Returns the value of attribute stdin.
21 22 23 |
# File 'lib/heel/server.rb', line 21 def stdin @stdin end |
#stdout ⇒ Object (readonly)
Returns the value of attribute stdout.
19 20 21 |
# File 'lib/heel/server.rb', line 19 def stdout @stdout end |
Class Method Details
.home_directory ⇒ Object
thank you Jamis - from Capistrano
26 27 28 29 30 |
# File 'lib/heel/server.rb', line 26 def home_directory # :nodoc: ENV["HOME"] || (ENV["HOMEPATH"] && "#{ENV["HOMEDRIVE"]}#{ENV["HOMEPATH"]}") || "/" end |
Instance Method Details
#default_directory ⇒ Object
66 67 68 |
# File 'lib/heel/server.rb', line 66 def default_directory ENV["HEEL_DEFAULT_DIRECTORY"] || File.join(::Heel::Server.home_directory,".heel") end |
#default_options ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/heel/server.rb', line 52 def defaults = ::OpenStruct.new defaults.show_version = false defaults.show_help = false defaults.address = "0.0.0.0" defaults.port = 4331 defaults.document_root = Dir.pwd defaults.daemonize = false defaults.highlighting = true defaults.kill = false defaults.launch_browser = true return defaults end |
#ensure_not_running ⇒ Object
make sure that if we are daemonizing the process is not running
199 200 201 202 203 204 205 206 |
# File 'lib/heel/server.rb', line 199 def ensure_not_running if File.exist?(pid_file) then @stdout.puts "ERROR: PID File #{pid_file} already exists. Heel may already be running." @stdout.puts "ERROR: Check the Log file #{log_file}" @stdout.puts "ERROR: Heel will not start until the .pid file is cleared (`heel --kill --port #{.port}' to clean it up)." exit 1 end end |
#error_version_help_kill ⇒ Object
if Version or Help options are set, then output the appropriate information instead of running the server.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/heel/server.rb', line 153 def error_version_help_kill if @parsed_options.show_version then @stdout.puts "#{@parser.program_name}: version #{Heel::VERSION}" exit 0 elsif @parsed_options.show_help then @stdout.puts @parser.to_s exit 0 elsif @error_message then @stdout.puts @error_message exit 1 elsif @parsed_options.kill then kill_existing_proc end end |
#heel_app ⇒ Object
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/heel/server.rb', line 220 def heel_app app = Heel::RackApp.new({ :document_root => .document_root, :highlighting => .highlighting}) logger = Heel::Logger.new( log_file ) stack = Rack::Builder.new { use Rack::CommonLogger, logger map "/" do run app end map "/heel_css" do run Rack::Files.new(Heel::Configuration.data_path( "css" )) end map "/heel_icons" do run Rack::Files.new(Heel::Configuration.data_path("lineicons")) end } return stack.to_app end |
#java? ⇒ Boolean
82 83 84 |
# File 'lib/heel/server.rb', line 82 def java? RUBY_PLATFORM =~ /java/ end |
#kill_existing_proc ⇒ Object
kill an already running background heel process
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/heel/server.rb', line 169 def kill_existing_proc if File.exist?(pid_file) then begin pid = open(pid_file).read.to_i @stdout.puts "Sending TERM to process #{pid}" Process.kill("TERM", pid) rescue Errno::ESRCH @stdout.puts "Unable to kill process with pid #{pid}. Process does not exist. Removing stale pid file." File.unlink(pid_file) rescue Errno::EPERM @stdout.puts "Unable to kill process with pid #{pid}. No permissions to kill process." end else @stdout.puts "No pid file exists for server running on port #{.port}, no process to kill" end @stdout.puts "Done." exit 0 end |
#launch_browser ⇒ Object
208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/heel/server.rb', line 208 def launch_browser Thread.new do print "Launching your browser" if .daemonize then puts " at http://#{.address}:#{.port}/" else puts "..." end ::Launchy.open("http://#{.address}:#{.port}/") end end |
#log_file ⇒ Object
74 75 76 |
# File 'lib/heel/server.rb', line 74 def log_file File.join(default_directory,"heel.#{.port}.log") end |
#merge_options ⇒ Object
136 137 138 139 140 141 |
# File 'lib/heel/server.rb', line 136 def = .marshal_dump .merge!( @parsed_options.marshal_dump ) @options = OpenStruct.new() end |
#option_parser ⇒ Object
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/heel/server.rb', line 86 def option_parser OptionParser.new do |op| op.separator "" op.on("-a", "--address ADDRESS", "Address to bind to", " (default: #{.address})") do |add| @parsed_options.address = add end op.on("-d", "--daemonize", "Run daemonized in the background") do raise ::OptionParser::ParseError, "Daemonizing is not supported on windows" if win? raise ::OptionParser::ParseError, "Daemonizing is not supported on java" if java? @parsed_options.daemonize = true end op.on("-h", "--help", "Display this text") do @parsed_options.show_help = true end op.on("-k", "--kill", "Kill an existing daemonized heel process") do @parsed_options.kill = true end op.on("--[no-]highlighting", "Turn on or off syntax highlighting", " (default: off)") do |highlighting| @parsed_options.highlighting = highlighting end op.on("--[no-]launch-browser", "Turn on or off automatic browser launch", " (default: on)") do |l| @parsed_options.launch_browser = l end op.on("-p", "--port PORT", Integer, "Port to bind to", " (default: #{.port})") do |port| @parsed_options.port = port end op.on("-r","--root ROOT", "Set the document root"," (default: #{.document_root})") do |document_root| @parsed_options.document_root = File.(document_root) raise ::OptionParser::ParseError, "#{@parsed_options.document_root} is not a valid directory" if not File.directory?(@parsed_options.document_root) end op.on("-v", "--version", "Show version") do @parsed_options.show_version = true end end end |
#pid_file ⇒ Object
70 71 72 |
# File 'lib/heel/server.rb', line 70 def pid_file File.join(default_directory,"heel.#{.port}.pid") end |
#run ⇒ Object
run the heel server with the current options.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 |
# File 'lib/heel/server.rb', line 281 def run error_version_help_kill setup_heel_dir ensure_not_running server_thread = start_server if .launch_browser then launch_browser.join end server_thread.join if server_thread end |
#server_options ⇒ Object
268 269 270 271 272 273 274 275 276 277 278 |
# File 'lib/heel/server.rb', line 268 def { :app => heel_app, :pid => pid_file, :Port => .port, :Host => .address, :environment => 'none', :server => 'puma', :daemonize => .daemonize } end |
#set_io(stdin = $stdin, stdout = $stdout, setderr = $stderr) ⇒ Object
set the IO objects in a single method call. This is really only for testing instrumentation
145 146 147 148 149 |
# File 'lib/heel/server.rb', line 145 def set_io(stdin = $stdin, stdout = $stdout ,setderr = $stderr) @stdin = stdin @stdout = stdout @stderr = stderr end |
#setup_heel_dir ⇒ Object
setup the directory that heel will use as the location to run from, where its logs will be stored and its PID file if backgrounded.
190 191 192 193 194 195 196 |
# File 'lib/heel/server.rb', line 190 def setup_heel_dir if not File.exist?(default_directory) then FileUtils.mkdir_p(default_directory) @stdout.puts "Created #{default_directory}" @stdout.puts "heel's PID (#{pid_file}) and log file (#{log_file}) are stored here" end end |
#start_background_server ⇒ Object
252 253 254 255 256 257 258 259 |
# File 'lib/heel/server.rb', line 252 def start_background_server if cpid = fork then Process.waitpid( cpid ) else server = ::Rackup::Server.new( ) server.start end end |
#start_foreground_server ⇒ Object
261 262 263 264 265 266 |
# File 'lib/heel/server.rb', line 261 def start_foreground_server Thread.new { server = ::Rackup::Server.new( ) server.start } end |
#start_server ⇒ Object
If we are daemonizing the fork and wait for the child to launch the server If we are not daemonizing, throw the ::Rackup::Server in a background thread
243 244 245 246 247 248 249 250 |
# File 'lib/heel/server.rb', line 243 def start_server if .daemonize then start_background_server return nil else return start_foreground_server end end |
#win? ⇒ Boolean
78 79 80 |
# File 'lib/heel/server.rb', line 78 def win? RUBY_PLATFORM =~ /mswin|mingw/ end |