Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Add S3 and Sentry support to server app / docker images (#13703)

Damian Legawiecd40d1fa00325
diff --git a/server/Gemfile.common b/server/Gemfile.common
index 130a42910f..f233e398ad 100644
--- a/server/Gemfile.common
+++ b/server/Gemfile.common
@@ -36,4 +36,13 @@ gem "thruster", require: false
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
gem "image_processing", "~> 1.2"
+# Authentication
gem "devise"
+
+# AWS S3 / Cloudflare R2 storage
+gem "aws-sdk-s3", require: false
+
+# Sentry for error/performance monitoring
+gem 'sentry-ruby'
+gem 'sentry-rails'
+gem 'sentry-sidekiq'
diff --git a/server/Gemfile.lock b/server/Gemfile.lock
index c8c074abde..a3f8c3267f 100644
--- a/server/Gemfile.lock
+++ b/server/Gemfile.lock
@@ -209,6 +209,25 @@ GEM
awesome_nested_set (3.9.0)
activerecord (>= 4.0.0, < 8.2)
awesome_print (1.9.2)
+ aws-eventstream (1.4.0)
+ aws-partitions (1.1220.0)
+ aws-sdk-core (3.242.0)
+ aws-eventstream (~> 1, >= 1.3.0)
+ aws-partitions (~> 1, >= 1.992.0)
+ aws-sigv4 (~> 1.9)
+ base64
+ bigdecimal
+ jmespath (~> 1, >= 1.6.1)
+ logger
+ aws-sdk-kms (1.122.0)
+ aws-sdk-core (~> 3, >= 3.241.4)
+ aws-sigv4 (~> 1.5)
+ aws-sdk-s3 (1.213.0)
+ aws-sdk-core (~> 3, >= 3.241.4)
+ aws-sdk-kms (~> 1)
+ aws-sigv4 (~> 1.5)
+ aws-sigv4 (1.12.1)
+ aws-eventstream (~> 1, >= 1.0.2)
base64 (0.3.0)
bcrypt (3.1.21)
benchmark (0.5.0)
@@ -362,6 +381,7 @@ GEM
prism (>= 1.3.0)
rdoc (>= 4.0.0)
reline (>= 0.4.2)
+ jmespath (1.6.2)
json (2.18.1)
json-schema (6.1.0)
addressable (~> 2.8)
@@ -667,6 +687,16 @@ GEM
rexml (~> 3.2, >= 3.2.5)
rubyzip (>= 1.2.2, < 3.0)
websocket (~> 1.0)
+ sentry-rails (6.4.1)
+ railties (>= 5.2.0)
+ sentry-ruby (~> 6.4.1)
+ sentry-ruby (6.4.1)
+ bigdecimal
+ concurrent-ruby (~> 1.0, >= 1.0.2)
+ logger
+ sentry-sidekiq (6.4.1)
+ sentry-ruby (~> 6.4.1)
+ sidekiq (>= 5.0)
sidekiq (8.1.1)
connection_pool (>= 3.0.0)
json (>= 2.16.0)
@@ -812,6 +842,7 @@ PLATFORMS
x86_64-linux-musl
DEPENDENCIES
+ aws-sdk-s3
bootsnap
brakeman
bundler-audit
@@ -827,6 +858,9 @@ DEPENDENCIES
rails (~> 8.1.2)
redis
rubocop-rails-omakase
+ sentry-rails
+ sentry-ruby
+ sentry-sidekiq
sidekiq
simplecov-cobertura
spree!
@@ -870,6 +904,12 @@ CHECKSUMS
async-pool (0.11.1) sha256=98e1583e199a75f7dc70f8e65fc8d0d3b28636c3f256595d43e206642ad8fbda
awesome_nested_set (3.9.0) sha256=3ce99e816550f97f4de118e621630070aacf24928b920fe4a68846578a8daaed
awesome_print (1.9.2) sha256=e99b32b704acff16d768b3468680793ced40bfdc4537eb07e06a4be11133786e
+ aws-eventstream (1.4.0) sha256=116bf85c436200d1060811e6f5d2d40c88f65448f2125bc77ffce5121e6e183b
+ aws-partitions (1.1220.0) sha256=1567da9ae45cba28e1d31f5e996928b2eb92ad01700000846d6d90043be8670f
+ aws-sdk-core (3.242.0) sha256=c17b3003acc78d80c1a8437b285a1cfc5e4d7749ce7821cf3071e847535a29a0
+ aws-sdk-kms (1.122.0) sha256=47ce3f51b26bd7d76f1270cfdfca17b40073ecd3219c8c9400788712abfb4eb8
+ aws-sdk-s3 (1.213.0) sha256=af596ccf544582406db610e95cc9099276eaf03142f57a2f30f76940e598e50d
+ aws-sigv4 (1.12.1) sha256=6973ff95cb0fd0dc58ba26e90e9510a2219525d07620c8babeb70ef831826c00
base64 (0.3.0) sha256=27337aeabad6ffae05c265c450490628ef3ebd4b67be58257393227588f5a97b
bcrypt (3.1.21) sha256=5964613d750a42c7ee5dc61f7b9336fb6caca429ba4ac9f2011609946e4a2dcf
benchmark (0.5.0) sha256=465df122341aedcb81a2a24b4d3bd19b6c67c1530713fd533f3ff034e419236c
@@ -942,6 +982,7 @@ CHECKSUMS
io-event (1.14.2) sha256=b0a069190eafe86005c22f7464f744971b5bd82f153740d34e6ab49548d4f613
io-stream (0.11.1) sha256=fa5f551fcff99581c1757b9d1cee2c37b124f07d2ca4f40b756a05ab9bd21b87
irb (1.17.0) sha256=168c4ddb93d8a361a045c41d92b2952c7a118fa73f23fe14e55609eb7a863aae
+ jmespath (1.6.2) sha256=238d774a58723d6c090494c8879b5e9918c19485f7e840f2c1c7532cf84ebcb1
json (2.18.1) sha256=fe112755501b8d0466b5ada6cf50c8c3f41e897fa128ac5d263ec09eedc9f986
json-schema (6.1.0) sha256=6bf70a2cfb6dfd5a06da28093fa8190f324c88eabd36a7f47097f227321dc702
jsonapi-rspec (0.0.11) sha256=995f1adb59b386cc36e3f6823c828df575167586937e3e42601914c4ee650082
@@ -1067,6 +1108,9 @@ CHECKSUMS
sawyer (0.9.3) sha256=0d0f19298408047037638639fe62f4794483fb04320269169bd41af2bdcf5e41
securerandom (0.4.1) sha256=cc5193d414a4341b6e225f0cb4446aceca8e50d5e1888743fac16987638ea0b1
selenium-webdriver (4.10.0) sha256=a322af38f9a8966a6c3ab85c365b08c5556cdef72f577905b2b42469faee9d43
+ sentry-rails (6.4.1) sha256=fb800a375533f2fff3101f26a1a32bdce0a4b99b16cd5be7b64b15e93bcb58e8
+ sentry-ruby (6.4.1) sha256=dac04976f791ad6ecd4fd30440c29d9b73aee08f790eeca73b439b5d67370f38
+ sentry-sidekiq (6.4.1) sha256=214082c26487cf2df315d41f45001584ebb8039c77f5a07090284b0deff934df
sidekiq (8.1.1) sha256=afdf2736883e1e80c1c9b7c47afe0387e1a4997c87170978a45560d35d429151
simple_po_parser (1.1.6) sha256=122687d44d3de516a0e69e2f383a4180f5015e8c5ed5a7f2258f2b376f64cbf3
simplecov (0.22.0) sha256=fe2622c7834ff23b98066bb0a854284b2729a569ac659f82621fc22ef36213a5
diff --git a/server/config/environments/production.rb b/server/config/environments/production.rb
index ec40accc04..53f2fbc749 100644
--- a/server/config/environments/production.rb
+++ b/server/config/environments/production.rb
@@ -22,7 +22,13 @@ Rails.application.configure do
# config.asset_host = "http://assets.example.com"
# Store uploaded files on the local file system (see config/storage.yml for options).
- config.active_storage.service = :local
+ if ENV["AWS_ACCESS_KEY_ID"].present? && ENV["AWS_SECRET_ACCESS_KEY"].present?
+ config.active_storage.service = :amazon
+ elsif ENV["CLOUDFLARE_ACCESS_KEY_ID"].present? && ENV["CLOUDFLARE_ACCESS_SECRET"].present? && ENV["CLOUDFLARE_ENDPOINT"].present?
+ config.active_storage.service = :cloudflare
+ else
+ config.active_storage.service = :local
+ end
# Assume all access to the app is happening through a SSL-terminating reverse proxy.
config.assume_ssl = ENV["RAILS_ASSUME_SSL"] != "false"
diff --git a/server/config/initializers/sentry.rb b/server/config/initializers/sentry.rb
new file mode 100644
index 0000000000..33b34d62af
--- /dev/null
+++ b/server/config/initializers/sentry.rb
@@ -0,0 +1,30 @@
+if ENV['SENTRY_DSN'].present?
+ Sentry.init do |config|
+ config.dsn = ENV['SENTRY_DSN']
+ config.breadcrumbs_logger = %i[active_support_logger http_logger]
+
+ # Set tracesSampleRate to 1.0 to capture 100%
+ # of transactions for performance monitoring.
+ # We recommend adjusting this value in production
+ config.traces_sample_rate = 0.5
+
+ config.enabled_environments = %w[production staging]
+ config.enabled_environments << 'development' if ENV['SENTRY_REPORT_ON_DEVELOPMENT'].present?
+
+ config.release = "spree@#{ENV['RENDER_GIT_COMMIT']}" if ENV['RENDER_GIT_COMMIT'].present?
+
+ config.excluded_exceptions = [
+ 'ActionController::RoutingError',
+ 'ActiveRecord::RecordNotFound',
+ 'Sidekiq::JobRetry::Skip',
+ 'Sidekiq::JobRetry::SilentRetry',
+ 'Aws::S3::Errors::NoSuchKey',
+ 'Aws::S3::Errors::NotFound',
+ 'ActiveStorage::FileNotFoundError'
+ ]
+
+ # Use native Rails error subscriber
+ # https://guides.rubyonrails.org/error_reporting.html
+ config.rails.register_error_subscriber = true
+ end
+end
diff --git a/server/config/storage.yml b/server/config/storage.yml
index 927dc537c8..181170fb7e 100644
--- a/server/config/storage.yml
+++ b/server/config/storage.yml
@@ -6,14 +6,22 @@ local:
service: Disk
root: <%= Rails.root.join("storage") %>
-# Use bin/rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key)
-# amazon:
-# service: S3
-# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %>
-# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %>
-# region: us-east-1
-# bucket: your_own_bucket-<%= Rails.env %>
+amazon:
+ service: S3
+ access_key_id: <%= ENV.fetch("AWS_ACCESS_KEY_ID", "") %>
+ secret_access_key: <%= ENV.fetch("AWS_SECRET_ACCESS_KEY", "") %>
+ region: <%= ENV.fetch("AWS_REGION", "") %>
+ bucket: <%= ENV.fetch("AWS_BUCKET", "spree-#{Rails.env}") %>
+cloudflare:
+ service: S3
+ endpoint: <%= ENV.fetch("CLOUDFLARE_ENDPOINT", "") %>
+ access_key_id: <%= ENV.fetch("CLOUDFLARE_ACCESS_KEY_ID", "") %>
+ secret_access_key: <%= ENV.fetch("CLOUDFLARE_SECRET_ACCESS_KEY", "") %>
+ region: auto
+ bucket: <%= ENV.fetch("CLOUDFLARE_BUCKET", "spree-#{Rails.env}") %>
+ request_checksum_calculation: "when_required"
+ response_checksum_validation: "when_required"
# Remember not to checkin your GCS keyfile to a repository
# google:
# service: GCS