Skip to content

Commit f2385db

Browse files
authored
Merge pull request #33 from renuo/feature/24939-rails-active-storage-example
Rails Active Storage example app
2 parents 286df72 + c7819be commit f2385db

83 files changed

Lines changed: 2468 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎README.md‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,56 @@ nctl create application rails \
2525
--env=SECRET_KEY_BASE=$(rails secret)
2626
```
2727

28+
### Ruby On Rails with ActiveStorage
29+
30+
Next to the basic Rails example, we also provide a Rails application that has ActiveStorage configured with
31+
Deploio Object Storage.
32+
33+
Once deployed, you can head to the root page of the app and test the upload functionality. The example app supports
34+
direct uploads from the browser (CORS configuration is required as below) and custom bucket hostnames
35+
(DNS change required, see below).
36+
37+
```bash
38+
# setup postgres database
39+
nctl create postgresdatabase rails-active-storage-db --location=nine-es34 --wait
40+
DATABASE_URL="$(nctl get postgresdatabase rails-active-storage-db --print-connection-string)"
41+
42+
# setup application
43+
nctl create application rails-active-storage \
44+
--git-url=https://github.com/ninech/deploio-examples \
45+
--git-sub-path=ruby/rails-active-storage \
46+
--env=SECRET_KEY_BASE=$(rails secret)\;DATABASE_URL=${DATABASE_URL}
47+
48+
# create bucket
49+
nctl create bucket --location=nine-es34 rails-active-storage-bucket
50+
51+
# create bucket user
52+
nctl create bucketuser --location=nine-es34 rails-active-storage-bucketuser
53+
54+
# grant bucket user read & write access to bucket
55+
nctl update bucket rails-active-storage-bucket --permissions reader=rails-active-storage-bucketuser \
56+
--permissions writer=rails-active-storage-bucketuser
57+
58+
# update application with object storage credentials
59+
ACCESS_KEY="$(nctl get bucketuser rails-active-storage-bucketuser --print-credentials -o json | jq -r .s3_access_key)"
60+
SECRET_KEY="$(nctl get bucketuser rails-active-storage-bucketuser --print-credentials -o json | jq -r .s3_secret_key)"
61+
nctl update app rails-active-storage --env="S3_ACCESS_KEY=${ACCESS_KEY};S3_SECRET_KEY=${SECRET_KEY};S3_ENDPOINT=https://es34.objects.nineapis.ch;S3_BUCKET=rails-active-storage-bucket"
62+
63+
# optional: custom bucket hostnames
64+
nctl update bucket rails-active-storage-bucket --custom-hostnames={S3_BUCKET_HOST}
65+
nctl update app rails-active-storage --env="S3_BUCKET_HOST={S3_BUCKET_HOST}"
66+
nctl get bucket rails-active-storage-bucket --output="yaml" # setup DNS
67+
68+
# CORS configuration for direct uploads from browser
69+
# see https://guides.rubyonrails.org/active_storage_overview.html#cross-origin-resource-sharing-cors-configuration
70+
APP_HOST=$(nctl get app rails-active-storage -o json | jq -r ".status.atProvider.defaultURLs | first")
71+
nctl update bucket rails-active-storage-bucket \
72+
--cors origins=${APP_HOST} \
73+
--cors allowed-headers=Content-Type,Content-MD5,Content-Disposition \
74+
--cors response-headers=Content-Type,Content-MD5,Content-Disposition,ETag \
75+
--cors max-age=3600
76+
```
77+
2878
## Ruby - Sinatra on Falcon
2979

3080
```bash
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
deployJob:
2+
name: db-migrations
3+
command: bundle exec rails db:migrate
4+
retries: 0
5+
timeout: 5m
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
S3_ACCESS_KEY=
2+
S3_SECRET_KEY=
3+
S3_ENDPOINT=
4+
S3_BUCKET=
5+
S3_BUCKET_HOST=
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# See https://git-scm.com/docs/gitattributes for more about git attribute files.
2+
3+
# Mark the database schema as having been generated.
4+
db/schema.rb linguist-generated
5+
6+
# Mark any vendored files as having been vendored.
7+
vendor/* linguist-vendored
8+
config/credentials/*.yml.enc diff=rails_credentials
9+
config/credentials.yml.enc diff=rails_credentials
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
2+
#
3+
# Temporary files generated by your text editor or operating system
4+
# belong in git's global ignore instead:
5+
# `$XDG_CONFIG_HOME/git/ignore` or `~/.config/git/ignore`
6+
7+
# Ignore bundler config.
8+
/.bundle
9+
10+
# Ignore all environment files.
11+
/.env*
12+
!.env.example
13+
14+
# Ignore all logfiles and tempfiles.
15+
/log/*
16+
/tmp/*
17+
!/log/.keep
18+
!/tmp/.keep
19+
20+
# Ignore pidfiles, but keep the directory.
21+
/tmp/pids/*
22+
!/tmp/pids/
23+
!/tmp/pids/.keep
24+
25+
# Ignore storage (uploaded files in development and any SQLite databases).
26+
/storage/*
27+
!/storage/.keep
28+
/tmp/storage/*
29+
!/tmp/storage/
30+
!/tmp/storage/.keep
31+
32+
/public/assets
33+
34+
# Ignore key files for decrypting credentials and more.
35+
/config/*.key
36+

‎ruby/rails-active-storage/.rspec‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
--require spec_helper
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Omakase Ruby styling for Rails
2+
inherit_gem:
3+
rubocop-rails-omakase: rubocop.yml
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4.0.1

‎ruby/rails-active-storage/Gemfile‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
source "https://rubygems.org"
2+
3+
# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
4+
gem "rails", "~> 8.1.2"
5+
# The modern asset pipeline for Rails [https://github.com/rails/propshaft]
6+
gem "propshaft"
7+
# Use postgresql as the database for Active Record
8+
gem "pg", "~> 1.1"
9+
# Use the Puma web server [https://github.com/puma/puma]
10+
gem "puma", ">= 5.0"
11+
12+
gem "aws-sdk-s3"
13+
14+
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
15+
gem "tzinfo-data", platforms: %i[ windows jruby ]
16+
17+
group :development, :test do
18+
gem "rspec-rails"
19+
gem "dotenv"
20+
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
21+
gem "debug", platforms: %i[ mri windows ], require: "debug/prelude"
22+
end

0 commit comments

Comments
 (0)