Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@ukazap
Last active September 7, 2019 02:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukazap/743d6b03ec418d75ffdd4397891cea61 to your computer and use it in GitHub Desktop.
Save ukazap/743d6b03ec418d75ffdd4397891cea61 to your computer and use it in GitHub Desktop.
Rack middleware for GAE health check without a trip to DB
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
@ukazap
Copy link
Author

ukazap commented Sep 5, 2019

Usage

copy this file to lib/middlewares/

add this to application.rb:

require_relative '../lib/middlewares/ping_pong'

use it:

config.middleware.insert_before ActionDispatch::Static, PingPong::Middleware

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment