Skip to content

Instantly share code, notes, and snippets.

@rosswilson
Created January 12, 2020 01:48
Show Gist options
  • Save rosswilson/3d477e21b44ff22dae472264cb514595 to your computer and use it in GitHub Desktop.
Save rosswilson/3d477e21b44ff22dae472264cb514595 to your computer and use it in GitHub Desktop.
Rails response header containing git revision sha
# config/application.rb
require_relative 'boot'
require 'rails/all'
require_relative '../lib/middleware/revision_middleware'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module MyApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 6.0
# Set a response header containing the git revision
config.middleware.use RevisionMiddleware
# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.
end
end
# lib/revision_middleware.rb
class RevisionMiddleware
def initialize(app)
@app = app
@revision = ENV["GIT_REV"] || "unknown"
end
def call(env)
status, headers, body = @app.call(env)
headers["X-Revision"] = @revision
[status, headers, body]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment