Skip to content
/ registry Public

Framework for controlling application behavior through configurable properties

License

Notifications You must be signed in to change notification settings

berk/registry

Repository files navigation

=== Registry Engine

  This engine provides a registry mechanism which can be used to enable/disable
  functionality or change parameters at runtime.


=== Installation

  script/plugin install git://github.com/berk/registry.git


=== Test

  script/server
  point your browser at http://localhost:3000/registry


=== Usage

  Configure the plugin

  in config/initializers/registry.rb

    Registry.configure do |config|

      # This permission check is used to access the registry UI
      config.permission_check { redirect_to login_path and return false unless current_user.admin? }

      # The layout used by the registry UI
      config.layout = 'admin'

      # user id to blame when updating entries
      config.user_id = { current_user.id }
    end


=== Example usage

  in app/controllers/api_controller.rb
    
    class ApiController < ActionController::Base

      before_filter :ensure_api_enabled, :check_rate_limit

      ...

    private

      def ensure_api_enabled
        raise ServiceUnavailableError.new('API Disabled') unless Registry.api.enabled?
      end

      def check_rate_limit
        cache_key = "api_rate_limit_p#{current_user.id}"

        requests = Rails.cache.read(cache_key).to_i
        if requests >= Registry.api.request_limit
          # Over the limit.
          raise ForbiddenError.new('Rate limit exceeded.')
        else
          # Under the limit.
          Rails.cache.write(cache_key, (requests+1).to_s, :expires_in => Registry.api.request_window)
        end
      end

    end # class ApiController


  ---

  To initialize your registry, create a file of default values and add a rake task.


  in config/registry.yml

    defaults:
      api_enabled:        true
      api_request_limit:  1
      api_request_window: 1000

    development:
      api_request_window: 100
   
    test:
      api_request_window: 100
   
    production:
      api_enabled:        false
      api_request_limit:  10
      api_request_window: 10000
   

  in lib/tasks/my_tasks.rake

    desc 'Import configuration'
    task :import_configuration => [:environment] do
      Registry.import("#{Rails.root}/config/registry.yml")
    end

  ---

  You can update the registry via console.

  Registry.api.enabled = false

About

Framework for controlling application behavior through configurable properties

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •