./lib/lux/config/config.rb in lux-fw-0.2.3 vs ./lib/lux/config/config.rb in lux-fw-0.5.32

- old
+ new

@@ -1,26 +1,21 @@ # frozen_string_literal: true # $LOADED_FEATURES.select{ |f| f.index('/app/') || f.index('/lux/') } +require 'yaml' + module Lux::Config extend self - LIVE_RELOAD ||= {} - - # if we have errors in module loading, try to load them one more time - @module_error = [] - # requires all files recrusive in, with spart sort - def require_all files - files = files.to_s - files += '/*' unless files.include?('*') + def require_all dir_path + dir_path = dir_path.to_s.sub(/\/$/,'') + raise '* is not allowed' if dir_path.include?('*') - file_errors = [] - glob = `echo #{files} #{files}/* #{files}/*/* #{files}/*/*/* #{files}/*/*/*/* |tr ' ' '\n' | grep .rb`.split("\n") - - glob.select{ |o| o.index('.rb') }.map{ |o| o.split('.rb')[0]}.each do |ruby_file| + glob = `echo #{dir_path}/* #{dir_path}/*/* #{dir_path}/*/*/* #{dir_path}/*/*/*/* #{dir_path}/*/*/*/*/* #{dir_path}/*/*/*/*/*/* |tr ' ' '\n' | grep .rb`.split("\n") + glob.select{ |o| o.index('.rb') }.each do |ruby_file| require ruby_file end end # preview config in development @@ -29,46 +24,43 @@ next if v.kind_of?(Hash) puts "* config :#{k} = #{v.kind_of?(Hash) ? '{...}' : v}" end end - # check all files and reload if needed def live_require_check! - if LIVE_RELOAD.blank? - root = Lux.root.to_s - for file in $LOADED_FEATURES.select{ |f| f.index(root) || f.index('/lux/') } - LIVE_RELOAD[file] = File.mtime(file).to_i - end - end + $live_require_check ||= Time.now - for file, mtime in LIVE_RELOAD - new_file_mtime = File.mtime(file).to_i + changed_files = $LOADED_FEATURES + .select{ |f| f.include?('/app/') || f.include?('lux') } + .select {|f| File.mtime(f) > $live_require_check } - next if mtime == new_file_mtime - LIVE_RELOAD[file] = new_file_mtime - Lux.log " Reloaded: .#{file.split(Lux.root.to_s).last.red}" + for file in changed_files + Lux.log ' Reloaded: %s' % file.split(Lux.root.to_s).last.red load file end - true + + $live_require_check = Time.now end def ram `ps -o rss -p #{$$}`.chomp.split("\n").last.to_i / 1000 end - def show_load_speed load_start=nil - return @@load_info || 'No lux load info' unless load_start + def start! + Object.class_callback :config, Lux::Application + start_info $lux_start_time + end - speed = ((Time.now - load_start)*1000).round.to_s.sub(/(\d)(\d{3})$/,'\1s \2')+'ms' + def start_info start=nil + return @load_info if @load_info production_mode = true - production_opts = [ - [:compile_assets, false], - [:auto_code_reload, false], - [:show_server_errors, false], - [:log_to_stdout, false], + [:compile_assets, false], + [:auto_code_reload, false], + [:dump_errors, false], + [:log_to_stdout, false], ] opts = production_opts.map do |key, production_value| config_test = Lux.config(key) config_ok = production_value == config_test @@ -76,38 +68,103 @@ data = "#{key} (%s)" % [config_test ? :yes : :no] config_ok ? data : data.yellow end - puts @@load_info = '* Config: %s' % opts.join(', ') - - mode = production_mode ? 'production'.green : 'development'.yellow - - "* #{'Lux'.white} loaded #{mode} mode in #{speed.to_s.white}, uses #{ram.to_s.white} MB RAM with total of #{Gem.loaded_specs.keys.length.to_s.white} gems in spec".tap do |it| - @@load_info += "\n#{it}" + mode = production_mode ? 'production'.green : 'development'.yellow + speed = + if start + text = ((Time.now - start)*1000).round.to_s.sub(/(\d)(\d{3})$/,'\1s \2') + ' in %s ms' % text.to_s.white + else end - end - def set_default_vars - # how long will session last if BROWSER or IP change - Lux.config.session_forced_validity = 5.minutes.to_i + info = [] + info.push '* Config: %s' % opts.join(', ') + info.push "* Lux loaded #{mode} mode#{speed}, uses #{ram.to_s.white} MB RAM with total of #{Gem.loaded_specs.keys.length.to_s.white} gems in spec" - # name of the session cookie - Lux.config.session_cookie_name = '__luxs' + @@load_info = info.join($/) + puts @@load_info if start + end + + def init! # Show server errors to a client - Lux.config.show_server_errors = false + Lux.config.dump_errors = Lux.dev? # Log debug output to stdout - Lux.config.log_to_stdout = false + Lux.config.log_to_stdout = Lux.dev? # Automatic code reloads in development - Lux.config.auto_code_reload = false - end + Lux.config.auto_code_reload = Lux.dev? + # Runtime compile js and css assets + Lux.config.compile_assets = Lux.dev? + + Lux.config.session_cookie_domain = false + Lux.config.asset_root = false + + ### + + if ENV['LUX_MODE'].to_s.downcase == 'log' + Lux.config.dump_errors = false + Lux.config.auto_code_reload = false + Lux.config.compile_assets = false + end + + ### + + # Default error logging + Lux.config.error_logger = proc do |error| + ap [error.class, error.message, Lux.error.split_backtrace(error)] + + 'no-key' + end + + # Default mail logging + Lux.config.on_mail = proc do |mail| + Lux.logger(:email).info "[#{self.class}.#{@_template} to #{mail.to}] #{mail.subject}" + end + + # default event bus error handle + Lux.config.on_event_bus_error = proc do |error, name| + Lux.logger(:event_bus).error '[%s] %s' % [name, error.message] + end + + # server static files + Lux.config.serve_static_files = true + + # Template to show when displaying unhandled server side errors + Lux.config.server_error_template = proc do |text| + text = text.to_s.gsub('<', '&lt;') + text = text.to_s.gsub($/,'<br />') + + %[<html> + <head> + <title>Server error (#{Lux.current.response.status})</title> + </head> + <body style="background:#fff; font-size:12pt; font-family: Arial; padding: 20px;"> + <h3>HTTP error #{Lux.current.response.status} in #{Lux.config.app.name}</h3> + <pre style="color:red; padding:10px; background-color: #eee; border: 1px solid #ccc; font-family:'Lucida console'; line-height: 15pt;">#{text}</pre> + <br> + <a href="https://httpstatuses.com/#{Lux.current.response.status}" target="http_error">more info on http error</a> + </body> + </html>] + end + + # inflector + String.inflections do |inflect| + inflect.plural 'bonus', 'bonuses' + inflect.plural 'clothing', 'clothes' + inflect.plural 'people', 'people' + inflect.singular /news$/, 'news' + end + end end -class Object - def reload! - Lux::Config.live_require_check! +if Lux.cli? + class Object + def reload! + Lux::Config.live_require_check! + end end end \ No newline at end of file