#!/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 |opts| opts.banner = "Usage<#{Nephos::VERSION}>: nephos-server " $server_port = ENV["SERVER_PORT"] || 8080 opts.on("-p=nb", "--port=nb", "Port to listen") do |port| $server_port = Integer(port) end $server_host = ENV["SERVER_HOST"] || "0.0.0.0" opts.on("-h=listen", "--host=listen", "Listen on the network") do |host| $server_host = host end $server_env = ENV["ENVIRONMENT"] || "development" opts.on("-e=env", "--env=env", "Environment") do |env| $server_env = env end opts.on("--debug", "Enable debugging mode") do $debug = true end opts.on("--test", "Enable testing mode (for nephos developpers)") do $test = true end opts.on("-d", "--daemon", "Start in background") do $daemon = true end opts.on("-k", "--kill", "Kill the daemon if started") do $kill_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? puts "Daemon #{Nephos::Bin::Daemon.get_pid} stopped" Nephos::Bin::Daemon.kill! exit 0 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.exists? "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