Skip to content
Draft
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.bundle
.env.local
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Use the Ruby 2.7.1 image from Docker Hub
# as the base image (https://hub.docker.com/_/ruby)
FROM ruby:2.7.2

# Use a directory called /code in which to store
# this application's files. (The directory name
# is arbitrary and could have been anything.)
WORKDIR /code

# Copy all the application's files into the /code
# directory.
COPY . /code

# Run bundle install to install the Ruby dependencies.
RUN bundle install

# Commmand to run when this container starts.
CMD ["bundle", "exec", "puma", "-t", "5:5", "-p", "${PORT:-3000}", "-e", "${RACK_ENV:-development}"]
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
To run migrations in docker:
```
docker run --rm -it --network=home_rev_proxy_default -v "$(pwd)/db:/db" --env "DATABASE_URL=postgres://hackinews@database/hackinews?sslmode=disable" amacneil/dbmate up
```
111 changes: 111 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# Use the file format compatible with Docker Compose 3.8
version: "3.8"

# Each thing that Docker Compose runs is referred to as
# a "service". In our case, our Ruby application is one
# service ("web") and our PostgreSQL database instance
# is another service ("database").
services:
database:
# Use the postgres 13.1 base image for this container.
image: postgres:13.1

networks:
- rev_proxy

volumes:
# We need to tell Docker where on the PostgreSQL
# container we want to keep the PostgreSQL data.
# In this case we're telling it to use a directory
# called /var/lib/postgresql/data, although it
# conceivably could have been something else.
#
# We're associating this directory with something
# called a volume. (You can see all your Docker
# volumes by running +docker volume ls+.) The name
# of our volume is db_data.
- db_data:/var/lib/postgresql/data

# This copies our init.sql into our container, to
# a special file called
# /docker-entrypoint-initdb.d/init.sql. Anything
# at this location will get executed one per
# container, i.e. it will get executed the first
# time the container is created but not again.
#
# The init.sql file is a one-line that creates a
# user called (arbitrarily) hackinews.
- ./init.sql:/docker-entrypoint-initdb.d/init.sql

deploy:
restart_policy:
condition: on-failure

environment:
POSTGRES_HOST_AUTH_METHOD: trust

web:
# The root directory from which we're building.
build: .

networks:
- rev_proxy

# This makes it so any code changes inside the project
# directory get synced with Docker. Without this line,
# we'd have to restart the container every time we
# changed a file.
volumes:
- .:/code:cached

# The command to be run when we run "docker-compose up".
command: bash -c "bundle exec puma -t 5:5 -p ${PORT:-3000} -e ${RACK_ENV:-development}"

# Expose port 3000.
ports:
- "3000:3000"

# Specify that this container depends on the other
# container which we've called "database".
depends_on:
- database

deploy:
restart_policy:
condition: on-failure

# Specify the values of the environment variables
# used in this container.
environment:
RACK_ENV: development
DATABASE_URL: postgres://hackinews@database/hackinews

worker:
build: .

networks:
- rev_proxy

volumes:
- .:/code:cached

command: bash -c "bundle exec ruby -r ./worker.rb -e \"loop { Worker.run }\""

depends_on:
- database

deploy:
restart_policy:
condition: on-failure

environment:
RACK_ENV: development
DATABASE_URL: postgres://hackinews@database/hackinews

# Declare the volumes that our application uses.
volumes:
db_data:

networks:
rev_proxy:
name: home_rev_proxy_default
1 change: 1 addition & 0 deletions init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE USER hackinews SUPERUSER;
4 changes: 1 addition & 3 deletions lib/dotenv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,4 @@ def replace_newline_character(string)
end
end

if ENV['RACK_ENV'] == 'development' || ENV['RACK_ENV'] == 'test'
Dotenv.new.load %w[.env .env.local]
end
Dotenv.new.load %w[.env.local .env]
2 changes: 1 addition & 1 deletion models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def prefetch_children
def truncated_url
return unless data['url']

uri = URI.parse data['url'].delete('#%').encode(Encoding.find('ASCII'), { replace: '' })
uri = URI.parse data['url'].delete('#%').encode(Encoding.find('ASCII'), **{ replace: '' })

uri.host.tap do |host|
return host unless EXTENDED_TRUNCATION_DOMAINS.include? host
Expand Down