Class: OMF::Web::Server
- Inherits:
-
Common::LObject
- Object
- Common::LObject
- OMF::Web::Server
- 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
Class Method Summary collapse
Instance Method Summary collapse
-
#_rec_sym_keys(hash) ⇒ Object
Recusively Symbolize keys of hash.
- #get_database(config) ⇒ Object
-
#initialize(server_name, description, top_dir, opts) ⇒ Server
constructor
A new instance of Server.
- #load_database(config) ⇒ Object
- #load_datasource(config) ⇒ Object
-
#load_datasource_file(name, opts) ⇒ Object
The data to be served as a datasource is contained in a file.
- #load_environment(opts) ⇒ Object
- #load_repository(config) ⇒ Object
Methods included from Common::Loggable
#_logger, #debug, #error, #fatal, #info, init_log, logger, set_environment, #warn
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 |
# File 'lib/omf-web/thin/server.rb', line 18 def initialize(server_name, description, top_dir, opts) OMF::Common::Loggable.init_log server_name opts = { static_dirs_pre: ["#{top_dir}/htdocs"], handlers: { pre_parse: lambda do |p, runner| p.on("--config CONF_FILE", "File holding description of web site") {|f| runner.[: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 } end, post_parse: lambda { |r| load_environment(r.) }, }, authentication: { required: false } }.merge(opts) @top_dir = top_dir @databases = {} 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
207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/omf-web/thin/server.rb', line 207 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 |
#get_database(config) ⇒ Object
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 |
# File 'lib/omf-web/thin/server.rb', line 112 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 puts "Database '#{config}' not defined - (#{@databases.keys})" abort end unless id = config[:id] puts "Missing id in database configuration" abort end # unless id = config[:id] # puts "Database '#{config}' not defined - (#{@databases.keys})" # abort # end unless url = config[:url] puts "Missing URL for database '#{id}'" abort end if url.start_with?('sqlite://') && ! url.start_with?('sqlite:///') # inject top dir url.insert('sqlite://'.length, @top_dir + '/') end puts "URL: #{url}" begin return @databases[id] = OMF::OML::OmlSqlSource.new(url, :check_interval => 3.0) rescue Exception => ex puts "Can't connect to database '#{id}' - #{ex}" abort end end |
#load_database(config) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/omf-web/thin/server.rb', line 95 def load_database(config) unless table_name = config[:table] puts "Missing 'table' in datasource configuration '#{id}'" abort end unless db_cfg = config[:database] puts "Missing database configuration in datasource '#{id}'" abort end db = get_database(db_cfg) unless table = db.create_table(table_name) puts "Can't find table '#{table_name}' in database '#{db}'" abort end OMF::Web.register_datasource table, name: id end |
#load_datasource(config) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/omf-web/thin/server.rb', line 80 def load_datasource(config) unless id = config[:id] puts "Missing id in datasource configuration" abort end case type = config[:type] || 'database' when 'database' load_database(config) when 'file' load_datasource_file(id, config) else abort "Unknown datasource type '#{type}'." 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.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
# File 'lib/omf-web/thin/server.rb', line 152 def load_datasource_file(name, opts) unless file = opts[:file] puts "Data source file is not defined in '#{opts}'" abort end unless file.start_with? '/' file = File.join(@top_dir, file) end unless File.readable? file puts "Can't read file '#{file}'" abort end case content_type = opts[: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}'" abort end OMF::Web.register_datasource ds, name: name end |
#load_environment(opts) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/omf-web/thin/server.rb', line 41 def load_environment(opts) unless cf = opts[:omf_config_file] puts "Missing config file" abort end unless File.readable? cf puts "Can't read config file '#{cf}'" abort end @top_dir ||= File.dirname(cf) cfg = _rec_sym_keys(YAML.load_file(cf)) (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 unless wa = cfg[:widgets] puts "Can't find 'widgets' section in config file '#{cf}' - #{cfg.keys}" abort end wa.each do |w| OMF::Web. w end end |
#load_repository(config) ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/omf-web/thin/server.rb', line 177 def load_repository(config) unless id = config[:id] puts "Missing id in respository configuration" abort end unless type = config[:type] puts "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}'" abort end unless top_dir.start_with? '/' top_dir = File.join(@top_dir, top_dir) end OMF::Web::ContentRepository.register_repo(id, type: :file, top_dir: top_dir) else puts "Unknown repository type '#{type}'. Only supporting 'file'." abort end end |