class Fanforce::App::CoreConfig attr_accessor :bugsnag attr_reader :show_error_details, :is_bugsnag_enabled attr_writer :redis_url def initialize(root_dir, loader_filename) @root_dir = root_dir @factory_dir = File.expand_path '../', __FILE__ @show_error_details = false @bugsnag = (ENV['BUGSNAG_API_KEY'].present?) ? {api_key: ENV['BUGSNAG_API_KEY']} : nil end def _id raw_config[:_id] || (raise 'root_dir is missing') end def root_dir @root_dir || (raise 'root_dir is missing') end def factory_dir @factory_dir || (raise 'factory_dir is missing') end def show_error_details=(opt) @show_error_details = ['1','text'].include?(opt.to_s) ? opt.to_s : false end def redis_url @redis_url || ENV['REDIS_URL'] || 'redis://localhost:6379' end ###################################################################################################################### def raw_config @raw_config ||= validate_config_file(MultiJson.load(File.open("#{@root_dir}/config.json").read, symbolize_keys: true)) end def validate_config_file(raw_config) raw_config.is_a?(Hash) || (raise "#{@root_dir}/config.json must contain a json object (not a #{raw_config.class})") raw_config[:_id].present? || (raise '#{@root_dir}/config.json must include an _id field') return raw_config end ###################################################################################################################### def load_bugsnag return if !@bugsnag.is_a?(Hash) or ENV['RACK_ENV'] == 'test' require 'bugsnag' Bugsnag.configure do |config| config.api_key = ENV['BUGSNAG_API_KEY'] || @bugsnag[:api_key] config.notify_release_stages = @bugsnag[:notify_release_stages] || ['development','staging','production'] config.release_stage = @bugsnag[:release_stage] || ENV['RACK_ENV'] config.app_version = @bugsnag[:app_version] || "fanforce-app-factory-#{Fanforce::AppFactory::VERSION}" config.project_root = @root_dir end @is_bugsnag_enabled = true end def load_routes $: << File.join(@root_dir,'lib') if File.directory?("#{@root_dir}/lib") $: << @root_dir if File.directory?("#{@root_dir}") require "#{@factory_dir}/sinatra/helper_routes/app" require "#{@factory_dir}/sinatra/helper_routes/com_connector" require "#{@factory_dir}/sinatra/helper_routes/com_broadcaster" require "#{@factory_dir}/sinatra/helper_routes/com_identifier" require "#{@factory_dir}/sinatra/helper_routes/com_behavior" require "#{@factory_dir}/sinatra/helper_routes/com_js_widget" Dir["#{@root_dir}/initializers/*.rb"].each {|f| require f } if File.directory?("#{@root_dir}/initializers") require "#{@factory_dir}/Routes" require "#{@root_dir}/Routes" end def load_env if (!ENV['RACK_ENV'] or ENV['RACK_ENV'] == 'development') load_env_from_file("#{@root_dir}/.powenv") if File.exists?("#{@root_dir}/.powenv") load_env_from_file("#{@root_dir}/.appenv") if File.exists?("#{@root_dir}/.appenv") end end def load_env_from_file(file) File.open(file, 'rb') do |file| contents = file.read lines = contents.gsub('export ', '').split(/\n\r?/).reject{|line| line.blank?} lines.each do |line| keyValue = line.split("=", 2) next unless keyValue.count == 2 ENV[keyValue.first] = keyValue.last.gsub("'",'').gsub('"','') end end end end