FROM ruby:3.2.0-slim

#HEALTHCHECK --start-period=60s CMD curl http://127.0.0.1/

ENV TINI_VERSION v0.19.0
ADD https://github.com/krallin/tini/releases/download/${TINI_VERSION}/tini /tini
RUN chmod +x /tini

RUN apt-get -qq update \
  && DEBIAN_FRONTEND=noninteractive apt-get -qq install -y \
    build-essential \
    openssl \
    curl \
    sqlite3 \
    libsqlite3-dev \
    wget \
    default-libmysqlclient-dev \
    nodejs \
    tzdata \
    git \
  && apt-get -qq clean \
  && apt-get autoremove -y \
  && rm -rf \
    /var/lib/apt/lists/* \
    /tmp/* \
    /var/tmp/*

RUN echo 'gem: --no-rdoc --no-ri' > /root/.gemrc
RUN gem install bundler
RUN gem update --system
#Cache gems so they don't install on every code change
RUN rm -rf /rails && mkdir /rails
WORKDIR /rails
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock
RUN bundle install --jobs 4

# Copy site into place.
ADD . /rails/

# Need a temporary secret key to precompile assets
ENV WRESTLINGDEV_SECRET_KEY_BASE=077cdbef5c2ccf22543fb17a67339f234306b7fa2e1e4463d851c444c10a5611829a2290b253da78339427f131571fac9a42c83d960b2d25ecc10a4a0a7ce1a2
# Precompile assets
RUN RAILS_ENV=production bundle exec rake assets:clobber
RUN RAILS_ENV=production bundle exec rake assets:precompile

#SSL
RUN mkdir /ssl \
  && openssl req -sha256 -subj '/CN=home/O=home LTD./C=US' \
    -x509 -nodes -days 365 -newkey rsa:4096 -keyout /ssl/server.key -out /ssl/server.crt

EXPOSE 80
EXPOSE 443

# Tini solves the zombie PID problem
ENTRYPOINT ["/tini", "--"]

# Enable Solid Queue to run inside Puma
ENV SOLID_QUEUE_IN_PUMA=true

# Use rails server instead of Puma directly to ensure Rails environment is loaded
CMD bundle exec rails server -e production -p 80 -b '0.0.0.0'
