Class: OMF::Web::Server

Inherits:
Base::LObject
  • Object
show all
Defined in:
lib/omf-web/thin/server.rb

Overview

Most of the code to run an OMF Web server from a configuration file

USAGE:

Defined Under Namespace

Classes: JSONDataSource, OmspEndpointProxy

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server_name, description, top_dir, opts) ⇒ Server

Returns a new instance of Server.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/omf-web/thin/server.rb', line 18

def initialize(server_name, description, top_dir, opts)
  OMF::Base::Loggable.init_log(server_name)
  #OMF::Base::Loggable.init_log server_name
  opts = {
    handlers: {
      pre_parse: lambda do |p, runner|
        p.on("--config CONF_FILE", "File holding description of web site") {|f| runner.options[:omf_config_file] = f}
        p.on("--top-dir DIR", "Directory to start from for relative data paths [directory of config file]") {|td| @top_dir = td }
        p.on("--example [EXAMPLE]", "Run an example [#{list_of_examples.join(', ')}]")  do |e|
          unless e
            puts "Available examples: #{list_of_examples.join(', ')}"
            exit 0
          end
          runner.options[:omf_config_file] = "example:#{e}"
        end
      end
    },
    authentication: {
      required: false
    }
  }.merge(opts)
  post_parse = opts[:handlers][:post_parse]
  opts[:handlers][:post_parse] = lambda do |r|
    post_parse.call(r) if post_parse
    load_environment(r.options)
  end
  @server_name = server_name
  @top_dir = top_dir
  @databases = {}
  @omsp_endpoints = {}

  OMF::Web.start(opts)
end

Class Method Details

.start(server_name, description, top_dir, opts = {}) ⇒ Object



14
15
16
# File 'lib/omf-web/thin/server.rb', line 14

def self.start(server_name, description, top_dir, opts = {})
  self.new(server_name, description, top_dir, opts)
end

Instance Method Details

#_rec_sym_keys(hash) ⇒ Object

Recusively Symbolize keys of hash



395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/omf-web/thin/server.rb', line 395

def _rec_sym_keys(hash)
  h = {}
  hash.each do |k, v|
    if v.is_a? Hash
      v = _rec_sym_keys(v)
    elsif v.is_a? Array
      v = v.map {|e| e.is_a?(Hash) ? _rec_sym_keys(e) : e }
    end
    h[k.to_sym] = v
  end
  h
end

#check_for_builtin(cf, opts) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/omf-web/thin/server.rb', line 72

def check_for_builtin(cf, opts)
  pa = cf.split(':')
  unless pa.length == 2 && pa[0] == 'example'
    return nil
  end
  path = File.join(@top_dir, 'example', pa[1])
  if File.directory? path
    dir = path
    path = File.join(dir, "#{pa[1]}.yaml")
    unless File.readable? path
      # check .yml
      path = File.join(dir, "#{pa[1]}.yml")
    end
  end
  unless File.readable?(path)
    da = list_of_examples
    ex = pa[1].split('/')[0]
    if da.include? ex
      ya = Dir.glob(File.join(@top_dir, 'example', ex, '*.yaml')).map do |fn|
        File.basename(fn)
      end
      fatal "Unknown config file. Did you mean '#{ya.join(', ')}'?"
    else
      fatal "Unknown example '#{}'. Did you mean '#{da.join(', ')}'?"
    end
    abort
  end
  return path
end

#get_database(config) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/omf-web/thin/server.rb', line 235

def get_database(config)
  require 'omf_oml/table'
  require 'omf_oml/sql_source'
  if config.is_a? String
    if db = @databases[config]
      return db
    end
    fatal "Database '#{config}' not defined - (#{@databases.keys})"
    abort
  end
  if id = config.delete(:id)
    if db = @databases[id.to_s] # already known
      return db
    end
  end

  # unless id = config[:id]
    # fatal "Database '#{config}' not defined - (#{@databases.keys})"
    # abort
  # end
  unless url = config.delete(:url)
    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
    db = OMF::OML::OmlSqlSource.new(url, config)
    @databases[id] = db if id
    return db
  rescue Exception => ex
    fatal "Can't connect to database '#{id}' - #{ex}"
    abort
  end
end

#list_of_examplesObject



102
103
104
105
106
# File 'lib/omf-web/thin/server.rb', line 102

def list_of_examples()
  Dir.entries(File.join(@top_dir, 'example')).select do |n|
    !(n == 'NOT_WORKING' || n.start_with?('.'))
  end
end

#load_config_file(cf, opts) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/omf-web/thin/server.rb', line 108

def load_config_file(cf, opts)
  debug "Loading config file '#{cf}'"
  cfg = _rec_sym_keys(YAML.load_file(cf))

  if log_opts = cfg[:logging]
    # TODO: Deal with custom logging option
  end

  (cfg[:server] || {}).each do |k, v|
    k = k.to_sym
    case k
    when :port
      opts[:port] = v.to_i
    else
      opts[k] = v
    end
  end
  (cfg[:data_sources] || []).each do |ds|
    load_datasource(ds)
  end
  (cfg[:repositories] || []).each do |repo|
    load_repository(repo)
  end

  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
  widgets.each do |w|
    register_widget w
  end

  # Any other file to load before opening shop
  if load = cfg[:load]
    unless load.is_a? Enumerable
      load = [load]
    end
    load.each do |g| #{|f| load_ruby_file(f) }
      unless g.start_with? '/'
        g = File.absolute_path(g, File.dirname(cf))
      end
      found_something = false
      Dir.glob(g).each do |f|
        found_something = true
        load_ruby_file(f)
      end
      unless found_something
        fatal "Couldn't find any load file for pattern '#{g}'"
        abort
      end
    end
  end

  # Any other configure to load
  if include = cfg[:include]
    unless include.is_a? Enumerable
      include = [include]
    end
    include.each do |g|
      unless g.start_with? '/'
        g = File.absolute_path(g, File.dirname(cf))
      end
      found_something = false
      Dir.glob(g).each do |f|
        found_something = true
        load_config_file(f, opts)
      end
      unless found_something
        fatal "Couldn't find any config file for pattern '#{g}'"
        abort
      end
    end
  end

end

#load_database(id, config) ⇒ Object



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/omf-web/thin/server.rb', line 207

def load_database(id, config)
  unless db_cfg = config[:database]
    fatal "Missing database configuration in datasource '#{config}'"
    abort
  end
  db = get_database(db_cfg)
  if query_s = config[:query]
    unless schema = config[:schema]
      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)
      fatal "Missing 'table' in datasource configuration '#{config}'"
      abort
    end
    config[:name] = id
    unless table = db.create_table(table_name, config)
      fatal "Can't find table '#{table_name}' in database '#{db_cfg}'"
      abort
    end
  end
  OMF::Web.register_datasource table, name: id
end

#load_datasource(config) ⇒ Object



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/omf-web/thin/server.rb', line 189

def load_datasource(config)
  unless id = config[:id]
    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

#load_datasource_file(name, opts) ⇒ Object

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.



279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/omf-web/thin/server.rb', line 279

def load_datasource_file(name, opts)
  unless file = opts[:file]
    fatal "Data source file is not defined in '#{opts}'"
    abort
  end
  unless file.start_with? '/'
    file = File.absolute_path(file, @cfg_dir)
  end
  unless File.readable? file
    fatal "Can't read file '#{file}'"
    abort
  end
  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
    fatal "Unknown content type '#{content_type}'"
    abort
  end
  OMF::Web.register_datasource ds, name: name
end

#load_environment(opts) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/omf-web/thin/server.rb', line 52

def load_environment(opts)
  unless cf = opts[:omf_config_file]
    fatal "Missing config file '--config'"
    abort
  end

  unless File.readable? cf
    unless cf2 = check_for_builtin(cf, opts)
      fatal "Can't read config file '#{cf}'"
      abort
    end
    cf = cf2 # found a builtin config file
  end

  @cfg_dir = File.dirname(cf)
  opts[:static_dirs].insert(0, File.absolute_path(File.join(@cfg_dir, 'htdocs')))
  @top_dir ||= @cfg_dir
  load_config_file(cf, opts)
end

#load_generator(id, config) ⇒ Object



317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
# File 'lib/omf-web/thin/server.rb', line 317

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

#load_omsp_endpoint(id, config) ⇒ Object



307
308
309
310
311
312
313
314
315
# File 'lib/omf-web/thin/server.rb', line 307

def load_omsp_endpoint(id, config)
  oconfig = config[:omsp]
  unless port = oconfig[:port]
    fatal "Need port in OMSP definition '#{oconfig}' - datasource '#{id}'"
    abort
  end
  ep = @omsp_endpoints[port] ||= OmspEndpointProxy.new(port)
  ep.add_datasource(id, config)
end

#load_repository(config) ⇒ Object



354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/omf-web/thin/server.rb', line 354

def load_repository(config)
  unless id = config[:id]
    fatal "Missing id in respository configuration"
    abort
  end
  unless type = config[:type]
    fatal "Missing 'type' in respository configuration '#{id}'"
    abort
  end

  require 'omf-web/content/repository'
  case type
  when 'file'
    unless top_dir = config[:top_dir]
      fatal "Missing 'top_dir' in respository configuration '#{id}'"
      abort
    end
    unless top_dir.start_with? '/'
      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
    fatal "Unknown repository type '#{type}'. Only supporting 'file'."
    abort
  end

end

#load_ruby_file(file) ⇒ Object



341
342
343
344
345
346
347
348
349
350
351
# File 'lib/omf-web/thin/server.rb', line 341

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

#register_widget(w) ⇒ Object



383
384
385
386
387
388
389
390
391
# File 'lib/omf-web/thin/server.rb', line 383

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