Sha256: 9589f29372c0a0c78caa100fe608ef8b70f98bbdd5044bbf2127a263fc6b258b

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

require 'configatron'
require 'jettr/server'
require 'jettr/handler'

module Jettr
  class Config
    attr_reader :config
    def initialize(options={})
      @config = Configatron::Store.new
      if options[:config_file] && File.exist?(options[:config_file])
        config.configure_from_yaml(options[:config_file])
        config.base_path = File.expand_path(File.dirname(options[:config_file]))
      else
        config.configure_from_hash(options)
      end
      config.server.set_default(:port, 8080)
      config.set_default(:apps, [])
    end
    
    def store
      return @config
    end
    
    def create_server
      puts "Creating server..."
      server = Jettr::Server.new(config.server.to_hash)
      config.apps.each do |app_config|
        app_config.symbolize_keys!
        if app_config[:app_path] && config.exists?(:base_path)
          app_config[:app_path] = File.expand_path(File.join(config.base_path, app_config[:app_path]))
        elsif app_config[:app_path]
          app_config[:app_path] = File.expand_path(app_config[:app_path])
        end
        server.handlers << create_app(app_config)
        puts "Added handler: #{server.handlers.last.inspect}"
      end
      server
    end
    
    def create_app(app_config)
      puts "Creating app handler: #{app_config.inspect}"
      app_type = app_config.delete(:type)
      app_type = app_type ? app_type.to_sym : app_type
      handler_class = Jettr::Handler::HANDLERS[app_type] || Jettr::Handler::HANDLERS[:default]
      handler_class.new(app_config)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jettr-0.2.1-java lib/jettr/config.rb