diff --git a/.env.example b/.env.example index 363d135..4383c82 100644 --- a/.env.example +++ b/.env.example @@ -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 diff --git a/admin-ui/middleware.ts b/admin-ui/middleware.ts index 6511d0d..050b356 100644 --- a/admin-ui/middleware.ts +++ b/admin-ui/middleware.ts @@ -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(); } diff --git a/admin-ui/next.config.ts b/admin-ui/next.config.ts index 3df43b8..3c27519 100644 --- a/admin-ui/next.config.ts +++ b/admin-ui/next.config.ts @@ -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; diff --git a/deploy/lxc/env.example b/deploy/lxc/env.example index 2ab22c7..391524d 100644 --- a/deploy/lxc/env.example +++ b/deploy/lxc/env.example @@ -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. diff --git a/docker-compose.yml b/docker-compose.yml index 6cb8da5..2f00ee4 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -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: diff --git a/scripts/onboard-project.sh b/scripts/onboard-project.sh new file mode 100755 index 0000000..e6186c1 --- /dev/null +++ b/scripts/onboard-project.sh @@ -0,0 +1,98 @@ +#!/usr/bin/env bash +# onboard-project.sh — create a new project + ingest key in error_reporting. +# +# Usage: +# onboard-project.sh [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 [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="$(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 </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