Files
vocero-client/s2s-reasoning-engine/start.sh
T

85 lines
3.5 KiB
Bash
Executable File

#!/usr/bin/env bash
# =============================================================
# start.sh — Run Node-RED 5.0 locally for s2s-reasoning-engine
#
# Usage:
# ./start.sh # default port 1881
# PORT=1900 ./start.sh # custom port
# LOG_LEVEL=debug ./start.sh # detailed logging
# DEBUG=nodes:* ./start.sh # internal Node-RED diagnostics
# Stop: Ctrl+C
# =============================================================
set -euo pipefail
ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PARENT_DIR="$(dirname "$ROOT_DIR")"
cd "$ROOT_DIR"
# ----------------------------------------------------------
# 1) Load the local .env first, then the parent ../.env when present.
# The parent file takes precedence when both define the same variable.
# ----------------------------------------------------------
set -a
if [ -f "$ROOT_DIR/.env" ]; then
echo "[env] loaded $ROOT_DIR/.env"
# shellcheck disable=SC1091
source "$ROOT_DIR/.env"
fi
if [ -f "$PARENT_DIR/.env" ]; then
echo "[env] loaded $PARENT_DIR/.env"
# shellcheck disable=SC1091
source "$PARENT_DIR/.env"
fi
set +a
# ----------------------------------------------------------
# 2) Node-RED runtime parameters
# ----------------------------------------------------------
PORT="${PORT:-${NODE_RED_PORT:-1881}}"
export PORT
export NODE_RED_PORT="$PORT"
export NODE_RED_LOG_LEVEL="${LOG_LEVEL:-${NODE_RED_LOG_LEVEL:-info}}"
export NODE_RED_CREDENTIAL_SECRET="${NODE_RED_CREDENTIAL_SECRET:-s2s-reasoning-local-dev}"
export FLOWS="${FLOWS:-$ROOT_DIR/.node-red/flows.json}"
# Local user directory; keep state here rather than in ~/.node-red.
USERDIR="${USERDIR:-$ROOT_DIR/.node-red}"
SETTINGS="${SETTINGS:-$USERDIR/settings.js}"
# Create the user directory and refresh its configuration from the source files.
mkdir -p "$USERDIR"
if [ ! -f "$SETTINGS" ] || [ "$ROOT_DIR/settings.js" -nt "$SETTINGS" ]; then
cp -f "$ROOT_DIR/settings.js" "$SETTINGS"
echo "[init] copied settings.js -> $SETTINGS"
fi
if [ ! -f "$FLOWS" ] || [ "$ROOT_DIR/flows.json" -nt "$FLOWS" ]; then
cp -f "$ROOT_DIR/flows.json" "$FLOWS"
echo "[init] copied flows.json -> $FLOWS"
fi
# Check that the local Node-RED dependencies are installed.
if [ ! -d "$ROOT_DIR/node_modules/node-red" ]; then
echo "[init] node_modules was not found. Running npm install..."
npm install --no-audit --no-fund
fi
echo ""
echo "╔════════════════════════════════════════════════════════════╗"
echo "║ s2s-reasoning-engine — Local Node-RED v5 ║"
echo "╠════════════════════════════════════════════════════════════╣"
echo "║ Editor: http://localhost:${PORT}/ ║"
echo "║ API: http://localhost:${PORT}/api/reasoning ║"
echo "║ UserDir: ${USERDIR}"
echo "║ LogLevel: ${NODE_RED_LOG_LEVEL}"
echo "╚════════════════════════════════════════════════════════════╝"
echo ""
# ----------------------------------------------------------
# 3) Start Node-RED
# ----------------------------------------------------------
exec "$ROOT_DIR/node_modules/.bin/node-red" \
--userDir "$USERDIR" \
--settings "$SETTINGS" \
--port "$PORT"