├── .github └── workflows │ └── rubygems.yml ├── .gitignore ├── .travis.yml ├── Gemfile ├── Gemfile.lock ├── MIT-LICENSE ├── README.md ├── Rakefile ├── bin └── rails ├── healthcheck.gemspec ├── lib ├── aws-healthcheck.rb └── healthcheck │ ├── middleware.rb │ ├── railtie.rb │ └── version.rb └── test ├── healthcheck_test.rb └── test_helper.rb /.github/workflows/rubygems.yml: -------------------------------------------------------------------------------- 1 | name: Publish Gem 2 | 3 | on: 4 | push: 5 | branches: 6 | - "master" 7 | tags: 8 | - v* 9 | jobs: 10 | build: 11 | runs-on: ubuntu-latest 12 | 13 | steps: 14 | - uses: actions/checkout@v1 15 | 16 | - name: Release Gem 17 | if: contains(github.ref, 'refs/tags/v') 18 | uses: cadwallion/publish-rubygems-action@master 19 | env: 20 | GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}} 21 | RUBYGEMS_API_KEY: ${{secrets.RUBYGEMS_API_KEY}} 22 | RELEASE_COMMAND: rake release 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .bundle/ 2 | log/*.log 3 | pkg/ 4 | *.gem 5 | 6 | .ruby-version 7 | 8 | test/dummy/db/*.sqlite3 9 | test/dummy/db/*.sqlite3-journal 10 | test/dummy/log/*.log 11 | test/dummy/tmp/ 12 | test/dummy/.sass-cache 13 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: ruby 3 | rvm: 4 | - 2.2.3 5 | - 2.3.0 6 | before_install: gem install bundler -v 1.13.6 7 | cache: bundler 8 | deploy: 9 | provider: rubygems 10 | gem: 11 | master: aws-healthcheck 12 | on: 13 | repo: lserman/healthcheck 14 | branch: master 15 | api_key: 16 | secure: Ph2hsMBioV9mlg1gAJSVqWdTsOj+HAW2in1ALAAv50kBTyRv5iNO8BF8F5uiEWVsBgIqn0AXfrI5RFz9auSq4MRO1Wb88dq1rbEymxv6qbXr+mDsG2HUuPLV/l002aQOTpzJpfktyNcau9dQv5erBH5ODwUTykpShlyWw3tqs4M= 17 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "https://rubygems.org" 2 | 3 | # Declare your gem's dependencies in healthcheck.gemspec. 4 | # Bundler will treat runtime dependencies like base dependencies, and 5 | # development dependencies will be added by default to the :development group. 6 | gemspec 7 | 8 | # Declare any dependencies that are still in development here instead of in 9 | # your gemspec. These might include edge Rails or gems from your path or 10 | # Git. Remember to move these dependencies to your gemspec before releasing 11 | # your gem to rubygems.org. 12 | 13 | # To use debugger 14 | # gem 'debugger' 15 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | aws-healthcheck (2.0.0) 5 | railties (>= 3.0) 6 | 7 | GEM 8 | remote: https://rubygems.org/ 9 | specs: 10 | actionpack (6.0.3.4) 11 | actionview (= 6.0.3.4) 12 | activesupport (= 6.0.3.4) 13 | rack (~> 2.0, >= 2.0.8) 14 | rack-test (>= 0.6.3) 15 | rails-dom-testing (~> 2.0) 16 | rails-html-sanitizer (~> 1.0, >= 1.2.0) 17 | actionview (6.0.3.4) 18 | activesupport (= 6.0.3.4) 19 | builder (~> 3.1) 20 | erubi (~> 1.4) 21 | rails-dom-testing (~> 2.0) 22 | rails-html-sanitizer (~> 1.1, >= 1.2.0) 23 | activesupport (6.0.3.4) 24 | concurrent-ruby (~> 1.0, >= 1.0.2) 25 | i18n (>= 0.7, < 2) 26 | minitest (~> 5.1) 27 | tzinfo (~> 1.1) 28 | zeitwerk (~> 2.2, >= 2.2.2) 29 | builder (3.2.4) 30 | concurrent-ruby (1.1.7) 31 | crass (1.0.6) 32 | erubi (1.10.0) 33 | i18n (1.8.5) 34 | concurrent-ruby (~> 1.0) 35 | loofah (2.7.0) 36 | crass (~> 1.0.2) 37 | nokogiri (>= 1.5.9) 38 | method_source (1.0.0) 39 | mini_portile2 (2.4.0) 40 | minitest (5.14.2) 41 | nokogiri (1.10.10) 42 | mini_portile2 (~> 2.4.0) 43 | rack (2.2.3) 44 | rack-test (1.1.0) 45 | rack (>= 1.0, < 3) 46 | rails-dom-testing (2.0.3) 47 | activesupport (>= 4.2.0) 48 | nokogiri (>= 1.6) 49 | rails-html-sanitizer (1.3.0) 50 | loofah (~> 2.3) 51 | railties (6.0.3.4) 52 | actionpack (= 6.0.3.4) 53 | activesupport (= 6.0.3.4) 54 | method_source 55 | rake (>= 0.8.7) 56 | thor (>= 0.20.3, < 2.0) 57 | rake (13.0.1) 58 | thor (1.0.1) 59 | thread_safe (0.3.6) 60 | tzinfo (1.2.8) 61 | thread_safe (~> 0.1) 62 | zeitwerk (2.4.1) 63 | 64 | PLATFORMS 65 | ruby 66 | 67 | DEPENDENCIES 68 | aws-healthcheck! 69 | rack-test 70 | 71 | BUNDLED WITH 72 | 2.1.4 73 | -------------------------------------------------------------------------------- /MIT-LICENSE: -------------------------------------------------------------------------------- 1 | Copyright 2014 YOURNAME 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ### AWS Healthcheck 2 | 3 | This gem defines a route in your Rails app for ELB healthchecks. The healthcheck is a piece of Rack middleware that does absolutely nothing, so it is faster than just using the default "/" route. 4 | 5 | ```ruby 6 | gem 'aws-healthcheck' 7 | ``` 8 | 9 | Your Rails app now returns a 200 from `/healthcheck`. 10 | 11 | If `ENV['GIT_COMMIT']` is set, the response will also include that value in the body: 12 | 13 | ``` 14 | { "commit": "abcdef123456" } 15 | ``` 16 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | begin 2 | require 'bundler/setup' 3 | rescue LoadError 4 | puts 'You must `gem install bundler` and `bundle install` to run rake tasks' 5 | end 6 | 7 | APP_RAKEFILE = File.expand_path('../test/dummy/Rakefile', __FILE__) 8 | load 'rails/tasks/engine.rake' 9 | 10 | Bundler::GemHelper.install_tasks 11 | 12 | require 'rake/testtask' 13 | 14 | Rake::TestTask.new(:test) do |t| 15 | t.libs << 'lib' 16 | t.libs << 'test' 17 | t.pattern = 'test/**/*_test.rb' 18 | t.verbose = false 19 | end 20 | 21 | 22 | task default: :test 23 | -------------------------------------------------------------------------------- /bin/rails: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # This command will automatically be run when you run "rails" with Rails 4 gems installed from the root of your application. 3 | 4 | ENGINE_ROOT = File.expand_path('../..', __FILE__) 5 | ENGINE_PATH = File.expand_path('../../lib/healthcheck/engine', __FILE__) 6 | 7 | # Set up gems listed in the Gemfile. 8 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) 9 | require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE']) 10 | 11 | require 'rails/all' 12 | require 'rails/engine/commands' 13 | -------------------------------------------------------------------------------- /healthcheck.gemspec: -------------------------------------------------------------------------------- 1 | $LOAD_PATH.push File.expand_path('../lib', __FILE__) 2 | 3 | # Maintain your gem's version: 4 | require 'healthcheck/version' 5 | 6 | # Describe your gem and declare its dependencies: 7 | Gem::Specification.new do |s| 8 | s.name = 'aws-healthcheck' 9 | s.version = Healthcheck::VERSION 10 | s.authors = ['Logan Serman'] 11 | s.email = ['logan@serman.dev'] 12 | s.homepage = 'http://github.com/lserman/healthcheck' 13 | s.summary = 'Mounts a Rack app at /healthcheck that returns a 200 for AWS load balancers' 14 | s.description = s.summary 15 | s.license = 'MIT' 16 | 17 | s.files = Dir['{app,config,db,lib}/**/*', 'MIT-LICENSE', 'Rakefile', 'README.rdoc'] 18 | s.test_files = Dir['test/**/*'] 19 | 20 | s.add_dependency 'railties', '>= 3.0' 21 | s.add_development_dependency 'rack-test' 22 | end 23 | -------------------------------------------------------------------------------- /lib/aws-healthcheck.rb: -------------------------------------------------------------------------------- 1 | require 'healthcheck/middleware' 2 | require 'healthcheck/railtie' 3 | 4 | module Healthcheck 5 | end 6 | -------------------------------------------------------------------------------- /lib/healthcheck/middleware.rb: -------------------------------------------------------------------------------- 1 | module Healthcheck 2 | class Middleware 3 | def initialize(app) 4 | @app = app 5 | end 6 | 7 | def call(env) 8 | if env['PATH_INFO'] == '/healthcheck' 9 | [ 200, { 'Content-Type' => 'application/json' }, [ body.to_json ] ] 10 | else 11 | @app.call env 12 | end 13 | end 14 | 15 | private 16 | def body 17 | { commit: ENV['GIT_COMMIT'].to_s } 18 | end 19 | end 20 | end 21 | -------------------------------------------------------------------------------- /lib/healthcheck/railtie.rb: -------------------------------------------------------------------------------- 1 | require 'rails' 2 | 3 | module Healthcheck 4 | class Railtie < ::Rails::Railtie 5 | config.app_middleware.use Healthcheck::Middleware 6 | end 7 | end 8 | -------------------------------------------------------------------------------- /lib/healthcheck/version.rb: -------------------------------------------------------------------------------- 1 | module Healthcheck 2 | VERSION = '2.0.0' 3 | end 4 | -------------------------------------------------------------------------------- /test/healthcheck_test.rb: -------------------------------------------------------------------------------- 1 | require 'minitest/autorun' 2 | require 'rack/test' 3 | require 'rack/lobster' 4 | require 'aws-healthcheck' 5 | 6 | class HealthcheckTest < Minitest::Test 7 | include Rack::Test::Methods 8 | 9 | def app 10 | Rack::Builder.app(Rack::Lobster.new) do 11 | use Healthcheck::Middleware 12 | end 13 | end 14 | 15 | def test_returns_200 16 | get '/healthcheck' 17 | assert_equal 200, last_response.status 18 | end 19 | 20 | def test_returns_blank_commit_if_GIT_COMMIT_env_is_not_set 21 | ENV['GIT_COMMIT'] = nil 22 | get '/healthcheck' 23 | json = JSON.parse last_response.body 24 | assert_equal '', json['commit'] 25 | end 26 | 27 | def test_returns_GIT_COMMIT_env_value_if_set 28 | ENV['GIT_COMMIT'] = 'test1234' 29 | get '/healthcheck' 30 | json = JSON.parse last_response.body 31 | assert_equal 'test1234', json['commit'] 32 | end 33 | end 34 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | # Configure Rails Environment 2 | ENV['RAILS_ENV'] = 'test' 3 | 4 | require File.expand_path('../dummy/config/environment.rb', __FILE__) 5 | require 'rails/test_help' 6 | 7 | Rails.backtrace_cleaner.remove_silencers! 8 | 9 | # Load support files 10 | Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f } 11 | 12 | # Load fixtures from the engine 13 | if ActiveSupport::TestCase.method_defined?(:fixture_path=) 14 | ActiveSupport::TestCase.fixture_path = File.expand_path('../fixtures', __FILE__) 15 | end 16 | --------------------------------------------------------------------------------