From 9a6c2441a52356989aa69da67f8283a27a279ff6 Mon Sep 17 00:00:00 2001 From: PiTrem Date: Fri, 16 Sep 2022 14:04:42 +0200 Subject: [PATCH] harmonized url building for notification and email messages * introduce new env variable PUBLIC_URL (uri) * warning: deprecate SMTP_HOST variable to build url in email body * dry email configuration * email delivery disabled if SMTP_ADDRESS is set or if DISABLE_MAIL_DELIVERY * use app.root_url to build url in notification * NB: PUBLIC_URL: is intended to be used in container environments to point to the public web entrypoint of the Chemotion instance --- app/jobs/download_analyses_job.rb | 9 +++------ app/jobs/export_collections_job.rb | 6 +----- config/application.rb | 30 +++++++++++++++++++++++++----- config/environments/development.rb | 6 +----- config/environments/production.rb | 17 +---------------- config/environments/test.rb | 2 +- config/initializers/mail.rb | 26 ++++++++++++++++++++++++++ 7 files changed, 58 insertions(+), 38 deletions(-) create mode 100644 config/initializers/mail.rb diff --git a/app/jobs/download_analyses_job.rb b/app/jobs/download_analyses_job.rb index ec87ff8893..1a18be9051 100644 --- a/app/jobs/download_analyses_job.rb +++ b/app/jobs/download_analyses_job.rb @@ -38,11 +38,7 @@ def perform(sid, user_id, rt=true) @file_path = Rails.public_path.join('zip', @filename) begin - @link = if Rails.env.production? - "https://#{ENV['HOST'] || ENV['SMTP_DOMAIN']}/zip/#{@filename}" - else - "http://#{ENV['HOST'] || 'localhost:3000'}/zip/#{@filename}" - end + @link = "#{app.root_url}/zip/#{@filename}" @expires_at = Time.now + 24.hours zip = Zip::OutputStream.write_buffer do |zip| @@ -88,4 +84,5 @@ def perform(sid, user_id, rt=true) end zip end -end \ No newline at end of file +end + diff --git a/app/jobs/export_collections_job.rb b/app/jobs/export_collections_job.rb index 1568481032..910e3bb614 100644 --- a/app/jobs/export_collections_job.rb +++ b/app/jobs/export_collections_job.rb @@ -36,11 +36,7 @@ def perform(collection_ids, extname, nested, user_id) @user_id = user_id begin @labels = Collection.where(id: collection_ids[0..9]).pluck(:label) - @link = if Rails.env.production? - "https://#{ENV['HOST'] || ENV['SMTP_DOMAIN']}/zip/#{job_id}.#{extname}" - else - "http://#{ENV['HOST'] || 'localhost:3000'}/zip/#{job_id}.#{extname}" - end + @link = "#{app.root_url}/zip/#{job_id}.#{extname}" @expires_at = Time.now + 24.hours export = Export::ExportCollections.new(job_id, collection_ids, extname, nested) diff --git a/config/application.rb b/config/application.rb index 778f6a7972..1f2f75bc3e 100644 --- a/config/application.rb +++ b/config/application.rb @@ -35,14 +35,34 @@ class Application < Rails::Application # -- all .rb files in that directory are automatically loaded after loading # the framework and any gems in your application. # - + # + if ENV['SMTP_HOST'].present? + message = <<~MESSAGE + ######################################################################### + The use of SMTP_HOST has been deprecated. + In your .env file, use the variable PUBLIC_URL with a proper uri instead. + (eg PUBLIC_URL=http://my.eln.edu) + ######################################################################### + MESSAGE + puts message + end + + uri = URI.parse(ENV['PUBLIC_URL'] || 'http://localhost:3000') + scheme = uri.scheme || 'http' + host = uri.host || 'localhost' + port = uri.port + routes do + default_url_options(host: host, protocol: scheme, port: port) + end + config.root_url = uri&.to_s + # tmp assets fix sprite_file = Rails.public_path.join('sprite.png') - sprite_source = Rails.public_path.join('assets', 'ketcherails','sprite*.png' ) - new_sprite = Dir.glob(sprite_source).max_by{ |f| File.mtime(f)} + sprite_source = Rails.public_path.join('assets', 'ketcherails', 'sprite*.png') + new_sprite = Dir.glob(sprite_source).max_by { |f| File.mtime(f) } if new_sprite.present? - FileUtils.rm(sprite_file) if File.exist?(sprite_file) - FileUtils.ln_s(new_sprite, sprite_file) + FileUtils.rm(sprite_file) if File.file?(sprite_file) + FileUtils.ln_s(new_sprite, sprite_file) end end end diff --git a/config/environments/development.rb b/config/environments/development.rb index 66ac8712a4..b49f81a719 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -33,7 +33,7 @@ # Don't care if the mailer can't send. config.action_mailer.raise_delivery_errors = false - config.action_mailer.perform_caching = false + # config.action_mailer.perform_caching = false # Print deprecation notices to the Rails logger. config.active_support.deprecation = :log @@ -55,10 +55,6 @@ # Raises error for missing translations # config.action_view.raise_on_missing_translations = true - config.action_mailer.delivery_method = :smtp - config.action_mailer.smtp_settings = { :address => "localhost", :port => 1025 } - #config.action_mailer.default_url_options = { host: 'localhost', port: 3000 } - # TurboSprockets.configure do |config| # config.precompiler.enabled = false # config.preloader.enabled = false diff --git a/config/environments/production.rb b/config/environments/production.rb index 1a977d7e44..af2a51402c 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -78,7 +78,7 @@ # config.active_job.queue_adapter = :resque # config.active_job.queue_name_prefix = "chemotion_#{Rails.env}" - config.action_mailer.perform_caching = false + # config.action_mailer.perform_caching = false # Ignore bad email addresses and do not raise email delivery errors. # Set this to true and configure the email server for immediate delivery to raise delivery errors. @@ -106,19 +106,4 @@ # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false - - config.action_mailer.raise_delivery_errors = true - config.action_mailer.delivery_method = :smtp - config.action_mailer.default_url_options = { host: ENV['SMTP_HOST']} - config.action_mailer.smtp_settings = { - :address => ENV["SMTP_ADDRESS"], - :port => ENV["SMTP_PORT"], - :user_name => ENV['SMTP_USERNAME'], - :domain => ENV['SMTP_DOMAIN'], - :password => ENV['SMTP_PASSWORD'], - :authentication => ENV['SMTP_AUTH'] && ENV['SMTP_AUTH'].to_sym, - :enable_starttls_auto => ENV['SMTP_TLS'] && ENV['SMTP_TLS'].match(/true/), - :openssl_verify_mode => ENV['SMTP_SSL_MODE'] - } - end diff --git a/config/environments/test.rb b/config/environments/test.rb index 62e6e20f28..20fc6222dd 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -31,7 +31,7 @@ # Store uploaded files on the local file system in a temporary directory # config.active_storage.service = :test - config.action_mailer.perform_caching = false + # config.action_mailer.perform_caching = false # Tell Action Mailer not to deliver emails to the real world. # The :test delivery method accumulates sent emails in the diff --git a/config/initializers/mail.rb b/config/initializers/mail.rb new file mode 100644 index 0000000000..00f5c77014 --- /dev/null +++ b/config/initializers/mail.rb @@ -0,0 +1,26 @@ +Rails.application.configure do + uri = URI.parse(config.root_url) + scheme = uri.scheme + host = uri.host + port = uri.port + + config.action_mailer.raise_delivery_errors = true if Rails.env.production? + config.action_mailer.perform_caching = false + config.action_mailer.default_url_options = { host: host, protocol: scheme, port: port } + config.action_mailer.delivery_method = :smtp unless Rails.env.test? + config.action_mailer.smtp_settings = { + address: ENV['SMTP_ADDRESS'], + port: ENV['SMTP_PORT'], + user_name: ENV['SMTP_USERNAME'], + domain: ENV['SMTP_DOMAIN'], + password: ENV['SMTP_PASSWORD'], + authentication: ENV['SMTP_AUTH'] && ENV['SMTP_AUTH'].to_sym, + enable_starttls_auto: ENV['SMTP_TLS'] && ENV['SMTP_TLS'].match(/true/), + openssl_verify_mode: ENV['SMTP_SSL_MODE'] + } + + if ENV['SMTP_ADDRESS'].blank? || ENV['DISABLE_MAIL_DELIVERY'].present? + config.action_mailer.perform_deliveries = false + end +end +