Skip to content

Instantly share code, notes, and snippets.

@marten
Created July 2, 2014 09:54
Show Gist options
  • Save marten/3a5f9d0336ccae063dd9 to your computer and use it in GitHub Desktop.
Save marten/3a5f9d0336ccae063dd9 to your computer and use it in GitHub Desktop.
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