Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dockerizing linky #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.git
.gitignore

log/*
tmp/*
4 changes: 4 additions & 0 deletions .env/development/database
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
POSTGRES_USER=postgres
POSTGRES_PASSWORD=aa91da1e-6f0b-4553-8ab9-5a13b239e745
POSTGRES_DB=linky_development

2 changes: 2 additions & 0 deletions .env/development/web
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DATABASE_HOST=database

2 changes: 1 addition & 1 deletion .ruby-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
ruby-2.6.1
ruby-2.6.3
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM ruby:2.6

# Add the Postgres apt repo (including lsb-release, which is needed to pick the right repo).
RUN apt-get update -yqq && apt-get install -yqq --no-install-recommends lsb-release && \
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -sc)-pgdg main" > /etc/apt/sources.list.d/PostgreSQL.list

# Always do an apt-get update when installing a package, in order to avoid getting
# a cached package list.
RUN apt-get update -yqq && apt-get install -yqq --no-install-recommends \
nodejs \
postgresql-client-11

# Copy just the Gemfile and run bundle install first, so that the step can be properly
# cached.
COPY Gemfile* /usr/src/app/

# Set the working directory to the app.
WORKDIR /usr/src/app/

# Install all of our dependencies.
RUN bundle install

# Now, copy the contents of the app into the image.
COPY . /usr/src/app/

# Start the rails server, binding to all network interfaces.
CMD ["bin/rails", "s", "-b", "0.0.0.0"]
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.1'
ruby '2.6.3'

gem 'rails', '~> 5.2.2'
gem 'pg', '>= 0.18', '< 2.0'
Expand Down
32 changes: 25 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,42 @@

Linky is a link shortening web application written in Ruby on Rails. Users interact with the app via a RESTFful HTTP API. The app provides an [Open API Specification](https://github.com/OAI/OpenAPI-Specification) that describes the API, allowing users to easily send requests via the included Swagger UI, the native [Postman app](https://www.getpostman.com/) or [generate their own API client](https://swagger.io/tools/swagger-codegen/).

## Requirements
## Containerized Development (Recommended)

### Requirements

* [Docker](https://www.docker.com/products/docker-desktop)

### Setup

1. Clone this repo from Github and `cd` into it.
2. `docker-compose up -d`
3. `docker-compose --rm web bin/rails db:setup`

### Usage

When the Rails container is launched via `docker-compose`, the app is automatically started and exposed on port `3000`.

## Local Development

### Requirements

* [Ruby](https://www.ruby-lang.org/en/) v2.6.1
* [Bundler](https://bundler.io/)
* [Postgres](https://www.postgresql.org/) v11 (earlier versions not supported)

## Setup
### Setup

1. Clone this repo from Github.
1. Clone this repo from Github and `cd` into it.
2. `bundle install`
3. `bin/rails db:setup` ()
3. `bin/rails db:setup`

## Usage
### Usage

### Running the App
#### Running the App

Just execute `bin/rails server` to start the application at `http://localhost:300`. The root URL is set as a redirect to the Swagger UI loaded with the app's Open API Specification. The spec document itself can be viewed directly via `/open_api/spec.json` path.

### Running the Tests
#### Running the Tests

Just execute `bin/rspec `.
1 change: 1 addition & 0 deletions config/boot.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$stdout.sync = true
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)

require 'bundler/setup' # Set up gems listed in the Gemfile.
Expand Down
75 changes: 4 additions & 71 deletions config/database.yml
Original file line number Diff line number Diff line change
@@ -1,85 +1,18 @@
# PostgreSQL. Versions 9.1 and up are supported.
#
# Install the pg driver:
# gem install pg
# On OS X with Homebrew:
# gem install pg -- --with-pg-config=/usr/local/bin/pg_config
# On OS X with MacPorts:
# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config
# On Windows:
# gem install pg
# Choose the win32 build.
# Install PostgreSQL and put its /bin directory on your path.
#
# Configure Using Gemfile
# gem 'pg'
#
default: &default
adapter: postgresql
encoding: unicode
# For details on connection pooling, see Rails configuration guide
# http://guides.rubyonrails.org/configuring.html#database-pooling
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
host: <%= ENV.fetch('DATABASE_HOST') %>
username: <%= ENV.fetch('POSTGRES_USER') %>
password: <%= ENV.fetch('POSTGRES_PASSWORD') %>
database: <%= ENV.fetch('POSTGRES_DB') %>

development:
<<: *default
database: linky_development

# The specified database role being used to connect to postgres.
# To create additional roles in postgres see `$ createuser --help`.
# When left blank, postgres will use the default role. This is
# the same name as the operating system user that initialized the database.
#username: linky

# The password associated with the postgres role (username).
#password:

# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
# domain sockets, so uncomment these lines.
#host: localhost

# The TCP port the server listens on. Defaults to 5432.
# If your server runs on a different port number, change accordingly.
#port: 5432

# Schema search path. The server defaults to $user,public
#schema_search_path: myapp,sharedapp,public

# Minimum log levels, in increasing order:
# debug5, debug4, debug3, debug2, debug1,
# log, notice, warning, error, fatal, and panic
# Defaults to warning.
#min_messages: notice

# Warning: The database defined as "test" will be erased and
# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.
test:
<<: *default
database: linky_test

# As with config/secrets.yml, you never want to store sensitive information,
# like your database password, in your source code. If your source code is
# ever seen by anyone, they now have access to your database.
#
# Instead, provide the password as a unix environment variable when you boot
# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database
# for a full rundown on how to provide these environment variables in a
# production deployment.
#
# On Heroku and other platform providers, you may have a full connection URL
# available as an environment variable. For example:
#
# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase"
#
# You can use this database configuration with:
#
# production:
# url: <%= ENV['DATABASE_URL'] %>
#
production:
<<: *default
database: linky_production
username: linky
password: <%= ENV['LINKY_DATABASE_PASSWORD'] %>
1 change: 1 addition & 0 deletions db/structure.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;

Expand Down
18 changes: 18 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: '3'

services:
web:
build: .
ports:
- '3000:3000'
volumes:
- .:/usr/src/app
env_file:
- .env/development/database
- .env/development/web


database:
image: postgres:11
env_file:
- .env/development/database