From b859eebff8545eea972d479137e14b917e6519dc Mon Sep 17 00:00:00 2001 From: George Claghorn Date: Mon, 5 Nov 2018 12:39:37 -0500 Subject: Only load the AWS SDK when the Amazon ingress is configured --- app/controllers/action_mailbox/base_controller.rb | 4 ++++ .../ingresses/amazon/inbound_emails_controller.rb | 11 ++++++++--- lib/action_mailbox/engine.rb | 13 ++++++++++++- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/controllers/action_mailbox/base_controller.rb b/app/controllers/action_mailbox/base_controller.rb index a2f7eb4b61..c234ecd250 100644 --- a/app/controllers/action_mailbox/base_controller.rb +++ b/app/controllers/action_mailbox/base_controller.rb @@ -1,6 +1,10 @@ class ActionMailbox::BaseController < ActionController::Base skip_forgery_protection + def self.prepare + # Override in concrete controllers to run code on load. + end + before_action :ensure_configured private diff --git a/app/controllers/action_mailbox/ingresses/amazon/inbound_emails_controller.rb b/app/controllers/action_mailbox/ingresses/amazon/inbound_emails_controller.rb index 4d56e27c76..d3998be2d4 100644 --- a/app/controllers/action_mailbox/ingresses/amazon/inbound_emails_controller.rb +++ b/app/controllers/action_mailbox/ingresses/amazon/inbound_emails_controller.rb @@ -1,9 +1,14 @@ class ActionMailbox::Ingresses::Amazon::InboundEmailsController < ActionMailbox::BaseController before_action :authenticate - # TODO: Lazy-load the AWS SDK - require "aws-sdk-sns/message_verifier" - cattr_accessor :verifier, default: Aws::SNS::MessageVerifier.new + cattr_accessor :verifier + + def self.prepare + self.verifier ||= begin + require "aws-sdk-sns/message_verifier" + Aws::SNS::MessageVerifier.new + end + end def create ActionMailbox::InboundEmail.create_and_extract_message_id! params.require(:content) diff --git a/lib/action_mailbox/engine.rb b/lib/action_mailbox/engine.rb index 6a0312c65f..cf438d8f24 100644 --- a/lib/action_mailbox/engine.rb +++ b/lib/action_mailbox/engine.rb @@ -10,10 +10,21 @@ module ActionMailbox initializer "action_mailbox.config" do config.after_initialize do |app| - ActionMailbox.ingress = app.config.action_mailbox.ingress ActionMailbox.logger = app.config.action_mailbox.logger || Rails.logger ActionMailbox.incinerate_after = app.config.action_mailbox.incinerate_after || 30.days end end + + initializer "action_mailbox.ingress" do + config.after_initialize do |app| + if ActionMailbox.ingress = app.config.action_mailbox.ingress.presence + config.to_prepare do + if ingress_controller_class = "ActionMailbox::Ingresses::#{ActionMailbox.ingress.to_s.classify}::InboundEmailsController".safe_constantize + ingress_controller_class.prepare + end + end + end + end + end end end -- cgit v1.2.3