diff --git a/.gitignore b/.gitignore index 677c465..684c21b 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ .bundle +.env.local diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..f369d94 --- /dev/null +++ b/Dockerfile @@ -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}"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..fed9cac --- /dev/null +++ b/README.md @@ -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 +``` diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..6fa582c --- /dev/null +++ b/docker-compose.yml @@ -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 diff --git a/init.sql b/init.sql new file mode 100644 index 0000000..3d2c469 --- /dev/null +++ b/init.sql @@ -0,0 +1 @@ +CREATE USER hackinews SUPERUSER; diff --git a/lib/dotenv.rb b/lib/dotenv.rb index 8bf80d6..e9fdb50 100644 --- a/lib/dotenv.rb +++ b/lib/dotenv.rb @@ -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] diff --git a/models/item.rb b/models/item.rb index 6abb471..ecef878 100644 --- a/models/item.rb +++ b/models/item.rb @@ -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