langmart-gateway-type3 3.0.8 → 3.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/scripts/start.sh CHANGED
@@ -1,85 +1,254 @@
1
1
  #!/bin/bash
2
- # Start Gateway Type 3 in background with SSH disconnect protection
3
-
4
- # Colors
5
- GREEN='\033[0;32m'
6
- YELLOW='\033[1;33m'
7
- RED='\033[0;31m'
8
- BLUE='\033[0;34m'
9
- CYAN='\033[0;36m'
10
- NC='\033[0m' # No Color
11
-
12
- # Configuration
13
- GATEWAY_PORT=8083
14
- LOG_FILE="/tmp/gw3.log"
15
- PID_FILE="/tmp/gw3.pid"
2
+ # Gateway Type 3 Start Script
3
+ # Designed to work from installed location (via npm install) or development
4
+ #
5
+ # Features:
6
+ # - Detect service mode vs standalone mode
7
+ # - Load configuration from .env file
8
+ # - Pre-flight checks (port, required params)
9
+ # - Use isolated Node.js if available (for older systems)
10
+ # - Output machine-readable RESULT line for automation
11
+
12
+ set -e
13
+
14
+ # Get script directory and project directory
16
15
  SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
17
16
  PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
18
17
 
19
- # Detect host IP
20
- HOST_IP=$(hostname -I | awk '{print $1}')
21
- if [ -z "$HOST_IP" ]; then
22
- HOST_IP="localhost"
18
+ # If running from node_modules, find the actual install dir
19
+ if [[ "$PROJECT_DIR" == *"node_modules"* ]]; then
20
+ INSTALL_DIR="$(cd "$PROJECT_DIR/../.." && pwd)"
21
+ else
22
+ INSTALL_DIR="$PROJECT_DIR"
23
23
  fi
24
24
 
25
- # Check if already running
26
- if lsof -i :$GATEWAY_PORT -sTCP:LISTEN -t >/dev/null 2>&1; then
27
- echo -e "${YELLOW}Gateway Type 3 is already running on port $GATEWAY_PORT${NC}"
28
- exit 0
25
+ # Configuration with defaults
26
+ LOG_DIR="${INSTALL_DIR}/logs"
27
+ LOG_FILE="${LOG_DIR}/gateway.log"
28
+ PID_FILE="${INSTALL_DIR}/gateway.pid"
29
+ ENV_FILE="${INSTALL_DIR}/.env"
30
+ SERVICE_MODE_FILE="${INSTALL_DIR}/.service-mode"
31
+ SERVICE_NAME="langmart-gateway"
32
+
33
+ # Output tracking
34
+ running=false
35
+ port=""
36
+ pid=""
37
+ service_mode=false
38
+
39
+ # Log function that outputs to both console and can be parsed
40
+ log() {
41
+ local level="${2:-INFO}"
42
+ local timestamp=$(date +"%H:%M:%S")
43
+ echo "[$level] $timestamp - $1"
44
+ }
45
+
46
+ # Check if running in service mode
47
+ check_service_mode() {
48
+ if [ -f "$SERVICE_MODE_FILE" ]; then
49
+ local mode=$(cat "$SERVICE_MODE_FILE" 2>/dev/null || echo "")
50
+ if [ "$mode" = "systemd" ]; then
51
+ if systemctl list-unit-files 2>/dev/null | grep -q "^${SERVICE_NAME}.service"; then
52
+ service_mode=true
53
+ return 0
54
+ fi
55
+ fi
56
+ fi
57
+ service_mode=false
58
+ return 1
59
+ }
60
+
61
+ # Detect Node.js binary (prefer isolated installation)
62
+ detect_node() {
63
+ if [ -f "$INSTALL_DIR/node/bin/node" ]; then
64
+ NODE_BIN="$INSTALL_DIR/node/bin/node"
65
+ log "Using isolated Node.js: $($NODE_BIN --version)"
66
+ else
67
+ NODE_BIN="node"
68
+ local version=$(node --version 2>/dev/null || echo "not found")
69
+ log "Using system Node.js: $version"
70
+
71
+ if [[ "$version" != "not found" ]]; then
72
+ major_version=$(echo "$version" | sed 's/v//' | cut -d. -f1)
73
+ if [ "$major_version" -lt 14 ]; then
74
+ log "WARNING: Node.js $version may not support modern JavaScript syntax" "WARN"
75
+ fi
76
+ fi
77
+ fi
78
+ }
79
+
80
+ # Load .env file
81
+ load_env() {
82
+ if [ -f "$ENV_FILE" ]; then
83
+ log "Loading environment from $ENV_FILE"
84
+ set -a
85
+ source "$ENV_FILE"
86
+ set +a
87
+ return 0
88
+ else
89
+ log ".env file not found at $ENV_FILE" "WARN"
90
+ return 1
91
+ fi
92
+ }
93
+
94
+ # Find the correct entry file
95
+ find_entry_file() {
96
+ if [ -f "$INSTALL_DIR/node_modules/langmart-gateway-type3/dist/index-server.js" ]; then
97
+ ENTRY_FILE="$INSTALL_DIR/node_modules/langmart-gateway-type3/dist/index-server.js"
98
+ elif [ -f "$PROJECT_DIR/dist/index-server.js" ]; then
99
+ ENTRY_FILE="$PROJECT_DIR/dist/index-server.js"
100
+ else
101
+ log "Entry file not found!" "ERROR"
102
+ return 1
103
+ fi
104
+ log "Entry file: $ENTRY_FILE"
105
+ return 0
106
+ }
107
+
108
+ # ============================================
109
+ # MAIN SCRIPT
110
+ # ============================================
111
+
112
+ log "=== Gateway Type 3 Start Script ==="
113
+ log "Install directory: $INSTALL_DIR"
114
+
115
+ # Load environment
116
+ load_env
117
+
118
+ # Set defaults
119
+ GATEWAY_PORT="${GATEWAY_PORT:-8083}"
120
+ GATEWAY_API_KEY="${GATEWAY_API_KEY:-}"
121
+ MARKETPLACE_URL="${MARKETPLACE_URL:-ws://localhost:8081}"
122
+ GATEWAY_ID="${GATEWAY_ID:-}"
123
+
124
+ # ============================================
125
+ # CHECK SERVICE MODE
126
+ # ============================================
127
+ check_service_mode
128
+ if [ "$service_mode" = true ]; then
129
+ log "Service mode detected (systemd)"
130
+
131
+ service_status=$(systemctl is-active "$SERVICE_NAME" 2>/dev/null || echo "inactive")
132
+
133
+ if [ "$service_status" = "active" ]; then
134
+ existing_pid=$(lsof -ti:$GATEWAY_PORT 2>/dev/null || true)
135
+ if [ -n "$existing_pid" ]; then
136
+ log "Service already running (PID: $existing_pid)" "SUCCESS"
137
+ echo "RESULT:running=true,port=$GATEWAY_PORT,pid=$existing_pid,mode=service"
138
+ exit 0
139
+ fi
140
+ fi
141
+
142
+ log "Starting via systemctl..."
143
+ if sudo systemctl start "$SERVICE_NAME" 2>/dev/null || systemctl start "$SERVICE_NAME" 2>/dev/null; then
144
+ sleep 3
145
+ existing_pid=$(lsof -ti:$GATEWAY_PORT 2>/dev/null || true)
146
+ if [ -n "$existing_pid" ]; then
147
+ log "Service started successfully (PID: $existing_pid)" "SUCCESS"
148
+ echo "RESULT:running=true,port=$GATEWAY_PORT,pid=$existing_pid,mode=service"
149
+ exit 0
150
+ else
151
+ log "Service start command succeeded but gateway not listening" "ERROR"
152
+ echo "RESULT:running=false,port=,pid=,mode=service"
153
+ exit 1
154
+ fi
155
+ else
156
+ log "Failed to start service (may need sudo)" "ERROR"
157
+ echo "RESULT:running=false,port=,pid=,mode=service"
158
+ exit 1
159
+ fi
29
160
  fi
30
161
 
31
- # Check if dist folder exists
32
- if [ ! -d "$PROJECT_DIR/dist" ]; then
33
- echo -e "${YELLOW}Building TypeScript...${NC}"
34
- cd "$PROJECT_DIR"
35
- npm run build > /dev/null 2>&1 || echo -e "${RED}Build warnings (may be ignored)${NC}"
162
+ # ============================================
163
+ # STANDALONE MODE
164
+ # ============================================
165
+ log "Running in standalone mode"
166
+
167
+ # PRE-FLIGHT CHECK: Required parameters
168
+ log "Checking required parameters..."
169
+
170
+ missing_vars=""
171
+ [ -z "$GATEWAY_API_KEY" ] && missing_vars="$missing_vars GATEWAY_API_KEY"
172
+ [ -z "$MARKETPLACE_URL" ] && missing_vars="$missing_vars MARKETPLACE_URL"
173
+
174
+ if [ -n "$missing_vars" ]; then
175
+ log "Missing required parameters:$missing_vars" "ERROR"
176
+ echo "RESULT:running=false,port=,pid=,mode=standalone"
177
+ exit 1
36
178
  fi
37
179
 
38
- # Change to project directory
39
- cd "$PROJECT_DIR"
40
-
41
- # Set environment variables (use existing or defaults)
42
- export GATEWAY_API_KEY="${GATEWAY_API_KEY:-sk-test-inference-key}"
43
- export MARKETPLACE_URL="${MARKETPLACE_URL:-ws://$HOST_IP:8081}"
44
- export GATEWAY_PORT="${GATEWAY_PORT:-8083}"
45
-
46
- echo -e "${BLUE}Starting Gateway Type 3...${NC}"
47
- echo -e "${CYAN}Environment:${NC}"
48
- echo -e " Port: $GATEWAY_PORT"
49
- echo -e " Marketplace: $MARKETPLACE_URL"
50
- echo -e " API Key: ${GATEWAY_API_KEY:0:10}..."
51
-
52
- # Start gateway with nohup for SSH disconnect protection
53
- nohup node dist/index-server.js > "$LOG_FILE" 2>&1 &
54
- GATEWAY_PID=$!
55
-
56
- # Save PID to file
57
- echo $GATEWAY_PID > "$PID_FILE"
58
-
59
- echo -e "${GREEN}✅ Gateway Type 3 started (PID: $GATEWAY_PID)${NC}"
60
- echo -e "${GREEN}✅ Protected from SSH disconnection (nohup)${NC}"
61
- echo -e "${CYAN}Logs: $LOG_FILE${NC}"
62
- echo -e "${CYAN}PID File: $PID_FILE${NC}"
63
-
64
- # Wait for gateway to start
65
- echo -e "${YELLOW}Waiting for Gateway Type 3 to start...${NC}"
66
- sleep 2
67
-
68
- # Check health
69
- RETRIES=0
70
- MAX_RETRIES=10
71
- while [ $RETRIES -lt $MAX_RETRIES ]; do
72
- if curl -s "http://$HOST_IP:$GATEWAY_PORT/health" > /dev/null 2>&1; then
73
- echo -e "${GREEN}✅ Gateway Type 3 is healthy${NC}"
74
- echo -e "${CYAN}Management API: http://$HOST_IP:$GATEWAY_PORT${NC}"
75
- exit 0
180
+ log "GATEWAY_PORT: $GATEWAY_PORT"
181
+ log "MARKETPLACE_URL: $MARKETPLACE_URL"
182
+ log "GATEWAY_API_KEY: ${GATEWAY_API_KEY:0:8}..."
183
+
184
+ # PRE-FLIGHT CHECK: Already running
185
+ existing_pid=$(lsof -ti:$GATEWAY_PORT 2>/dev/null || true)
186
+ if [ -n "$existing_pid" ]; then
187
+ log "Gateway already running on port $GATEWAY_PORT (PID: $existing_pid)" "SUCCESS"
188
+ echo "RESULT:running=true,port=$GATEWAY_PORT,pid=$existing_pid,mode=standalone"
189
+ exit 0
190
+ fi
191
+ log "Port $GATEWAY_PORT is available" "SUCCESS"
192
+
193
+ # PRE-FLIGHT CHECK: Node.js
194
+ detect_node
195
+
196
+ # PRE-FLIGHT CHECK: Entry file
197
+ find_entry_file || {
198
+ echo "RESULT:running=false,port=,pid=,mode=standalone"
199
+ exit 1
200
+ }
201
+
202
+ # ============================================
203
+ # START GATEWAY
204
+ # ============================================
205
+ log "=== Starting Gateway ==="
206
+
207
+ mkdir -p "$LOG_DIR"
208
+ > "$LOG_FILE"
209
+
210
+ export GATEWAY_PORT GATEWAY_API_KEY MARKETPLACE_URL GATEWAY_ID
211
+
212
+ log "Starting with: $NODE_BIN $ENTRY_FILE"
213
+
214
+ cd "$INSTALL_DIR"
215
+ nohup "$NODE_BIN" "$ENTRY_FILE" >> "$LOG_FILE" 2>&1 &
216
+ gateway_pid=$!
217
+
218
+ echo $gateway_pid > "$PID_FILE"
219
+
220
+ log "Process started with PID: $gateway_pid"
221
+ log "Logs: $LOG_FILE"
222
+
223
+ # VERIFY GATEWAY IS LISTENING
224
+ log "Waiting for gateway to start listening..."
225
+
226
+ max_wait=15
227
+ wait_interval=2
228
+ total_waited=0
229
+ listening=false
230
+
231
+ while [ $total_waited -lt $max_wait ]; do
232
+ sleep $wait_interval
233
+ total_waited=$((total_waited + wait_interval))
234
+
235
+ check_pid=$(lsof -ti:$GATEWAY_PORT 2>/dev/null || true)
236
+ if [ -n "$check_pid" ]; then
237
+ listening=true
238
+ pid="$check_pid"
239
+ break
76
240
  fi
77
- sleep 1
78
- RETRIES=$((RETRIES + 1))
79
- echo -n "."
241
+ log "Waiting... ($total_waited/$max_wait seconds)"
80
242
  done
81
243
 
82
- echo ""
83
- echo -e "${RED}❌ Gateway failed to start within timeout${NC}"
84
- echo -e "${CYAN}Check logs: tail -f $LOG_FILE${NC}"
85
- exit 1
244
+ if [ "$listening" = true ]; then
245
+ log "Gateway confirmed listening on port $GATEWAY_PORT (PID: $pid)" "SUCCESS"
246
+ [ -f "$LOG_FILE" ] && tail -5 "$LOG_FILE" 2>/dev/null || true
247
+ echo "RESULT:running=true,port=$GATEWAY_PORT,pid=$pid,mode=standalone"
248
+ exit 0
249
+ else
250
+ log "Gateway failed to start listening" "ERROR"
251
+ [ -f "$LOG_FILE" ] && cat "$LOG_FILE" 2>/dev/null || true
252
+ echo "RESULT:running=false,port=,pid=,mode=standalone"
253
+ exit 1
254
+ fi
@@ -0,0 +1,114 @@
1
+ # Gateway Type 3 Status Script for Windows
2
+ # Features: Service mode detection, health check, RESULT output
3
+
4
+ $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
5
+ $ProjectDir = Split-Path -Parent $ScriptDir
6
+ $InstallDir = if ($ProjectDir -like "*node_modules*") { (Get-Location).Path } else { $ProjectDir }
7
+
8
+ $EnvFile = Join-Path $InstallDir ".env"
9
+ $PidFile = Join-Path $InstallDir "gateway.pid"
10
+ $LogFile = Join-Path $InstallDir "logs\gateway.log"
11
+ $ServiceModeFile = Join-Path $InstallDir ".service-mode"
12
+ $ServiceName = "LangMartGateway"
13
+
14
+ function Write-Log { param([string]$Message, [string]$Type = "INFO"); Write-Output "[$Type] $(Get-Date -Format 'HH:mm:ss') - $Message" }
15
+
16
+ function Test-ServiceMode {
17
+ if (Test-Path $ServiceModeFile) {
18
+ $mode = Get-Content $ServiceModeFile -ErrorAction SilentlyContinue
19
+ if ($mode -eq "windows-service") {
20
+ if (Get-Service -Name $ServiceName -ErrorAction SilentlyContinue) { return $true }
21
+ }
22
+ }
23
+ return $false
24
+ }
25
+
26
+ # Load .env for GATEWAY_PORT
27
+ if (Test-Path $EnvFile) {
28
+ Get-Content $EnvFile | ForEach-Object {
29
+ if ($_ -match '^([^#=]+)=(.*)$') {
30
+ [Environment]::SetEnvironmentVariable($matches[1].Trim(), $matches[2].Trim().Trim('"').Trim("'"), 'Process')
31
+ }
32
+ }
33
+ }
34
+
35
+ $GatewayPort = if ($env:GATEWAY_PORT) { [int]$env:GATEWAY_PORT } else { 8083 }
36
+
37
+ Write-Log "=== Gateway Type 3 Status ==="
38
+ Write-Log "Install directory: $InstallDir"
39
+ Write-Log "Port: $GatewayPort"
40
+
41
+ # CHECK SERVICE MODE
42
+ $serviceMode = Test-ServiceMode
43
+ if ($serviceMode) {
44
+ Write-Log "Mode: Service (Windows Service)"
45
+ $service = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
46
+ Write-Log "Service status: $($service.Status)"
47
+ Write-Log "Service startup: $($service.StartType)"
48
+ } else {
49
+ Write-Log "Mode: Standalone"
50
+ }
51
+
52
+ # Check if running on port
53
+ try {
54
+ $existingPID = (Get-NetTCPConnection -LocalPort $GatewayPort -State Listen -ErrorAction SilentlyContinue).OwningProcess | Select-Object -First 1
55
+ } catch { $existingPID = $null }
56
+
57
+ $modeStr = if ($serviceMode) { "service" } else { "standalone" }
58
+
59
+ if ($existingPID) {
60
+ Write-Log "Status: RUNNING" "SUCCESS"
61
+ Write-Log "PID: $existingPID"
62
+ Write-Log "Port: $GatewayPort"
63
+
64
+ # Check health endpoint
65
+ try {
66
+ Invoke-WebRequest -Uri "http://localhost:$GatewayPort/health" -TimeoutSec 5 -ErrorAction SilentlyContinue | Out-Null
67
+ Write-Log "Health: OK" "SUCCESS"
68
+ } catch {
69
+ Write-Log "Health: Port open but API not responding" "WARN"
70
+ }
71
+
72
+ # Show PID file status
73
+ if (Test-Path $PidFile) {
74
+ $savedPid = Get-Content $PidFile -ErrorAction SilentlyContinue
75
+ if ($savedPid -eq $existingPID) {
76
+ Write-Log "PID file: Match ($PidFile)"
77
+ } else {
78
+ Write-Log "PID file: Mismatch (saved: $savedPid, actual: $existingPID)" "WARN"
79
+ }
80
+ } else {
81
+ Write-Log "PID file: Not found" "WARN"
82
+ }
83
+
84
+ # Show log file info
85
+ if (Test-Path $LogFile) {
86
+ $logSize = (Get-Item $LogFile).Length / 1KB
87
+ Write-Log "Log file: $LogFile ($([math]::Round($logSize, 1)) KB)"
88
+ }
89
+
90
+ if ($serviceMode) {
91
+ Write-Log ""
92
+ Write-Log "Service commands:"
93
+ Write-Log " Get-Service $ServiceName"
94
+ Write-Log " Get-Content $InstallDir\logs\service-stdout.log -Tail 50"
95
+ }
96
+
97
+ Write-Output "RESULT:running=true,port=$GatewayPort,pid=$existingPID,mode=$modeStr"
98
+ exit 0
99
+ } else {
100
+ Write-Log "Status: NOT RUNNING" "WARN"
101
+ Write-Log "Port: $GatewayPort (not in use)"
102
+
103
+ if (Test-Path $PidFile) {
104
+ Write-Log "PID file exists but process not running (stale)" "WARN"
105
+ }
106
+
107
+ if ($serviceMode) {
108
+ $service = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
109
+ if ($service.Status -eq "Stopped") { Write-Log "Service is stopped" }
110
+ }
111
+
112
+ Write-Output "RESULT:running=false,port=$GatewayPort,pid=,mode=$modeStr"
113
+ exit 0
114
+ }
package/scripts/status.sh CHANGED
@@ -1,79 +1,152 @@
1
1
  #!/bin/bash
2
- # Check Gateway Type 3 status
2
+ # Gateway Type 3 Status Script
3
+ # Check if gateway is running and report status
4
+ # Handles both service mode and standalone mode
5
+ #
6
+ # Output: RESULT:running=true|false,port=PORT,pid=PID,mode=service|standalone
3
7
 
4
- # Colors
5
- GREEN='\033[0;32m'
6
- YELLOW='\033[1;33m'
7
- RED='\033[0;31m'
8
- CYAN='\033[0;36m'
9
- NC='\033[0m' # No Color
8
+ set -e
9
+
10
+ # Get script directory and project directory
11
+ SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
12
+ PROJECT_DIR="$(dirname "$SCRIPT_DIR")"
13
+
14
+ # If running from node_modules, find the actual install dir
15
+ if [[ "$PROJECT_DIR" == *"node_modules"* ]]; then
16
+ INSTALL_DIR="$(cd "$PROJECT_DIR/../.." && pwd)"
17
+ else
18
+ INSTALL_DIR="$PROJECT_DIR"
19
+ fi
10
20
 
11
21
  # Configuration
12
- GATEWAY_PORT=8083
13
- PID_FILE="/tmp/gw3.pid"
14
- LOG_FILE="/tmp/gw3.log"
15
-
16
- # Detect host IP
17
- HOST_IP=$(hostname -I | awk '{print $1}')
18
- if [ -z "$HOST_IP" ]; then
19
- HOST_IP="localhost"
22
+ ENV_FILE="${INSTALL_DIR}/.env"
23
+ PID_FILE="${INSTALL_DIR}/gateway.pid"
24
+ LOG_FILE="${INSTALL_DIR}/logs/gateway.log"
25
+ SERVICE_MODE_FILE="${INSTALL_DIR}/.service-mode"
26
+ SERVICE_NAME="langmart-gateway"
27
+ service_mode=false
28
+
29
+ # Load .env for GATEWAY_PORT
30
+ if [ -f "$ENV_FILE" ]; then
31
+ set -a
32
+ source "$ENV_FILE"
33
+ set +a
20
34
  fi
21
35
 
22
- echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
23
- echo -e "${CYAN} Gateway Type 3 Status${NC}"
24
- echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
36
+ GATEWAY_PORT="${GATEWAY_PORT:-8083}"
37
+
38
+ # Log function
39
+ log() {
40
+ local level="${2:-INFO}"
41
+ local timestamp=$(date +"%H:%M:%S")
42
+ echo "[$level] $timestamp - $1"
43
+ }
44
+
45
+ # Check if running in service mode
46
+ check_service_mode() {
47
+ if [ -f "$SERVICE_MODE_FILE" ]; then
48
+ local mode=$(cat "$SERVICE_MODE_FILE" 2>/dev/null || echo "")
49
+ if [ "$mode" = "systemd" ]; then
50
+ if systemctl list-unit-files 2>/dev/null | grep -q "^${SERVICE_NAME}.service"; then
51
+ service_mode=true
52
+ return 0
53
+ fi
54
+ fi
55
+ fi
56
+ service_mode=false
57
+ return 1
58
+ }
59
+
60
+ log "=== Gateway Type 3 Status ==="
61
+ log "Install directory: $INSTALL_DIR"
62
+ log "Port: $GATEWAY_PORT"
63
+
64
+ # ============================================
65
+ # CHECK SERVICE MODE
66
+ # ============================================
67
+ check_service_mode
68
+ if [ "$service_mode" = true ]; then
69
+ log "Mode: Service (systemd)"
25
70
 
26
- # Check if port is in use
27
- if lsof -i :$GATEWAY_PORT -sTCP:LISTEN -t >/dev/null 2>&1; then
28
- PID=$(lsof -ti :$GATEWAY_PORT)
29
- echo -e "Status: ${GREEN}● Running${NC}"
30
- echo -e "PID: ${GREEN}$PID${NC}"
31
- echo -e "Port: ${GREEN}$GATEWAY_PORT${NC}"
71
+ service_status=$(systemctl is-active "$SERVICE_NAME" 2>/dev/null || echo "inactive")
72
+ service_enabled=$(systemctl is-enabled "$SERVICE_NAME" 2>/dev/null || echo "disabled")
73
+
74
+ log "Service status: $service_status"
75
+ log "Service enabled: $service_enabled"
76
+ else
77
+ log "Mode: Standalone"
78
+ fi
79
+
80
+ # Check if running on port
81
+ existing_pid=$(lsof -ti:$GATEWAY_PORT 2>/dev/null || true)
82
+
83
+ if [ -n "$existing_pid" ]; then
84
+ mode_str="standalone"
85
+ [ "$service_mode" = true ] && mode_str="service"
86
+
87
+ log "Status: RUNNING" "SUCCESS"
88
+ log "PID: $existing_pid"
89
+ log "Port: $GATEWAY_PORT"
32
90
 
33
91
  # Check health endpoint
92
+ HOST_IP=$(hostname -I 2>/dev/null | awk '{print $1}' || echo "localhost")
93
+ [ -z "$HOST_IP" ] && HOST_IP="localhost"
94
+
34
95
  if curl -s "http://$HOST_IP:$GATEWAY_PORT/health" > /dev/null 2>&1; then
35
- echo -e "Health: ${GREEN}✓ Healthy${NC}"
36
- echo -e "Management API: ${CYAN}http://$HOST_IP:$GATEWAY_PORT${NC}"
96
+ log "Health: OK" "SUCCESS"
37
97
  else
38
- echo -e "Health: ${YELLOW}⚠ Port open but API not responding${NC}"
98
+ log "Health: Port open but API not responding" "WARN"
39
99
  fi
40
100
 
41
101
  # Show PID file status
42
102
  if [ -f "$PID_FILE" ]; then
43
- SAVED_PID=$(cat "$PID_FILE")
44
- if [ "$PID" = "$SAVED_PID" ]; then
45
- echo -e "PID File: ${GREEN}✓ Match ($PID_FILE)${NC}"
103
+ saved_pid=$(cat "$PID_FILE")
104
+ if [ "$existing_pid" = "$saved_pid" ]; then
105
+ log "PID file: Match ($PID_FILE)"
46
106
  else
47
- echo -e "PID File: ${YELLOW}⚠ Mismatch (saved: $SAVED_PID, actual: $PID)${NC}"
107
+ log "PID file: Mismatch (saved: $saved_pid, actual: $existing_pid)" "WARN"
48
108
  fi
49
109
  else
50
- echo -e "PID File: ${YELLOW}⚠ Not found${NC}"
110
+ log "PID file: Not found" "WARN"
51
111
  fi
52
112
 
53
- # Show log file
113
+ # Show log file info
54
114
  if [ -f "$LOG_FILE" ]; then
55
- echo -e "Logs: ${CYAN}$LOG_FILE${NC}"
56
- LOG_SIZE=$(du -h "$LOG_FILE" | cut -f1)
57
- echo -e "Log Size: ${CYAN}$LOG_SIZE${NC}"
115
+ log_size=$(du -h "$LOG_FILE" 2>/dev/null | cut -f1 || echo "unknown")
116
+ log "Log file: $LOG_FILE ($log_size)"
58
117
  fi
118
+
119
+ # Show service-specific info
120
+ if [ "$service_mode" = true ]; then
121
+ log ""
122
+ log "Service commands:"
123
+ log " sudo systemctl status $SERVICE_NAME"
124
+ log " sudo journalctl -u $SERVICE_NAME -f"
125
+ fi
126
+
127
+ echo "RESULT:running=true,port=$GATEWAY_PORT,pid=$existing_pid,mode=$mode_str"
128
+ exit 0
59
129
  else
60
- echo -e "Status: ${RED}● Not Running${NC}"
61
- echo -e "Port: ${RED}$GATEWAY_PORT (not in use)${NC}"
130
+ mode_str="standalone"
131
+ [ "$service_mode" = true ] && mode_str="service"
62
132
 
63
- # Check PID file
133
+ log "Status: NOT RUNNING" "WARN"
134
+ log "Port: $GATEWAY_PORT (not in use)"
135
+
136
+ # Check for stale PID file
64
137
  if [ -f "$PID_FILE" ]; then
65
- echo -e "PID File: ${YELLOW}⚠ Exists but process not running (stale)${NC}"
66
- echo -e " ${CYAN}Run 'npm run stop' to clean up${NC}"
138
+ log "PID file exists but process not running (stale)" "WARN"
139
+ fi
140
+
141
+ # Service-specific info
142
+ if [ "$service_mode" = true ]; then
143
+ service_status=$(systemctl is-active "$SERVICE_NAME" 2>/dev/null || echo "inactive")
144
+ if [ "$service_status" = "failed" ]; then
145
+ log "Service is in failed state" "ERROR"
146
+ log "Check logs: sudo journalctl -u $SERVICE_NAME -n 50"
147
+ fi
67
148
  fi
68
- fi
69
149
 
70
- echo -e "${CYAN}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${NC}"
71
- echo -e ""
72
- echo -e "${CYAN}Commands:${NC}"
73
- echo -e " npm start - Start gateway in background"
74
- echo -e " npm stop - Stop gateway gracefully"
75
- echo -e " npm restart - Restart gateway"
76
- echo -e " npm run status - Show this status"
77
- echo -e " npm run ui - Start interactive UI"
78
- echo -e " npm run self - Start interactive UI (short form)"
79
- echo -e ""
150
+ echo "RESULT:running=false,port=$GATEWAY_PORT,pid=,mode=$mode_str"
151
+ exit 0
152
+ fi