class FanforceApp::CoreConfig attr_accessor :_id, :redis_url, :factory_root_dir, :root_dir, :dir_name, :show_error_details attr_accessor :bugsnag, :bugsnag_enabled attr_reader :type def initialize(root_dir) @_id = ENV['FANFORCE_APP_ID'] @root_dir = root_dir @factory_root_dir = File.expand_path '../../', __FILE__ @redis_url = self.class.default_redis_url @show_error_details = false @bugsnag = (ENV['BUGSNAG_API_KEY'].present?) ? {api_key: ENV['BUGSNAG_API_KEY']} : nil end def show_error_details=(bool) @show_error_details = [true,1,'1'].include?(bool) end def load raise 'CRITICAL: This FanforceApp requires config._id to be set. See documentation.' if !@_id raise 'CRITICAL: This FanforceApp requires config.root_dir to be set. See documentation' if !@root_dir FanforceApp::Sinatra.set :root, @root_dir FanforceApp::Sinatra.set :public_folder, [@root_dir+'/public', @factory_root_dir+'/public'].inject(''){|dir, d| File.directory?(d) ? (break d) : d } FanforceApp::Sinatra.set :views, [@root_dir+'/views', @factory_root_dir+'/views'].inject(''){|dir, d| File.directory?(d) ? (break d) : d } FanforceApp.config.load_bugsnag FanforceApp.config.load_routes FanforceApp.config.load_redis end def bugsnag_enabled? @bugsnag_enabled 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 @bugsnag_enabled = true end def load_routes @dir_name = /\/([^\/]+)\/?$/.match(@root_dir)[1] $: << File.join(@root_dir,'lib') if File.directory?("#{@root_dir}/lib") $: << @root_dir if File.directory?("#{@root_dir}") Dir["#{@root_dir}/initializers/*.rb"].each {|f| require f } if File.directory?("#{@root_dir}/initializers") require "#{@root_dir}/Routes" if File.exists?("#{@root_dir}/Routes.rb") require "#{@factory_root_dir}/routes_core" require "#{@factory_root_dir}/routes_connector" require "#{@factory_root_dir}/routes_broadcaster" require "#{@factory_root_dir}/routes_identifier" require "#{@factory_root_dir}/routes_behavior" require "#{@factory_root_dir}/routes_js_widget" end def load_env self.class.load_env end def self.load_env if (!ENV['RACK_ENV'] or ENV['RACK_ENV'] == 'development') load_env_from_file("#{FanforceApp.config.root_dir}/.powenv") if File.exists?("#{FanforceApp.config.root_dir}/.powenv") load_env_from_file("#{FanforceApp.config.root_dir}/.appenv") if File.exists?("#{FanforceApp.config.root_dir}/.appenv") end end def load_redis self.class.load_redis(@redis_url) end def self.load_redis(redis_url=nil) redis_url ||= default_redis_url $Redis = ::Redis.new(url: redis_url) end def self.default_redis_url ENV['REDIS_URL'] || 'redis://localhost:6379' end def self.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