lib/omf-web/thin/server.rb in omf_web-1.2.1 vs lib/omf-web/thin/server.rb in omf_web-1.2.2
- old
+ new
@@ -39,16 +39,16 @@
OMF::Web.start(opts)
end
def load_environment(opts)
unless cf = opts[:omf_config_file]
- puts "Missing config file"
+ fatal "Missing config file"
abort
end
unless File.readable? cf
- puts "Can't read config file '#{cf}'"
+ fatal "Can't read config file '#{cf}'"
abort
end
@cfg_dir = File.dirname(cf)
opts[:static_dirs].insert(0, File.absolute_path(File.join(@cfg_dir, 'htdocs')))
@@ -73,57 +73,73 @@
end
(cfg[:repositories] || []).each do |repo|
load_repository(repo)
end
- unless wa = cfg[:widgets]
- puts "Can't find 'widgets' section in config file '#{cf}' - #{cfg.keys}"
+ widgets = cfg[:widgets] || []
+ if (tabs = cfg[:tabs])
+ tabs.each {|t| t[:top_level] = true}
+ widgets += tabs
+ end
+ if widgets.empty?
+ fatal "Can't find 'widgets' or 'tabs' section in config file '#{cf}' - #{cfg.keys}"
abort
end
- wa.each do |w|
- OMF::Web.register_widget w
+ widgets.each do |w|
+ register_widget w
end
+
+ # Any other file to load before opening shop
+ if init = cfg[:init]
+ unless init.is_a? Enumerable
+ init = [init]
+ end
+ init.each {|f| load_ruby_file(f) }
+ end
+
end
def load_datasource(config)
unless id = config[:id]
- puts "Missing id in datasource configuration"
+ fatal "Missing id in datasource configuration"
abort
end
if config[:database]
load_database(id, config)
elsif config[:file]
load_datasource_file(id, config)
elsif config[:omsp]
load_omsp_endpoint(id, config)
+ elsif config[:generator]
+ load_generator(id, config[:generator])
else
abort "Unknown datasource type - #{config}"
end
end
def load_database(id, config)
unless db_cfg = config[:database]
- puts "Missing database configuration in datasource '#{config}'"
+ fatal "Missing database configuration in datasource '#{config}'"
abort
end
db = get_database(db_cfg)
if query_s = config[:query]
unless schema = config[:schema]
- puts "Missing schema configuration in datasource '#{config}'"
+ fatal "Missing schema configuration in datasource '#{config}'"
abort
end
require 'omf_oml/schema'
config[:schema] = OMF::OML::OmlSchema.create(schema)
table = db.create_table(id, config)
else
unless table_name = config.delete(:table)
- puts "Missing 'table' in datasource configuration '#{config}'"
+ fatal "Missing 'table' in datasource configuration '#{config}'"
abort
end
config[:name] = id
unless table = db.create_table(table_name, config)
- puts "Can't find table '#{table_name}' in database '#{db_cfg}'"
+ fatal "Can't find table '#{table_name}' in database '#{db_cfg}'"
abort
end
end
OMF::Web.register_datasource table, name: id
end
@@ -134,111 +150,161 @@
if config.is_a? String
if db = @databases[config]
return db
end
- puts "Database '#{config}' not defined - (#{@databases.keys})"
+ fatal "Database '#{config}' not defined - (#{@databases.keys})"
abort
end
- unless id = config.delete(:id)
- puts "Missing id in database configuration"
- abort
+ if id = config.delete(:id)
+ if db = @databases[id.to_s] # already known
+ return db
+ end
end
- if db = @databases[id.to_s] # already known
- return db
- end
# unless id = config[:id]
- # puts "Database '#{config}' not defined - (#{@databases.keys})"
+ # fatal "Database '#{config}' not defined - (#{@databases.keys})"
# abort
# end
unless url = config.delete(:url)
- puts "Missing URL for database '#{id}'"
+ fatal "Missing URL for database '#{id}'"
abort
end
if url.start_with?('sqlite:') && ! url.start_with?('sqlite:/')
# inject top dir
url.insert('sqlite:'.length, '//' + @cfg_dir + '/')
end
config[:check_interval] ||= 3.0
puts "URL: #{url} - #{config}"
begin
- return @databases[id] = OMF::OML::OmlSqlSource.new(url, config)
+ db = OMF::OML::OmlSqlSource.new(url, config)
+ @databases[id] = db if id
+ return db
rescue Exception => ex
- puts "Can't connect to database '#{id}' - #{ex}"
+ fatal "Can't connect to database '#{id}' - #{ex}"
abort
end
end
# The data to be served as a datasource is contained in a file. We
# currently support CSV with headers, and JSON which turns into a
# 1 col by 1 row datasource.
#
def load_datasource_file(name, opts)
unless file = opts[:file]
- puts "Data source file is not defined in '#{opts}'"
+ fatal "Data source file is not defined in '#{opts}'"
abort
end
unless file.start_with? '/'
- file = File.join(@top_dir, file)
+ File.absolute_path(file, @cfg_dir)
end
unless File.readable? file
- puts "Can't read file '#{file}'"
+ fatal "Can't read file '#{file}'"
abort
end
- case content_type = opts[:content_type].to_s
+ unless content_type = opts[:content_type]
+ content_type = File.extname(file)[1 .. -1]
+ end
+ case content_type.to_s
when 'json'
ds = JSONDataSource.new(file)
when 'csv'
require 'omf_oml/csv_table'
ds = OMF::OML::OmlCsvTable.create name, file, has_csv_header: true
else
- puts "Unknown content type '#{content_type}'"
+ fatal "Unknown content type '#{content_type}'"
abort
end
OMF::Web.register_datasource ds, name: name
end
def load_omsp_endpoint(id, config)
oconfig = config[:omsp]
unless port = oconfig[:port]
- puts "Need port in OMSP definition '#{oconfig}' - datasource '#{id}'"
+ fatal "Need port in OMSP definition '#{oconfig}' - datasource '#{id}'"
abort
end
ep = @omsp_endpoints[port] ||= OmspEndpointProxy.new(port)
ep.add_datasource(id, config)
end
+ def load_generator(id, config)
+ if file = config[:load]
+ load_ruby_file(file)
+ end
+ unless klass_name = config[:class]
+ fatal "Missing 'class' options for generator '#{id}'"
+ abort
+ end
+ klass = nil
+ begin
+ klass = Kernel.const_get(klass_name)
+ rescue
+ fatal "Can't find class '#{klass_name}' referenced in generator '#{id}'"
+ abort
+ end
+ opts = config[:opts] || {}
+ debug "Creating new generator '#{id}' from '#{klass_name}' with '#{opts}'"
+ unless klass.respond_to? :create_data_source
+ fatal "Class '#{klass_name}' doesn't have a 'create_data_source' class method."
+ abort
+ end
+ klass.create_data_source(id, opts)
+ end
+ def load_ruby_file(file)
+ unless file.start_with? '/'
+ file = File.absolute_path(file, @cfg_dir)
+ end
+ unless File.readable? file
+ fatal "Can't read file '#{file}'"
+ abort
+ end
+ debug "Loading #{file}"
+ load(file)
+ end
+
+
def load_repository(config)
unless id = config[:id]
- puts "Missing id in respository configuration"
+ fatal "Missing id in respository configuration"
abort
end
unless type = config[:type]
- puts "Missing 'type' in respository configuration '#{id}'"
+ fatal "Missing 'type' in respository configuration '#{id}'"
abort
end
require 'omf-web/content/repository'
case type
when 'file'
unless top_dir = config[:top_dir]
- puts "Missing 'top_dir' in respository configuration '#{id}'"
+ fatal "Missing 'top_dir' in respository configuration '#{id}'"
abort
end
unless top_dir.start_with? '/'
- top_dir = File.join(@top_dir, top_dir)
+ top_dir = File.join(@cfg_dir, top_dir)
end
+ #puts "TOP>>> #{File.absolute_path top_dir}"
OMF::Web::ContentRepository.register_repo(id, type: :file, top_dir: top_dir)
else
- puts "Unknown repository type '#{type}'. Only supporting 'file'."
+ fatal "Unknown repository type '#{type}'. Only supporting 'file'."
abort
end
end
+ def register_widget(w)
+ unless w[:id]
+ require 'digest/md5'
+ w[:id] = Digest::MD5.hexdigest(w[:name] || "tab#{rand(10000)}")[0, 8]
+ end
+ w[:top_level] = true
+ w[:type] ||= 'layout/one_column'
+ OMF::Web.register_widget w
+ end
+
# Recusively Symbolize keys of hash
#
def _rec_sym_keys(hash)
h = {}
hash.each do |k, v|
@@ -328,6 +394,6 @@
end
end # class OmspEndpointProxy
end # class
-end # module
\ No newline at end of file
+end # module