# ipass-gateway —— 多阶段构建
#
# 这里必须自己编译 nginx。原因见 CLAUDE.md：ngx-rust 官方明确要求「针对生产上
# 真正要用的那份 nginx 源码 + 完全相同的 configure 参数」编译模块，否则 bindgen
# 看到的符号/API 与实际运行时不一致。

# ---------------------------------------------------------------------------
# 构建阶段：编译 nginx + 编译 Rust 动态模块
# ---------------------------------------------------------------------------
FROM rust:1.89-slim-bookworm AS builder

ARG NGINX_VERSION=1.30.4

# nginx 构建依赖 + bindgen 需要的 libclang + 下载工具
RUN apt-get update && apt-get install -y --no-install-recommends \
        build-essential \
        libpcre2-dev \
        zlib1g-dev \
        libssl-dev \
        clang \
        libclang-dev \
        pkg-config \
        curl \
        ca-certificates \
        xz-utils \
    && rm -rf /var/lib/apt/lists/*

# 1) 拉取并编译 nginx（objs/ 里的头文件是 bindgen 的输入）
WORKDIR /src
RUN curl -fsSL "https://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz" -o nginx.tar.gz \
    && tar -xzf nginx.tar.gz \
    && rm nginx.tar.gz
WORKDIR /src/nginx-${NGINX_VERSION}
RUN ./configure \
        --prefix=/usr/local/nginx \
        --with-pcre \
        --with-http_realip_module \
        --with-http_ssl_module \
        --without-http_gzip_module \
        --without-http_ssi_module \
        --without-http_userid_module \
        --without-http_autoindex_module \
        --without-http_geo_module \
        --without-http_split_clients_module \
        --without-http_referer_module \
        --without-http_uwsgi_module \
        --without-http_scgi_module \
        --without-http_grpc_module \
    && make -j"$(nproc)"

# 2) 编译 Rust 动态模块。只需 configure 过的源码树 + objs/，不必等 nginx 编完。
WORKDIR /build/rust
COPY rust/ ./
RUN NGINX_SOURCE_DIR="/src/nginx-${NGINX_VERSION}" \
    NGINX_BUILD_DIR="/src/nginx-${NGINX_VERSION}/objs" \
    cargo build --release

# ---------------------------------------------------------------------------
# 运行阶段：只带 nginx 二进制 + 模块 + 静态资源
# ---------------------------------------------------------------------------
FROM debian:bookworm-slim AS runtime

ARG NGINX_VERSION=1.30.4

RUN apt-get update && apt-get install -y --no-install-recommends \
        libpcre2-8-0 \
        zlib1g \
        libssl3 \
        ca-certificates \
        curl \
        tzdata \
    && rm -rf /var/lib/apt/lists/*

# **必须设 TZ=Asia/Shanghai**（1.0.24 修复）：
# 容器默认 TZ=UTC，所有时间显示比北京慢 8h，「今日」配额/趋势日桶
# 在北京 08:00 切日（不是 00:00）反直觉。`util::day_id` 走 `localtime_r`，
# 设了 TZ 之后自动按上海时区 0:00 切日。
#
# 对应代码侧：plugin-llm/src/config.rs 的 `RESET_DAY_OFFSET_SEC` 必须从
# 28800 改为 0（之前手工补 8h 是因为容器 TZ=UTC）。改一处不动的副作用见
# commit message 与 docs。
ENV TZ=Asia/Shanghai

COPY --from=builder /src/nginx-${NGINX_VERSION}/objs/nginx /usr/local/nginx/sbin/nginx
COPY --from=builder /src/nginx-${NGINX_VERSION}/conf/mime.types /usr/local/nginx/conf/mime.types
COPY --from=builder /build/rust/target/release/libipass_gateway.so /usr/local/nginx/modules/

# 配置 / 证书（正式证书随镜像分发；勿把镜像推到公共仓库）/ 管理台静态页 / 种子数据
# conf/nginx.conf 里的 include tls/*.conf 不递归，子目录内的供应商 .conf 不会被误加载
COPY conf/nginx.conf /usr/local/nginx/conf/nginx.conf
COPY conf/tls/ /usr/local/nginx/conf/tls/
COPY www/ /usr/local/nginx/www/
COPY deploy/data-seed/ /usr/local/nginx/data/

# 与旧版保持一致的工作目录约定，便于沿用现成的挂载与运维习惯
# data 必须对 worker（默认 nobody）可写：状态写穿 JSON 依赖它
# audit 同理：LLM 对话审计落盘目录（docs/llm/AUDIT_LOG.md），生产建议挂宿主机卷
RUN mkdir -p /usr/local/nginx/conf /usr/local/nginx/www /usr/local/nginx/data /usr/local/nginx/audit /usr/local/nginx/logs \
    && chown -R nobody:nogroup /usr/local/nginx/data /usr/local/nginx/audit \
    && ln -sf /dev/stdout /usr/local/nginx/logs/access.log \
    && ln -sf /dev/stderr /usr/local/nginx/logs/error.log

WORKDIR /usr/local/nginx
EXPOSE 80 18098 443
STOPSIGNAL SIGQUIT

CMD ["/usr/local/nginx/sbin/nginx", "-g", "daemon off;"]
