Sha256: a4840443a1cbd2ca3e3a30cd395d1ba739636aaebc166fa6be0171c74498d0a9

Contents?: true

Size: 1.59 KB

Versions: 1

Compression:

Stored size: 1.59 KB

Contents

require "web-facter/version"
require 'rack'
require 'facter'
require 'json'
require 'parseconfig'

module  WebFacter
  class App

    def initialize(filters=[])
      @filters = filters
    end

    def call(env)
      response = Rack::Response.new
      response.header['Content-Type'] = 'application/json'
      facts = Facter.to_hash.dup
      @filters.each do |filter|
        facts.delete(filter.strip)
      end
      response.write JSON.pretty_generate(facts)
      response.finish
    end

    def add_auth(conf)
      application = Rack::Auth::Basic.new(self) do |username, password|
        stored_username = conf.get_value('username')
        username_check = stored_username ? stored_username == username : true
        password_check = conf.get_value('password') == password
        username_check && password_check
      end
      application.realm = 'Web Facter'
      application
    end

    def self.run!(options)

      conf = options[:config] ? ParseConfig.new(options[:config]) : false

      if conf && conf.get_value('filters')
        application = self.new(conf.get_value('filters').split(','))
      else
        application = self.new
      end

      daemonize = options[:daemonize]
      port = options[:port]

      if conf
        application = application.add_auth(conf) if conf.get_value('password')
        daemonize = conf.get_value('daemonize') ? conf.get_value('daemonize') == "true" : daemonize
        port = conf.get_value('port') ? conf.get_value('port') : port
      end

      Rack::Server.new(:app => application, :Port => port, :daemonize => daemonize).start

    end
  end
end


Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
web-facter-0.1.2 lib/web-facter.rb