bin/nephos-server
#!/usr/bin/env ruby
require 'colorize'
require 'optparse'
require_relative '../lib/nephos-server/bin-helpers'
require_relative '../lib/nephos-server/version'
class RoutingError < StandardError; end
begin
opts = OptionParser.new do |opt|
opt.banner = "Usage<#{Nephos::VERSION}>: nephos-server <options>"
$server_port = ENV["SERVER_PORT"] || 8080
opt.on("-p=nb", "--port=nb", "Port to listen") do |port|
$server_port = Integer(port)
end
$server_host = ENV["SERVER_HOST"] || "0.0.0.0"
opt.on("-h=listen", "--host=listen", "Listen on the network") do |host|
$server_host = host
end
$server_env = ENV["ENVIRONMENT"] || "development"
opt.on("-e=env", "--env=env", "Environment") do |env|
$server_env = env
end
opt.on("--debug", "Enable debugging mode") do
$debug = true
end
opt.on("--verbose=mode", "Enable verbosity [output|none=default]") do |mode|
$verbose = mode
end
opt.on("--test", "Enable testing mode (for nephos developpers)") do
$test = true
end
opt.on("-d", "--daemon", "Start in background") do
$daemon = true
end
opt.on("-k", "--kill", "Kill the daemon if started") do
$kill_daemon = true
end
opt.on("-r", "--restart", "Restart the daemon if started") do
$restart_daemon = true
$kill_daemon = true
$daemon = true
end
end.parse!
Nephos.env = $server_env
Dir.chdir(opts[0]) if not opts.empty?
if $kill_daemon
raise "No Daemon started" if not Nephos::Bin::Daemon.started?
pid = Nephos::Bin::Daemon.get_pid
Nephos::Bin::Daemon.kill!
puts "Daemon #{pid} stopped"
exit 0 unless $restart_daemon
end
if $daemon and Nephos::Bin::Daemon.started?
raise "Unable to start the server: the daemon #{Nephos::Bin::Daemon.get_pid} already exists"
end
if $test
require_relative "../lib/nephos-server"
else
require 'nephos-server'
end
if not $test and
(not File.exist? "Gemfile.lock" or
not File.read("Gemfile.lock").split.index("nephos-server"))
raise InvalidApplication
end
puts "Running Nephos::Server version #{Nephos::VERSION} (#{Nephos.env})"
if $daemon
Nephos::Bin::Daemon.detach!
end
Nephos::Server.start($server_port, $server_host)
rescue RoutingError => err
puts "Routing Error: Check out the documentation and `routes.rb` file.".yellow
puts "#{err.class}: #{err.message}".red
puts "#{err.backtrace.join("\n")}".yellow if $debug
exit 1
rescue => err
puts "#{err.class}: #{err.message}".red
puts "#{err.backtrace.join("\n")}".yellow if $debug
exit 1
end