# syntax=docker/dockerfile:1
# Build stage: regenerate the static site from source data.
FROM python:3.12-slim AS build
WORKDIR /app
COPY . .
RUN python3 generate_dashboard.py

# Serve stage: nginx serves static files + proxies /hook to the host webhook receiver.
FROM nginx:alpine
RUN printf '%s\n' \
    'server {' \
    '    listen 80;' \
    '    server_name _;' \
    '    root /usr/share/nginx/html;' \
    '    index dashboard.html;' \
    '    client_max_body_size 2m;' \
    '    location = / { return 302 /dashboard.html; }' \
    '    location /hook {' \
    '        proxy_pass http://__HOST_IP__:41798;' \
    '        proxy_set_header Host $host;' \
    '        proxy_set_header Authorization $http_authorization;' \
    '        proxy_set_header X-Gitea-Signature $http_x_gitea_signature;' \
    '        proxy_set_header X-Gitea-Event $http_x_gitea_event;' \
    '        proxy_set_header X-Gitea-Event-Type $http_x_gitea_event_type;' \
    '        proxy_set_header Content-Type $content_type;' \
    '        proxy_read_timeout 60s;' \
    '    }' \
    '}' > /etc/nginx/conf.d/default.conf.template
COPY --from=build /app/dashboard.html /usr/share/nginx/html/dashboard.html
COPY --from=build /app/pages         /usr/share/nginx/html/pages
RUN printf '%s\n' '#!/bin/sh' 'set -e' \
    'HOST_IP="${HOST_IP:-host.docker.internal}"' \
    'sed "s|__HOST_IP__|${HOST_IP}|g" /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf' \
    'exec nginx -g "daemon off;"' > /docker-entrypoint-hostip.sh \
    && chmod +x /docker-entrypoint-hostip.sh
EXPOSE 80
ENTRYPOINT ["/docker-entrypoint-hostip.sh"]
