Skip to content

Instantly share code, notes, and snippets.

@mvastola
Last active July 19, 2022 06:43
Show Gist options
  • Save mvastola/6b4ddae52f049ca19231f471cb7de96c to your computer and use it in GitHub Desktop.
Save mvastola/6b4ddae52f049ca19231f471cb7de96c to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
ENV['RAILS_ENV'] = 'development'
require 'rubygems'
require 'bundler/inline'
gemfile(false) do
source 'https://rubygems.org'
gem 'puma'
gem 'excon'
gem 'rails'
group :development, optional: true do
gem 'ruby-debug-ide'
gem 'debase'
end
end
require 'pp'
require 'rails'
require 'action_controller/railtie'
SERVER_PATH = Pathname.new(Dir.mktmpdir('puma'))
at_exit do
SERVER_PATH.rmtree
rescue Errno::ENOENT
nil
end
Rails::Application.redefine_singleton_method(:find_root) { |*args| SERVER_PATH }
STDERR.sync = true
class MiniApp < Rails::Application
config.colorize_logging = true
config.logger = begin
logger = ActiveSupport::Logger.new(STDERR)
logger.formatter = config.log_formatter
ActiveSupport::TaggedLogging.new(logger)
end
config.eager_load = false
config.cache_classes = true
config.cache = ActiveSupport::Cache::NullStore.new
config.api_only = true
middleware.delete ActionDispatch::HostAuthorization
middleware.delete Rack::Sendfile
middleware.delete ActionDispatch::ShowExceptions
config.log_tags = [
->(_) { Time.now.rfc3339 },
:uuid,
->(req) { "#{req.method} #{req.path}" }
]
end
Rails.application.initialize!
Rails.application.routes.draw do
root "main#show"
end
class ApplicationController < ActionController::API; end
class FakeError < StandardError; end
class MainController < ApplicationController
before_action :raise_error
prepend_around_action :log
rescue_from FakeError do |e|
e.backtrace.slice! 1..nil # Hide backtraces to reduce noise
render plain: e.full_message, status: :internal_server_error
raise e
end
def show
render plain: 'success'
end
def log
logger.info 'Yielding from #log `around_action`'
yield
logger.info 'Returned from `yield` in #log `around_action`'
ensure
logger.info 'Entered `ensure` section in #log `around_action`'
logger.info '**** IF THIS WERE A REAL APP, REQUEST/RESPONSE LOGGING WOULD HAPPEN HERE ****'
logger.info 'Wrote session to log #log `around_action`'
end
def raise_error
logger.info 'In #raise_error; about to `raise`'
raise FakeError, 'All your base are belong to us'
end
end
SOCKET_PATH = SERVER_PATH / 'server.sock'
require 'puma/server'
@server_config = Puma::Configuration.new do |c, *|
c.bind "unix://#{SOCKET_PATH.to_s}"
c.app MiniApp
c.silence_single_worker_warning
c.workers 1
c.threads 1, 1
c.environment 'development'
end.tap(&:load)
Thread.abort_on_exception = true
@launcher_events = Puma::Events.stdio
@launcher_events.on_booted { make_test_request }
@launcher = Puma::Launcher.new(@server_config, events: @launcher_events)
@launcher_thread = Thread.new(@launcher, &:run).tap(&:run)
def make_test_request
resp = Excon.get('unix:///', socket: SOCKET_PATH.to_s)
Rails.logger.info "Response: Status #{resp.status} (#{Rack::Utils::HTTP_STATUS_CODES[resp.status]})"
header_txt = resp.headers.map { |k,v| "#{k}: #{v}" }.join("\n")
Rails.logger.info "Response Headers:\n#{header_txt}"
Rails.logger.info "Response Body: \n#####\n#{resp.body}\n#####"
@launcher.stop
resp
end
@launcher_thread.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment