FROM python:3.12-alpine

# 配置 APK standard repositories and install LibreOffice 及 dependencies
RUN echo "https://dl-cdn.alpinelinux.org/alpine/v3.19/main" > /etc/apk/repositories && \
    echo "https://dl-cdn.alpinelinux.org/alpine/v3.19/community" >> /etc/apk/repositories && \
    apk update && \
    apk add --no-cache \
    libreoffice \
    libreoffice-common \
    libreoffice-writer \
    libreoffice-impress \
    libreoffice-calc \
    font-noto \
    font-noto-cjk \
    ttf-dejavu \
    imagemagick \
    && rm -rf /var/cache/apk/*

# 安装 Python 依赖
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

# Install Node.js, Chromium, and essential dependencies for Puppeteer
RUN apk add --no-cache \
    nodejs \
    npm \
    chromium \
    nss \
    freetype \
    harfbuzz \
    ca-certificates \
    ttf-dejavu \
    && rm -rf /var/cache/apk/*

# Configure Puppeteer to use installed Chromium
ENV PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true \
    PUPPETEER_EXECUTABLE_PATH=/usr/bin/chromium-browser

# Install Node dependencies
COPY package.json .
RUN npm install --registry=https://registry.npmmirror.com

WORKDIR /app
COPY main.py /app/
COPY md_to_pdf.js /app/

# Link node_modules to app directory so the script can find required modules
RUN ln -sf /node_modules /app/node_modules

# 创建挂载目录
RUN mkdir -p /app/uploads /temp

# 暴露端口
EXPOSE 8100

# 启动 FastAPI
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8100", "--workers", "2"]
