forked from ninjudd/registry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME
More file actions
113 lines (72 loc) · 2.49 KB
/
README
File metadata and controls
113 lines (72 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
=== Registry Engine
This engine provides a registry mechanism which can be used to enable/disable
functionality or change parameters at runtime.
=== Installation
# Gemfile
gem 'registry', :git => 'https://github.com/ss/registry.git'
== Testing/Coverage
# setup test environment
rbenv install 1.9.3-p551
gem install bundler -v '~>1.0.0'
bundle install --clean --path=vendor/bundle
bundle exec rake db:migrate
# run tests
bundle exec rake test
# generate coverage report
bundle exec rake registry:coverage
# point your browser at http://localhost:3000/coverage/
=== 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", :verbose => true)
end
---
You can update the registry via console.
Registry.api.enabled = false