FROM debian:bullseye-slim
MAINTAINER FmodeInc "support@fmode.cn"

######################################## APT Repos to 163
# RUN apt-get update

ADD sources.list /etc/apt/

RUN apt-get update

######################################## NODE

RUN groupadd --gid 1000 node \
  && useradd --uid 1000 --gid node --shell /bin/bash --create-home node

RUN apt-get update --fix-missing

RUN ARCH= && dpkgArch="$(dpkg --print-architecture)" \
    && case "${dpkgArch##*-}" in \
      amd64) ARCH='x64';; \
      ppc64el) ARCH='ppc64le';; \
      s390x) ARCH='s390x';; \
      arm64) ARCH='arm64';; \
      armhf) ARCH='armv7l';; \
      i386) ARCH='x86';; \
      *) echo "unsupported architecture"; exit 1 ;; \
    esac \
    && set -ex \
    # libatomic1 for arm
    && apt-get update && apt-get install -y ca-certificates apt-utils curl wget gnupg dirmngr xz-utils libatomic1 --no-install-recommends \
    && rm -rf /var/lib/apt/lists/* \
    && curl -fsSLO --compressed "https://npmmirror.com/mirrors/node/v18.19.1/node-v18.19.1-linux-$ARCH.tar.xz" \
    && curl -fsSLO --compressed "https://npmmirror.com/mirrors/node/v18.19.1/SHASUMS256.txt.asc" \
    && tar -xJf "node-v18.19.1-linux-$ARCH.tar.xz" -C /usr/local --strip-components=1 --no-same-owner \
    && rm "node-v18.19.1-linux-$ARCH.tar.xz" \
    && ln -s /usr/local/bin/node /usr/local/bin/nodejs


######################################## Nginx
# curl http检测
# procps 进程管理
RUN apt-get update &&apt-get install -y nginx procps && apt-get install -y nginx curl --no-install-recommends \
  && rm -rf /etc/nginx/sites-enabled/*  && service nginx restart

######################################## Copy Latest System
COPY ./opt/ /opt/

RUN mkdir -p /opt/edu-textbook-server/ && cd /opt/ && tar zxvf server.tar.gz && mv server/* edu-textbook-server/ && mkdir -p /etc/nginx/sites-enabled/ && cp -rf /opt/edu-textbook-server/config/nginx-server.conf /etc/nginx/sites-enabled/ && service nginx restart

RUN ls /opt/ && chown -R root:root /opt/*


######################################## PM2 && server node_modules
RUN npm config set registry https://registry.npmmirror.com/ && npm i -g npm@10.8.1
RUN cd /opt/edu-textbook-server/ && npm i --omit dev  --no-warn

CMD ["service","nginx","restart"]

# DEFAULT WEB
EXPOSE 80
# DEFAULT SSL
EXPOSE 443

ENTRYPOINT [ "node","/opt/edu-textbook-server/server.js" ]
