Skip to content

Commit

Permalink
Example Rails application to showcase how to use the client
Browse files Browse the repository at this point in the history
  • Loading branch information
Envek committed Jun 20, 2023
1 parent 69dc397 commit e16bfaf
Show file tree
Hide file tree
Showing 61 changed files with 1,467 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/rails_app/.dockerdev/.bashrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
alias be="bundle exec"
3 changes: 3 additions & 0 deletions examples/rails_app/.dockerdev/Aptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# An editor to work with credentials
vim

67 changes: 67 additions & 0 deletions examples/rails_app/.dockerdev/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# syntax=docker/dockerfile:1

ARG RUBY_VERSION
ARG DISTRO_NAME=bullseye

FROM ruby:$RUBY_VERSION-slim-$DISTRO_NAME

ARG DISTRO_NAME

# Common dependencies
# Using --mount to speed up build with caching, see https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/reference.md#run---mount
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
rm -f /etc/apt/apt.conf.d/docker-clean; \
echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' > /etc/apt/apt.conf.d/keep-cache; \
apt-get update -qq && \
DEBIAN_FRONTEND=noninteractive apt-get -yq dist-upgrade && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
build-essential \
gnupg2 \
curl \
less \
git

ARG NODE_MAJOR
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
curl -sL https://deb.nodesource.com/setup_$NODE_MAJOR.x | bash - && \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
nodejs

RUN npm install -g yarn

# Application dependencies
# We use an external Aptfile for this, stay tuned
RUN --mount=type=cache,target=/var/cache/apt,sharing=locked \
--mount=type=cache,target=/var/lib/apt,sharing=locked \
--mount=type=tmpfs,target=/var/log \
--mount=type=bind,source=Aptfile,target=/tmp/Aptfile \
DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends \
$(grep -Ev '^\s*#' /tmp/Aptfile | xargs)

# Configure bundler
ENV LANG=C.UTF-8 \
BUNDLE_JOBS=4 \
BUNDLE_RETRY=3

# Store Bundler settings in the project's root
ENV BUNDLE_APP_CONFIG=.bundle

# Uncomment this line if you want to run binstubs without prefixing with `bin/` or `bundle exec`
# ENV PATH /app/bin:$PATH

# Upgrade RubyGems and install the latest Bundler version
RUN gem update --system && \
gem install bundler

# Create a directory for the app code
RUN mkdir -p /app
WORKDIR /app

# Document that we're going to expose port 3000
EXPOSE 3000
# Use Bash as the default command
CMD ["/usr/bin/bash"]
84 changes: 84 additions & 0 deletions examples/rails_app/.dockerdev/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Docker for Development

Source: [Ruby on Whales: Dockerizing Ruby and Rails development](https://evilmartians.com/chronicles/ruby-on-whales-docker-for-ruby-rails-development).

## Installation

- Docker installed.

For MacOS just use the [official app](https://docs.docker.com/engine/installation/mac/).

- [`dip`](https://github.com/bibendi/dip) installed.

You can install `dip` as Ruby gem:

```sh
gem install dip
```

## Provisioning

When using Dip it could be done with a single command:

```sh
dip provision
```

## Running

```sh
dip rails s
```

## Developing with Dip

### Useful commands

```sh
# run rails console
dip rails c

# run rails server with debugging capabilities (i.e., `debugger` would work)
dip rails s

# or run the while web app (with all the dependencies)
dip up web

# run migrations
dip rails db:migrate

# pass env variables into application
dip VERSION=20100905201547 rails db:migrate:down

# simply launch bash within app directory (with dependencies up)
dip runner

# execute an arbitrary command via Bash
dip bash -c 'ls -al tmp/cache'

# Additional commands

# update gems or packages
dip bundle install
dip yarn install

# run tests
# TIP: `dip rails test` is already auto prefixed with `RAILS_ENV=test`
dip rails test

# shutdown all containers
dip down
```

### Development flow

Another way is to run `dip <smth>` for every interaction. If you prefer this way and use ZSH, you can reduce the typing
by integrating `dip` into your session:

```sh
$ dip console | source /dev/stdin
# no `dip` prefix is required anymore!
$ rails c
Loading development environment (Rails 7.0.1)
pry>
```
89 changes: 89 additions & 0 deletions examples/rails_app/.dockerdev/compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
x-app: &app
build:
context: .
args:
RUBY_VERSION: '3.2.2'
NODE_MAJOR: '18'
image: railsapp-dev:1.0.0
environment: &env
NODE_ENV: ${NODE_ENV:-development}
RAILS_ENV: ${RAILS_ENV:-development}
tmpfs:
- /tmp
- /app/tmp/pids

x-backend: &backend
<<: *app
stdin_open: true
tty: true
volumes:
- ..:/app:cached
- bundle:/usr/local/bundle
- rails_cache:/app/tmp/cache
- assets:/app/public/assets
- node_modules:/app/node_modules
- history:/usr/local/hist
- ./.bashrc:/root/.bashrc:ro
environment: &backend_environment
<<: *env
MALLOC_ARENA_MAX: 2
WEB_CONCURRENCY: ${WEB_CONCURRENCY:-2}
BOOTSNAP_CACHE_DIR: /usr/local/bundle/_bootsnap
XDG_DATA_HOME: /app/tmp/cache
YARN_CACHE_FOLDER: /app/node_modules/.yarn-cache
HISTFILE: /usr/local/hist/.bash_history
IRB_HISTFILE: /usr/local/hist/.irb_history
EDITOR: vi
TZ: ${TZ:-UTC}
NATS_URL: "nats://nats:4222"
depends_on:
- nats
- redis

services:
rails:
<<: *backend
command: bundle exec rails

web:
<<: *backend
command: bundle exec rails server -b 0.0.0.0
ports:
- '3000:3000'
stdin_open: true
tty: true

listener:
<<: *backend
command: bin/nats-listener

sidekiq:
<<: *backend
command: bundle exec sidekiq
environment:
<<: *backend_environment
SIDEKIQ_CONCURRENCY: 5

nats:
image: nats:2.9.18
command: -c /etc/nats.conf -js -sd=/data
ports:
- '4222:4222'
- '8080:8080'
volumes:
- ./nats.conf:/etc/nats.conf:ro
- nats_data:/data

redis:
image: redis:6.2.5
ports:
- '6379:6379'

volumes:
bundle:
node_modules:
history:
rails_cache:
assets:
nats_data:

6 changes: 6 additions & 0 deletions examples/rails_app/.dockerdev/nats.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
port 4222

websocket {
port: 8080
no_tls: true
}
7 changes: 7 additions & 0 deletions examples/rails_app/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# See https://git-scm.com/docs/gitattributes for more about git attribute files.

# Mark the database schema as having been generated.
db/schema.rb linguist-generated

# Mark any vendored files as having been vendored.
vendor/* linguist-vendored
29 changes: 29 additions & 0 deletions examples/rails_app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# See https://help.github.com/articles/ignoring-files for more about ignoring files.
#
# If you find yourself ignoring temporary files generated by your text editor
# or operating system, you probably want to add a global ignore instead:
# git config --global core.excludesfile '~/.gitignore_global'

# Ignore bundler config.
/.bundle

# Ignore the default SQLite database.
/db/*.sqlite3
/db/*.sqlite3-*

# Ignore all logfiles and tempfiles.
/log/*
/tmp/*
!/log/.keep
!/tmp/.keep

# Ignore pidfiles, but keep the directory.
/tmp/pids/*
!/tmp/pids/
!/tmp/pids/.keep


/public/assets

# Ignore master key for decrypting credentials and more.
/config/master.key
1 change: 1 addition & 0 deletions examples/rails_app/.ruby-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ruby-3.2.2
30 changes: 30 additions & 0 deletions examples/rails_app/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby "3.2.2"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.5"

# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"

# Use sqlite3 as the database for Active Record
gem "sqlite3", "~> 1.4"

# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.0"

# Background jobs
gem "sidekiq", "~> 7.1"

# NATS for inter-component messaging
gem "nats-pure", "~> 2.3", github: "Envek/nats-pure.rb", branch: "fix/auto-connect-after-code-reload", require: "nats/client"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]

group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri mingw x64_mingw ]
end
Loading

0 comments on commit e16bfaf

Please sign in to comment.