./lib/lux/config/config.rb in lux-fw-0.5.37 vs ./lib/lux/config/config.rb in lux-fw-0.6.2

- old
+ new

@@ -1,170 +1,119 @@ -# frozen_string_literal: true - -# $LOADED_FEATURES.select{ |f| f.index('/app/') || f.index('/lux/') } - require 'yaml' +require 'deep_merge' -module Lux::Config - extend self +module Lux + module Config + extend self - # requires all files recrusive in, with spart sort - def require_all dir_path - dir_path = dir_path.to_s.sub(/\/$/,'') - raise '* is not allowed' if dir_path.include?('*') - - 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 + def ram + `ps -o rss -p #{$$}`.chomp.split("\n").last.to_i / 1000 end - end - # preview config in development - def show_config - for k,v in Lux.config - next if v.kind_of?(Hash) - puts "* config :#{k} = #{v.kind_of?(Hash) ? '{...}' : v}" - end - end + def start_info + @load_info ||= proc do + info = [] - def live_require_check! - $live_require_check ||= Time.now + config = [] + %w(no_cache reload_code show_errors screen_log).each do |name| + value = Lux.env.send("#{name}?") + config.push value ? "#{name} (yes)".yellow : "#{name} (no)".green + end + info.push "Lux env: #{config.join(', ')}" - changed_files = $LOADED_FEATURES - .select{ |f| f.include?('/app/') || f.include?('lux') } - .select {|f| File.mtime(f) > $live_require_check } + if $lux_start_time.class == Array + # $lux_start_time ||= Time.now added to Gemfile + speed = 'in %s sec (%s gems, %s app)' % [ + time_diff($lux_start_time[0]).white, + time_diff($lux_start_time[0], $lux_start_time[1]), + time_diff($lux_start_time[1]), + ] + else + speed = 'in %s sec' % time_diff($lux_start_time).white + end - for file in changed_files - Lux.log ' Reloaded: %s' % file.split(Lux.root.to_s).last.red - load file + info.push "* Lux loaded in #{ENV['RACK_ENV']} mode, #{speed}, uses #{ram.to_s.white} MB RAM with total of #{Gem.loaded_specs.keys.length.to_s.white} gems in spec" + info.join($/) + end.call end - $live_require_check = Time.now - end + def set_defaults + ENV['LUX_ENV'] ||= '' + ENV['TZ'] ||= 'UTC' - def ram - `ps -o rss -p #{$$}`.chomp.split("\n").last.to_i / 1000 - end + # Delay + Lux.config.delay_timeout = Lux.env.dev? ? 3600 : 30 - def start! - Object.class_callback :config, Lux::Application - start_info $lux_start_time - end + # Logger + Lux.config.logger_path_mask = './log/%s.log' + Lux.config.logger_files_to_keep = 3 + Lux.config.logger_file_max_size = 10_240_000 + Lux.config.logger_formatter = nil - def start_info start=nil - return @load_info if @load_info + # Other + Lux.config.use_autoroutes = false + Lux.config.asset_root = false + Lux.config[:plugins] ||= [] + Lux.config[:error_logger] ||= Proc.new do |error| + ap [error.message, error.class, Lux::Error.mark_backtrace(error)] + end - production_mode = true - production_opts = [ - [: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 - production_mode = false unless config_ok + # Serve static files is on by default + Lux.config.serve_static_files = true - data = "#{key} (%s)" % [config_test ? :yes : :no] - config_ok ? data : data.yellow + # Etag and cache tags reset after deploy + Lux.config.deploy_timestamp = File.mtime('./Gemfile').to_i.to_s end - 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 + def app_timeout + @app_timeout ||= Lux.current.try('[]', :app_timeout) || Lux.config[:app_timeout] || (Lux.env.dev? ? 3600 : 30) + rescue + 30 end - 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" + # './config/secrets.yaml' + # default: + # development: + # foo: + # production: + # foo: + def load + source = Pathname.new './config/config.yaml' - @@load_info = info.join($/) + if source.exist? + data = YAML.safe_load source.read, aliases: true + base = data['default'] || data['base'] - puts @@load_info if start - end - - def init! - # Show server errors to a client - Lux.config.dump_errors = Lux.dev? - - # Log debug output to stdout - Lux.config.log_to_stdout = Lux.dev? - - # Automatic code reloads in development - 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 + if base + base.deep_merge!(data[Lux.env.to_s] || {}) + base['production'] = data['production'] + base + else + raise "Secrets :default root not defined in %s" % source + end + else + puts Lux.info '%s not found' % source + {} + end end - ### + private - # Default error logging - Lux.config.error_logger = proc do |error| - ap [error.class, error.message, Lux.error.split_backtrace(error)] - - 'no-key' + def time_diff time1, time2 = Time.now + ((time2 - time1)).round(2).to_s end - # Default mail logging - Lux.config.on_mail = proc do |mail| - Lux.logger(:email).info "[#{self.class}.#{@_template} to #{mail.to}] #{mail.subject}" - end + def env_value_of key, default = :_undef + value = ENV["LUX_#{key.to_s.upcase}"].to_s + value = true if ['true', 't', 'yes'].include?(value) + value = false if ['false', 'f', 'no'].include?(value) - # 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] + if default == :_undef + value + else + value.nil? ? deafult : value + end 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 - -if Lux.cli? - class Object - def reload! - Lux::Config.live_require_check! - end - end -end \ No newline at end of file