Skip to content

Commit

Permalink
harmonized url building for notification and email messages
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
PiTrem committed Sep 23, 2022
1 parent 5f66840 commit 9a6c244
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 38 deletions.
9 changes: 3 additions & 6 deletions app/jobs/download_analyses_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -88,4 +84,5 @@ def perform(sid, user_id, rt=true)
end
zip
end
end
end

6 changes: 1 addition & 5 deletions app/jobs/export_collections_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
30 changes: 25 additions & 5 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 1 addition & 5 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
17 changes: 1 addition & 16 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion config/environments/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
26 changes: 26 additions & 0 deletions config/initializers/mail.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 9a6c244

Please sign in to comment.