feat: migrar backend unificado a Go 1.27 + SPA Vite embebido

This commit is contained in:
urieljareth
2026-11-28 09:20:55 -06:00
parent 4f64487b88
commit b5ebcd0b86
332 changed files with 29135 additions and 37275 deletions
+33 -41
View File
@@ -1,49 +1,41 @@
# Cotizador — Uriel Jareth Consulting — Next.js (web)
# Multi-stage build. El runner conserva node_modules completo para que
# `prisma migrate deploy` y el seed (tsx) funcionen dentro del contenedor.
# Cotizador — Uriel Jareth Consulting — Binario unificado Go + SPA Vite embebido
# Multi-stage build:
# 1. node:22-alpine compila el SPA React 19 + Tailwind CSS v4 (web/dist)
# 2. golang:1.27-alpine embebe web/dist via //go:embed y compila binario estatico (CGO_ENABLED=0)
# 3. alpine:3.21 ejecuta el binario unico (~25 MB) con ca-certificates y tzdata
FROM node:22-slim AS base
# ── Stage 1: Frontend SPA (Vite + React 19 + Tailwind v4) ──
FROM node:22-alpine AS web-builder
WORKDIR /web
COPY web/package.json web/package-lock.json* ./
RUN if [ -f package-lock.json ]; then npm ci; else npm install; fi
COPY web/ ./
RUN npm run build
# ── Stage 2: Backend Go + SPA embebido ──
FROM golang:1.27-alpine AS go-builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY cmd/ ./cmd/
COPY internal/ ./internal/
RUN rm -rf internal/spa/dist/*
COPY --from=web-builder /web/dist/ ./internal/spa/dist/
RUN CGO_ENABLED=0 go build -trimpath -ldflags="-s -w" -o /out/cotizador ./cmd/cotizador
# ── Stage 3: Runtime minimo ──
FROM alpine:3.21 AS runner
RUN apk add --no-cache ca-certificates tzdata wget
WORKDIR /app
ENV NEXT_TELEMETRY_DISABLED=1
COPY --from=go-builder /out/cotizador /app/cotizador
# ── Dependencias ──
FROM base AS deps
COPY package.json package-lock.json ./
RUN npm ci
# ── Build ──
FROM base AS builder
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Placeholder solo para el build: src/lib/auth.ts lanza error al importarse
# sin JWT_SECRET. Todas las páginas son force-dynamic, nada queda horneado.
ENV JWT_SECRET=placeholder-build-only
RUN npx prisma generate && npm run build
# ── Runtime ──
FROM base AS runner
ENV NODE_ENV=production
ENV PORT=3000
# openssl: el CLI de Prisma lo necesita para detectar la versión de libssl
RUN apt-get update -y && apt-get install -y --no-install-recommends openssl \
&& rm -rf /var/lib/apt/lists/*
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public
COPY --from=builder /app/prisma ./prisma
COPY --from=builder /app/scripts ./scripts
COPY --from=builder /app/src/generated ./src/generated
COPY --from=builder /app/prisma.config.ts ./prisma.config.ts
COPY --from=builder /app/next.config.ts ./next.config.ts
COPY --from=builder /app/tsconfig.json ./tsconfig.json
COPY --from=builder /app/package.json ./package.json
ENV UPLOAD_DIR=/app/uploads
EXPOSE 3000
HEALTHCHECK --interval=30s --timeout=10s --start-period=60s --retries=5 \
CMD node -e "fetch('http://localhost:3000/api/health').then(r=>process.exit(r.ok?0:1)).catch(()=>process.exit(1))"
HEALTHCHECK --interval=30s --timeout=10s --start-period=30s --retries=5 \
CMD wget -qO- http://localhost:3000/api/health >/dev/null || exit 1
# Migraciones primero, seed opcional (RUN_SEED=true), luego el servidor.
CMD ["sh", "-c", "npx prisma migrate deploy && if [ \"$RUN_SEED\" = \"true\" ]; then npx tsx prisma/seed.ts; fi && exec node_modules/.bin/next start -p ${PORT:-3000}"]
ENTRYPOINT ["/app/cotizador"]
CMD ["serve"]