fix(admin-ui): proxy ingest API through Next.js rewrites (BIT-750)

The single-domain deployment (errors.bitreport.pt) routed all traffic
through the admin-ui, whose auth middleware redirected /v1/ingest/events
and /health to /login — making the ingest API unreachable from outside.

- middleware.ts: pass through /v1/* and /health without auth redirect
- next.config.ts: rewrite /v1/* and /health to ingest-api backend
  (INGEST_API_URL env, default http://127.0.0.1:3041)
- docker-compose.yml: set INGEST_API_URL=http://ingest-api:3041
- env examples: document INGEST_API_URL
- scripts/onboard-project.sh: generate project + ingest key SQL

Paperclip-Agent: peter-spark
Paperclip-Issue: BIT-750
Paperclip-Run: dc5b7949-b24d-4867-a2cd-cf4f47477ef8
This commit is contained in:
Peter Spark committed 2026-04-03 10:01:55 +00:00
1 parent c8adcac393
commit 3b175eec86
6 files changed
+128

No files matched your search

+3
View File
@@ -15,6 +15,9 @@ ER_PUBLISH_ADMIN_PORT=3040
ERROR_REPORTING_UI_DEMO=1
# When admin Fastify API serves /v1/incidents (service `api`, not ingest-api), e.g.:
# ERROR_REPORTING_ADMIN_API_BASE=http://api:3050
# Ingest API URL for Next.js reverse-proxy rewrites (default http://127.0.0.1:3041).
# In Docker Compose this should be http://ingest-api:3041.
# INGEST_API_URL=http://ingest-api:3041
# --- Ingest API (service: ingest-api) ---
ER_PUBLISH_INGEST_PORT=3041
+8
View File
@@ -27,6 +27,14 @@ export function middleware(request: NextRequest) {
return NextResponse.next();
}
/** Ingest API paths — proxied to the ingest-api backend via next.config rewrites. */
if (
pathname === "/health" ||
pathname.startsWith("/v1/")
) {
return NextResponse.next();
}
if (_allowWithoutSession()) {
return NextResponse.next();
}
+14
View File
@@ -1,8 +1,22 @@
import type { NextConfig } from "next";
/**
* Ingest API backend (same LXC, port 3041).
* In Docker Compose the service name is "ingest-api"; on bare-metal it is localhost.
*/
const INGEST_API_URL =
process.env.INGEST_API_URL || "http://127.0.0.1:3041";
const nextConfig: NextConfig = {
output: "standalone",
reactStrictMode: true,
async rewrites() {
return [
{ source: "/health", destination: `${INGEST_API_URL}/health` },
{ source: "/v1/:path*", destination: `${INGEST_API_URL}/v1/:path*` },
];
},
};
export default nextConfig;
+3
View File
@@ -31,6 +31,9 @@ MAX_BACKOFF_EXPONENT=8
# --- admin-ui (Next.js server-side API calls) — target the Fastify `api/` service (default PORT 3200) ---
ERROR_REPORTING_ADMIN_API_BASE=http://127.0.0.1:3200
# Ingest API backend for Next.js reverse-proxy rewrites (/v1/*, /health).
# On a single LXC this defaults to http://127.0.0.1:3041 (no change needed).
# INGEST_API_URL=http://127.0.0.1:3041
# --- Paperclip error loop / CRM-compatible GET /api/error-monitoring/recent-errors (Fastify validates query) ---
# Same names as CRM: copy one secret into PAPERCLIP_ERROR_API_KEY or DEBUG_VIEW_ERRORS_KEY on the runner.
+2
View File
@@ -117,6 +117,8 @@ services:
# Default demo KPIs/layout until admin API exposes /v1/incidents (set to 0 and set ERROR_REPORTING_ADMIN_API_BASE).
ERROR_REPORTING_UI_DEMO: ${ERROR_REPORTING_UI_DEMO:-1}
ERROR_REPORTING_ADMIN_API_BASE: ${ERROR_REPORTING_ADMIN_API_BASE:-}
# Ingest API backend for Next.js reverse-proxy rewrites (/v1/*, /health).
INGEST_API_URL: http://ingest-api:3041
ports:
- "${ER_PUBLISH_ADMIN_PORT:-3040}:3040"
healthcheck:
+98
View File
@@ -0,0 +1,98 @@
#!/usr/bin/env bash
# onboard-project.sh — create a new project + ingest key in error_reporting.
#
# Usage:
# onboard-project.sh <slug> <name> [label]
#
# Generates a random ingest token, prints the SQL to apply and the
# plaintext token to configure in the client. Requires: openssl, sha256sum.
#
# Example:
# ./scripts/onboard-project.sh crm "BitReport CRM"
# ./scripts/onboard-project.sh ott "BitReport OTT" staging
#
# Environment:
# DATABASE_URL If set and mysql client available, applies SQL directly.
# Otherwise prints SQL to stdout.
set -euo pipefail
_die() { echo "error: $*" >&2; exit 1; }
[[ $# -ge 2 ]] || _die "Usage: onboard-project.sh <slug> <name> [label]"
SLUG="$1"
NAME="$2"
LABEL="${3:-default}"
# Validate slug: lowercase alphanumeric + hyphens, 2-64 chars
[[ "${SLUG}" =~ ^[a-z0-9][a-z0-9-]{0,62}[a-z0-9]$ ]] \
|| _die "slug must be 2-64 lowercase alphanumeric/hyphens: ${SLUG}"
# Generate a random 32-byte token (er_<slug-prefix>_<random>)
SLUG_PREFIX="$(echo "${SLUG}" | tr -d '-' | head -c6)"
RANDOM_PART="$(openssl rand -hex 20)"
TOKEN="er_${SLUG_PREFIX}_${RANDOM_PART}"
# SHA-256 hash (stored in DB)
TOKEN_HASH="$(printf '%s' "${TOKEN}" | sha256sum | awk '{print $1}')"
# Token prefix for display (first 8 chars)
TOKEN_PREFIX="$(printf '%s' "${TOKEN}" | head -c8)"
# Build SQL
SQL="$(cat <<EOSQL
-- Project: ${NAME} (${SLUG})
BEGIN;
INSERT INTO er_projects (slug, name, is_active)
VALUES ('${SLUG}', '${NAME}', TRUE)
ON DUPLICATE KEY UPDATE name = VALUES(name);
INSERT INTO er_ingest_keys (project_id, label, token_prefix, token_hash, status)
SELECT p.id, '${LABEL}', '${TOKEN_PREFIX}', '${TOKEN_HASH}', 'active'
FROM er_projects p WHERE p.slug = '${SLUG}'
ON DUPLICATE KEY UPDATE token_hash = VALUES(token_hash), token_prefix = VALUES(token_prefix), status = 'active';
COMMIT;
EOSQL
)"
echo "================================================================"
echo " Project : ${NAME} (${SLUG})"
echo " Label : ${LABEL}"
echo " Token : ${TOKEN}"
echo " Hash : ${TOKEN_HASH}"
echo "================================================================"
echo ""
echo "--- SQL (apply to error_reporting database) ---"
echo "${SQL}"
echo ""
echo "--- Client configuration ---"
echo " INGEST_URL=https://errors.bitreport.pt/v1/ingest/events"
echo " INGEST_TOKEN=${TOKEN}"
echo ""
echo "--- Smoke test ---"
echo " curl -sf -X POST https://errors.bitreport.pt/v1/ingest/events \\"
echo " -H 'Authorization: Bearer ${TOKEN}' \\"
echo " -H 'Content-Type: application/json' \\"
echo " -d '{\"events\":[{\"schema_version\":1,\"occurred_at\":\"$(date -u +%Y-%m-%dT%H:%M:%S.000Z)\",\"level\":\"info\",\"message\":\"onboard smoke test\"}]}'"
echo ""
# If DATABASE_URL is set, offer to apply directly
if [[ -n "${DATABASE_URL:-}" ]] && command -v mysql >/dev/null 2>&1; then
echo "DATABASE_URL is set. Applying SQL..."
# Parse mysql:// URL
DB_URL="${DATABASE_URL#mysql://}"
DB_USER="${DB_URL%%:*}"
DB_REST="${DB_URL#*:}"
DB_PASS="${DB_REST%%@*}"
DB_REST="${DB_REST#*@}"
DB_HOST="${DB_REST%%:*}"
DB_REST="${DB_REST#*:}"
DB_PORT="${DB_REST%%/*}"
DB_NAME="${DB_REST#*/}"
echo "${SQL}" | mysql -h"${DB_HOST}" -P"${DB_PORT}" -u"${DB_USER}" -p"${DB_PASS}" "${DB_NAME}"
echo "Applied successfully."
fi