Last active
September 7, 2019 02:21
-
-
Save ukazap/743d6b03ec418d75ffdd4397891cea61 to your computer and use it in GitHub Desktop.
Rack middleware for GAE health check without a trip to DB
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
module PingPong # App Engine health check without a trip to DB | |
class Middleware | |
def initialize app, ping_path = '/status' | |
@app = app | |
@ping_path = ping_path | |
Rails.backtrace_cleaner.add_silencer { |line| line =~ /#{File.basename(__FILE__)}/ } | |
end | |
def pong_code | |
200 | |
end | |
def pong_headers | |
{'Content-Type' => 'application/json'} | |
end | |
def pong_body | |
{ | |
hello_there: 'I am alive!', | |
rails_env: ENV['RAILS_ENV'], | |
service: ENV['GAE_SERVICE'], | |
version: ENV['GAE_VERSION'] | |
}.to_json | |
end | |
def call env | |
req = Rack::Request.new env | |
if (req.get? || req.head?) && req.path.chomp('/') == @ping_path | |
[pong_code, pong_headers, [pong_body]] | |
else | |
@app.call env | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
copy this file to
lib/middlewares/
add this to
application.rb
:use it: