# frozen_string_literal: true
# $LOADED_FEATURES.select{ |f| f.index('/app/') || f.index('/lux/') }
require 'yaml'
module Lux::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
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 live_require_check!
$live_require_check ||= Time.now
changed_files = $LOADED_FEATURES
.select{ |f| f.include?('/app/') || f.include?('lux') }
.select {|f| File.mtime(f) > $live_require_check }
for file in changed_files
Lux.log ' Reloaded: %s' % file.split(Lux.root.to_s).last.red
load file
end
$live_require_check = Time.now
end
def ram
`ps -o rss -p #{$$}`.chomp.split("\n").last.to_i / 1000
end
def start!
Object.class_callback :config, Lux::Application
start_info $lux_start_time
end
def start_info start=nil
return @load_info if @load_info
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
data = "#{key} (%s)" % [config_test ? :yes : :no]
config_ok ? data : data.yellow
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
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"
@@load_info = info.join($/)
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
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('<', '<')
text = text.to_s.gsub($/,'
')
%[
#{text}