class FanforceApp::CoreConfig attr_accessor :layout_for_index, :layout_for_dashboard attr_accessor :_id, :redis_url, :factory_root_dir, :root_dir, :dir_name, :show_error_details attr_accessor :bugsnag, :bugsnag_enabled attr_reader :type REDIS_URL = ENV['REDIS_URL'] || 'redis://localhost:6379' def initialize(root_dir) @layout_for_index = true @layout_for_dashboard = true @_id = ENV['FANFORCE_APP_ID'] @root_dir = root_dir @factory_root_dir = File.expand_path '../../', __FILE__ @redis_url = 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}") require "#{@root_dir}/config/initializer" if File.exists?("#{@root_dir}/config/initializer.rb") require "#{@root_dir}/initializer" if File.exists?("#{@root_dir}/initializer.rb") if File.directory?("#{@root_dir}/initializers") Dir["#{@root_dir}/initializers/*.rb"].each {|file| require file } end require "#{@root_dir}/config/routes" if File.exists?("#{@root_dir}/config/routes.rb") require "#{@root_dir}/views/_routes" if File.exists?("#{@root_dir}/views/_routes.rb") require "#{@root_dir}/_routes" if File.exists?("#{@root_dir}/_routes.rb") require "#{@root_dir}/routes" if File.exists?("#{@root_dir}/routes.rb") require "#{@factory_root_dir}/routes" end def load_redis self.class.load_redis(@redis_url) end def self.load_redis(redis_url=REDIS_URL) $Redis = ::Redis.new(url: redis_url) end end