Created
July 2, 2014 09:54
-
-
Save marten/3a5f9d0336ccae063dd9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BalancerState | |
InvalidStatus = Class.new(StandardError) | |
def initialize(disk_access = File) | |
@disk_access = disk_access | |
end | |
def call(env) | |
if balancer_member? | |
[200, {'Content-Type' => 'application/json'}, ['{"status": "ok", "member": true}']] | |
else | |
[503, {'Content-Type' => 'application/json'}, ['{"status": "ok", "member": false}']] | |
end | |
rescue InvalidStatus | |
[500, {'Content-Type' => 'application/json'}, ['{"status": "unknown status"}']] | |
end | |
private | |
def balancer_member? | |
state = @disk_access.read(File.expand_path('../config/balancer_state', __FILE__)).strip | |
raise InvalidStatus, "Invalid state: #{state}" unless %w(on off).include?(state) | |
state == "on" | |
end | |
end | |
app = Rack::Builder.app do | |
map '/load_balancer_status' do | |
run BalancerState.new | |
end | |
map '/' do | |
run lambda { |env| [200, {'Content-Type' => 'text/plain'}, ['Je hebt mij bereikt!']] } | |
end | |
end | |
run app |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment