# gateway Dockerfile 离线版本
FROM maven:3-eclipse-temurin-21 AS builder

# 配置 Maven 阿里云镜像
RUN mkdir -p /root/.m2 && \
    echo '<?xml version="1.0" encoding="UTF-8"?>\n\
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"\n\
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n\
          xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">\n\
  <mirrors>\n\
    <mirror>\n\
      <id>aliyunmaven</id>\n\
      <mirrorOf>*</mirrorOf>\n\
      <name>阿里云公共仓库</name>\n\
      <url>https://maven.aliyun.com/repository/public</url>\n\
    </mirror>\n\
  </mirrors>\n\
</settings>' > /root/.m2/settings.xml

WORKDIR /opt/gateway

COPY backend/pom.xml ./
COPY backend/api-gateway/pom.xml ./api-gateway/

RUN --mount=type=cache,target=/root/.m2/repository \
    cd /opt/gateway/api-gateway && \
    mvn dependency:go-offline -Dmaven.test.skip=true || true

COPY backend/api-gateway /opt/gateway/api-gateway

RUN --mount=type=cache,target=/root/.m2/repository \
    cd /opt/gateway/api-gateway && \
    mvn clean package -Dmaven.test.skip=true

FROM datamate-java-base:latest

COPY --from=builder /opt/gateway/api-gateway/target/gateway.jar /opt/gateway/gateway.jar
COPY scripts/images/gateway/start.sh /opt/gateway/start.sh

RUN dos2unix /opt/gateway/start.sh \
    && chmod +x /opt/gateway/start.sh \
    && ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

EXPOSE 8080

ENTRYPOINT ["/opt/gateway/start.sh"]
CMD ["java", "-Duser.timezone=Asia/Shanghai", "-jar", "/opt/gateway/gateway.jar"]
