langmart-gateway-type3 3.0.9 → 3.0.11

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.
@@ -1,211 +1,114 @@
1
- # Check Gateway Type 3 status on Windows
2
- # This script is designed to work both interactively and via SSH
3
- # Output format is designed to be parseable for remote status checks
1
+ # Gateway Type 3 Status Script for Windows
2
+ # Features: Service mode detection, health check, RESULT output
4
3
 
5
- param(
6
- [int]$Port = 8083,
7
- [switch]$Json,
8
- [switch]$Brief
9
- )
10
-
11
- # Get script/project directory
12
4
  $ScriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
13
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" }
14
15
 
15
- # If running from node_modules, adjust path
16
- if ($ProjectDir -like "*node_modules*") {
17
- $ProjectDir = (Get-Location).Path
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
18
24
  }
19
25
 
20
- # Load .env file to get port if not specified
21
- $EnvFile = Join-Path $ProjectDir ".env"
22
- $envVars = @{}
26
+ # Load .env for GATEWAY_PORT
23
27
  if (Test-Path $EnvFile) {
24
28
  Get-Content $EnvFile | ForEach-Object {
25
- if ($_ -match '^([^#][^=]+)=(.*)$') {
26
- $key = $matches[1].Trim()
27
- $value = $matches[2].Trim()
28
- $envVars[$key] = $value
29
- if ($key -eq "GATEWAY_PORT" -and $Port -eq 8083 -and $value) {
30
- $Port = [int]$value
31
- }
29
+ if ($_ -match '^([^#=]+)=(.*)$') {
30
+ [Environment]::SetEnvironmentVariable($matches[1].Trim(), $matches[2].Trim().Trim('"').Trim("'"), 'Process')
32
31
  }
33
32
  }
34
33
  }
35
34
 
36
- # Status object for JSON output
37
- $status = @{
38
- running = $false
39
- port = $Port
40
- pid = $null
41
- health = $false
42
- version = $null
43
- gateway_id = $null
44
- gateway_name = $null
45
- installed = $false
46
- mode = "direct"
47
- error = $null
48
- }
35
+ $GatewayPort = if ($env:GATEWAY_PORT) { [int]$env:GATEWAY_PORT } else { 8083 }
49
36
 
50
- # Output helper
51
- function Write-Log {
52
- param([string]$Message, [string]$Type = "INFO")
53
- if (-not $Json) {
54
- Write-Output "[$Type] $Message"
55
- }
56
- }
37
+ Write-Log "=== Gateway Type 3 Status ==="
38
+ Write-Log "Install directory: $InstallDir"
39
+ Write-Log "Port: $GatewayPort"
57
40
 
58
- # Check if gateway package is installed
59
- $EntryFile = $null
60
- if (Test-Path "$ProjectDir\node_modules\langmart-gateway-type3\dist\index-server.js") {
61
- $EntryFile = "$ProjectDir\node_modules\langmart-gateway-type3\dist\index-server.js"
62
- $status.installed = $true
63
- } elseif (Test-Path "$ProjectDir\dist\index-server.js") {
64
- $EntryFile = "$ProjectDir\dist\index-server.js"
65
- $status.installed = $true
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"
66
50
  }
67
51
 
68
- # Get version from package.json
69
- $PackageJson = "$ProjectDir\node_modules\langmart-gateway-type3\package.json"
70
- if (Test-Path $PackageJson) {
71
- try {
72
- $pkg = Get-Content $PackageJson | ConvertFrom-Json
73
- $status.version = $pkg.version
74
- } catch {}
75
- }
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 }
76
56
 
77
- # Get gateway config
78
- $ConfigFile = Join-Path $ProjectDir "gateway-config.json"
79
- if (Test-Path $ConfigFile) {
80
- try {
81
- $config = Get-Content $ConfigFile | ConvertFrom-Json
82
- $status.gateway_id = $config.gatewayId
83
- $status.gateway_name = $config.gatewayName
84
- } catch {}
85
- }
57
+ $modeStr = if ($serviceMode) { "service" } else { "standalone" }
86
58
 
87
- # Check if running
88
- try {
89
- $existingPID = (Get-NetTCPConnection -LocalPort $Port -State Listen -ErrorAction SilentlyContinue).OwningProcess | Select-Object -First 1
90
- if ($existingPID) {
91
- $status.running = $true
92
- $status.pid = $existingPID
93
- }
94
- } catch {
95
- $status.error = $_.Exception.Message
96
- }
59
+ if ($existingPID) {
60
+ Write-Log "Status: RUNNING" "SUCCESS"
61
+ Write-Log "PID: $existingPID"
62
+ Write-Log "Port: $GatewayPort"
97
63
 
98
- # Check health if running
99
- if ($status.running) {
64
+ # Check health endpoint
100
65
  try {
101
- $health = Invoke-RestMethod -Uri "http://localhost:$Port/health" -TimeoutSec 3 -ErrorAction Stop
102
- $status.health = $true
103
- if ($health.version) {
104
- $status.version = $health.version
105
- }
106
- if ($health.gateway_id) {
107
- $status.gateway_id = $health.gateway_id
108
- }
66
+ Invoke-WebRequest -Uri "http://localhost:$GatewayPort/health" -TimeoutSec 5 -ErrorAction SilentlyContinue | Out-Null
67
+ Write-Log "Health: OK" "SUCCESS"
109
68
  } catch {
110
- $status.health = $false
69
+ Write-Log "Health: Port open but API not responding" "WARN"
111
70
  }
112
- }
113
71
 
114
- # JSON output mode
115
- if ($Json) {
116
- $status | ConvertTo-Json -Compress
117
- exit $(if ($status.running) { 0 } else { 1 })
118
- }
119
-
120
- # Brief output mode (for SSH parsing)
121
- if ($Brief) {
122
- if ($status.running) {
123
- Write-Output "RUNNING:$($status.pid):$Port"
124
- if ($status.health) {
125
- Write-Output "HEALTH:OK"
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)"
126
77
  } else {
127
- Write-Output "HEALTH:FAIL"
128
- }
129
- if ($status.version) {
130
- Write-Output "VERSION:$($status.version)"
131
- }
132
- if ($status.gateway_id) {
133
- Write-Output "GATEWAY_ID:$($status.gateway_id)"
78
+ Write-Log "PID file: Mismatch (saved: $savedPid, actual: $existingPID)" "WARN"
134
79
  }
135
80
  } else {
136
- Write-Output "STOPPED"
137
- if ($status.installed) {
138
- Write-Output "INSTALLED:YES"
139
- if ($status.version) {
140
- Write-Output "VERSION:$($status.version)"
141
- }
142
- } else {
143
- Write-Output "INSTALLED:NO"
144
- }
145
- if ($status.gateway_id) {
146
- Write-Output "GATEWAY_ID:$($status.gateway_id)"
147
- }
81
+ Write-Log "PID file: Not found" "WARN"
148
82
  }
149
- exit $(if ($status.running) { 0 } else { 1 })
150
- }
151
-
152
- # Full status output
153
- Write-Output ""
154
- Write-Output "=========================================="
155
- Write-Output " Gateway Type 3 Status (Windows)"
156
- Write-Output "=========================================="
157
83
 
158
- if ($status.running) {
159
- Write-Log "Status: Running" "SUCCESS"
160
- Write-Log "PID: $($status.pid)"
161
- Write-Log "Port: $Port"
162
-
163
- if ($status.health) {
164
- Write-Log "Health: OK" "SUCCESS"
165
- } else {
166
- Write-Log "Health: Port open but API not responding" "WARN"
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)"
167
88
  }
168
89
 
169
- if ($status.version) {
170
- Write-Log "Version: $($status.version)"
171
- }
172
- if ($status.gateway_id) {
173
- Write-Log "Gateway ID: $($status.gateway_id)"
174
- }
175
- if ($status.gateway_name) {
176
- Write-Log "Gateway Name: $($status.gateway_name)"
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"
177
95
  }
96
+
97
+ Write-Output "RESULT:running=true,port=$GatewayPort,pid=$existingPID,mode=$modeStr"
98
+ exit 0
178
99
  } else {
179
- Write-Log "Status: Not Running" "WARN"
180
- Write-Log "Port: $Port (not in use)"
100
+ Write-Log "Status: NOT RUNNING" "WARN"
101
+ Write-Log "Port: $GatewayPort (not in use)"
181
102
 
182
- if ($status.installed) {
183
- Write-Log "Installed: Yes"
184
- if ($status.version) {
185
- Write-Log "Version: $($status.version)"
186
- }
187
- } else {
188
- Write-Log "Installed: No" "WARN"
103
+ if (Test-Path $PidFile) {
104
+ Write-Log "PID file exists but process not running (stale)" "WARN"
189
105
  }
190
- if ($status.gateway_id) {
191
- Write-Log "Gateway ID: $($status.gateway_id) (from config)"
106
+
107
+ if ($serviceMode) {
108
+ $service = Get-Service -Name $ServiceName -ErrorAction SilentlyContinue
109
+ if ($service.Status -eq "Stopped") { Write-Log "Service is stopped" }
192
110
  }
193
- }
194
111
 
195
- # Show log files
196
- $LogDir = Join-Path $ProjectDir "logs"
197
- $LogFile = Join-Path $LogDir "gateway.log"
198
- if (Test-Path $LogFile) {
199
- $logSize = (Get-Item $LogFile).Length
200
- $logSizeStr = if ($logSize -gt 1MB) { "{0:N2} MB" -f ($logSize / 1MB) } else { "{0:N0} KB" -f ($logSize / 1KB) }
201
- Write-Log "Log File: $LogFile ($logSizeStr)"
112
+ Write-Output "RESULT:running=false,port=$GatewayPort,pid=,mode=$modeStr"
113
+ exit 0
202
114
  }
203
-
204
- Write-Output ""
205
- Write-Output "Commands:"
206
- Write-Output " npm start - Start gateway"
207
- Write-Output " npm stop - Stop gateway"
208
- Write-Output " npm run status - Show this status"
209
- Write-Output ""
210
-
211
- exit $(if ($status.running) { 0 } else { 1 })
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