# Neuron Landing — El-native server.
#
# Two-stage build:
#   1. Build the El landing binary in a Debian container with cc + libcurl-dev
#      from server.c (which was generated on the host by `elc server.el > server.c`)
#      plus el_runtime.c.
#   2. Copy the binary + the pre-rendered index.html + assets into a slim runtime
#      image. Final image is ~80 MB (mostly libc + libcurl).
#
# Build:
#   ./build.sh   (regenerates server.c, then docker buildx for linux/amd64)
#
# Run:
#   docker run -p 8080:8080 neuron-landing

# ── Stage 1: compile ──────────────────────────────────────────────────────────
FROM debian:bookworm-slim AS builder

RUN apt-get update \
 && apt-get install -y --no-install-recommends \
        build-essential \
        libcurl4-openssl-dev \
        ca-certificates \
 && rm -rf /var/lib/apt/lists/*

WORKDIR /build
COPY runtime/el_runtime.c runtime/el_runtime.h ./
COPY server.c ./

# -rdynamic is the Linux-specific bit: the runtime resolves El handler
# functions (e.g. handle_request) via dlsym(RTLD_DEFAULT, name), and on
# glibc that only sees the executable's own symbols if they were exported
# by the linker. macOS exports them by default, Linux doesn't.
RUN cc -std=c11 -O2 -rdynamic -I . -o landing server.c el_runtime.c -lcurl -lpthread -ldl -lm
# `strip` would discard .symtab but keeps .dynsym (which is what -rdynamic
# populated and what dlsym actually looks at), so it'd be safe — but the
# binary is small enough that the few MB saved isn't worth the next person
# wondering why dlsym fails.

# ── Stage 2: runtime ──────────────────────────────────────────────────────────
FROM debian:bookworm-slim

RUN apt-get update \
 && apt-get install -y --no-install-recommends \
        libcurl4 \
        ca-certificates \
 && rm -rf /var/lib/apt/lists/* \
 && groupadd -r landing && useradd -r -g landing landing

COPY --from=builder /build/landing /usr/local/bin/landing
COPY src/index.html /srv/landing/index.html
COPY src/assets /srv/landing/assets

ENV LANDING_ROOT=/srv/landing
ENV PORT=8080

USER landing
EXPOSE 8080

# Cloud Run sends SIGTERM on revision shutdown. The runtime doesn't trap it
# explicitly, so the kernel just kills the process. That's fine for a stateless
# server.
CMD ["/usr/local/bin/landing"]
