bin/ginatra
#!/usr/bin/env ruby
$:.unshift File.expand_path('../../lib', __FILE__)
ENV['RACK_ENV'] ||= 'production'
require 'ginatra/config'
require 'optparse'
options = {
port: Ginatra.config.port,
host: Ginatra.config.host
}
global_opts = OptionParser.new do |opts|
opts.banner = "Usage: ginatra <options> <command>"
opts.separator ""
opts.separator "Options:"
opts.on('-v', '--version', 'Print version and exit') do
require_relative '../lib/ginatra/version'
puts "Ginatra #{Ginatra::VERSION} #{Ginatra::RELEASE_NAME}"
exit
end
opts.on_tail('-h', '--help', 'Show this help message') do
puts opts
exit
end
end
server_opts = {
'run' => OptionParser.new do |opts|
opts.banner = "Usage: ginatra run <options>"
opts.separator ""
opts.separator "Options:"
opts.on('-p', '--port PORT', 'Port to bind to (defaults from config file)') do |port|
options[:port] = port.to_i
end
opts.on('-h', '--host HOST', 'Host address to bind to (defaults from config file)') do |host|
options[:host] = host
end
opts.on 'Start Ginatra web server' do
rack_config = File.expand_path("#{__FILE__}/../../config.ru")
pid_file = File.expand_path("#{__FILE__}/../../ginatra.pid")
system "rackup -D -E #{ENV['RACK_ENV']} -P #{pid_file} -p #{options[:port]} -o #{options[:host]} #{rack_config}"
puts "Ginatra runs on #{options[:host]}:#{options[:port]}"
end
end,
'stop' => OptionParser.new do |opts|
opts.banner = "Usage: ginatra stop"
opts.on 'Stop Ginatra web server' do
pid_file = File.expand_path("#{__FILE__}/../../ginatra.pid")
if File.exist?(pid_file)
pid = File.read(pid_file)
Process.kill('INT', pid.to_i)
File.delete(pid_file)
puts "Stopped Ginatra web server"
end
end
end,
'status' => OptionParser.new do |opts|
opts.banner = "Usage: ginatra status"
opts.on 'Get status of Ginatra web server' do
pid_file = File.expand_path("#{__FILE__}/../../ginatra.pid")
status = File.exist?(pid_file) ? "running.. (pid: #{File.read(pid_file).to_i})" : 'NOT running'
puts "Ginatra is #{status}"
end
end
}
if ARGV.size.zero?
puts global_opts
%w(run stop status).each do |cmd|
puts
puts server_opts[cmd]
end
exit 1
end
global_opts.order!
server_opts[ARGV.shift].order!