Gateway CLI Reference
Theneuratrade gateway CLI is your primary interface for managing NeuraTrade services. It orchestrates the backend API, CCXT service, and Telegram bot with intelligent process management and health monitoring.
Overview
The gateway CLI provides:- Unified service orchestration - Start/stop all services with a single command
- Health monitoring - Continuous service health checks with automatic state tracking
- Process management - PID file-based process tracking with graceful shutdown
- Configuration management - Environment variable resolution with config file fallbacks
- Supervised mode - Keep services running during warmup phases
Architecture
The gateway acts as a lightweight process supervisor:Installation
Theneuratrade binary is built as part of the standard build process:
~/.local/bin/neuratrade (ensure this is in your PATH).
Gateway Commands
gateway start
Starts all NeuraTrade services in the correct dependency order.--supervised- Enable supervised mode (keeps gateway running even if initial health checks fail)
Environment resolution
Gateway reads configuration from:
- Environment variables
~/.neuratrade/config.json- Built-in defaults
SERVER_PORT→PORT→BACKEND_HOST_PORT→ config.server.port →8080
Service startup
Services start in this order:
- CCXT Service (port 3001, bound to 127.0.0.1)
- Backend API (port 8080, bound to 0.0.0.0)
- Telegram Service (port 3002, bound to 127.0.0.1)
- Logs to
~/.neuratrade/logs/<service>.log - Writes PID to
~/.neuratrade/pids/<service>.pid - Receives appropriate environment variables
Health verification
After startup, gateway probes each service:
- Timeout: 90 seconds (configurable via
NEURATRADE_GATEWAY_HEALTH_TIMEOUT_SECONDS) - Probe interval: 500ms
- Success criteria: HTTP 200-499 response
Supervised mode is recommended for production deployments where services may take time to initialize (e.g., loading market data, connecting to exchanges).
gateway stop
Stops all running NeuraTrade services gracefully.Process validation
For each PID:
- Verifies process exists
- Validates process matches expected pattern (e.g.,
neuratrade-server) - Removes stale PID files if process doesn’t match
Graceful termination
Sends SIGTERM to each process:This allows services to:
- Flush logs
- Close database connections
- Complete in-flight requests
- Clean up resources
| Service | Expected Patterns |
|---|---|
| Backend API | neuratrade-server |
| CCXT Service | ccxt-service |
| Telegram Service | telegram-service, bun run index.ts |
gateway status
Displays the current status of all services with detailed health information.Runtime state
Shows mode from Possible modes:
gateway-state.json:healthy- All services running and healthydegraded- Some services unhealthy but runningwarming- Services starting updown- All services stopped
Process Management
PID Files
The gateway uses PID files for robust process tracking: Location:~/.neuratrade/pids/
Files:
backend.pid- Backend API process IDccxt.pid- CCXT service process IDtelegram.pid- Telegram service process IDgateway-state.json- Runtime state and health data
Gateway State JSON
The gateway maintains a JSON state file for advanced monitoring: Location:~/.neuratrade/pids/gateway-state.json
Schema:
starting- Service process has been launchedwarming- Service running but health check not passing yethealthy- Service running and health check passingdegraded- Service running but returning errorsdown- Service process not running
Health Monitoring
The gateway runs a background health monitor when in foreground mode: Monitoring Behavior:- Interval: 15 seconds
- Checks: Process existence + HTTP health probe
- Updates:
gateway-state.jsonafter each check - Mode derivation:
- All services up + healthy →
healthy - All services down →
down - Services up but not healthy →
warming - Mixed state →
degraded
- All services up + healthy →
Environment Variables
The gateway respects these environment variables:Core Configuration
| Variable | Default | Description |
|---|---|---|
NEURATRADE_HOME | ~/.neuratrade | Base directory for config/logs/data |
PORT | 8080 | Backend API port |
SERVER_PORT | - | Overrides PORT if set |
BACKEND_HOST_PORT | - | Overrides PORT if set |
BIND_HOST | 127.0.0.1 | Host binding for internal services |
Service Ports
| Variable | Default | Description |
|---|---|---|
CCXT_PORT | 3001 | CCXT service HTTP port |
TELEGRAM_PORT | 3002 | Telegram service HTTP port |
CCXT_GRPC_PORT | 50051 | CCXT gRPC port |
TELEGRAM_GRPC_PORT | 50052 | Telegram gRPC port |
Gateway Behavior
| Variable | Default | Description |
|---|---|---|
NEURATRADE_GATEWAY_SUPERVISED | false | Enable supervised mode |
NEURATRADE_GATEWAY_HEALTH_TIMEOUT_SECONDS | 90 | Health check timeout in seconds |
Service Configuration
| Variable | Description |
|---|---|
DATABASE_DRIVER | Database type (sqlite, postgres) |
SQLITE_PATH | SQLite database file path |
REDIS_HOST | Redis server host |
REDIS_PORT | Redis server port |
TELEGRAM_BOT_TOKEN | Telegram bot token |
TELEGRAM_USE_POLLING | Use polling mode (true/false) |
JWT_SECRET | JWT signing secret (min 32 chars) |
ADMIN_API_KEY | Admin API key (min 32 chars) |
AI_API_KEY | AI provider API key |
AI_PROVIDER | AI provider name |
AI_MODEL | AI model identifier |
The gateway automatically generates secure
JWT_SECRET and ADMIN_API_KEY values if they’re missing or too short (< 32 characters).Signal Handling
The gateway handles Unix signals gracefully:SIGINT / SIGTERM
When Ctrl+C is pressed or SIGTERM is received:Advanced Usage
Custom Configuration File
Override default config location:config.json at /custom/path/config.json.
Port Resolution Logic
Backend port resolution follows this precedence:SERVER_PORTenvironment variablePORTenvironment variableBACKEND_HOST_PORTenvironment variableconfig.server.portfrom config.json- Default:
8080
Supervised Mode for Production
For production deployments with slower startup (database migrations, market data loading):systemd Integration
Create a systemd service for automatic startup:/etc/systemd/system/neuratrade.service
Troubleshooting
Gateway Won’t Start
Port already in use
Port already in use
Check which process is using the port:Kill the conflicting process or change the port:
Permission denied on log files
Permission denied on log files
Fix log directory permissions:
Config file not found
Config file not found
Verify NEURATRADE_HOME is set correctly:
Services Show as Unhealthy
Backend database connection fails
Backend database connection fails
Check database configuration:
Redis connection timeout
Redis connection timeout
Verify Redis is running:Check Redis configuration:
Telegram bot token invalid
Telegram bot token invalid
Test bot token:If invalid, get a new token from @BotFather.
PID Files Are Stale
If services crashed ungracefully, PID files may point to dead processes:Source Code Reference
Key implementation files:cmd/neuratrade-cli/gateway.go:51-gatewayStartfunctioncmd/neuratrade-cli/gateway.go:336-gatewayStopfunctioncmd/neuratrade-cli/gateway.go:438-gatewayStatusfunctioncmd/neuratrade-cli/gateway.go:296-startServicehelpercmd/neuratrade-cli/gateway.go:703-monitorGatewayHealthbackground monitor
Next Steps
Native Deployment
Full deployment guide
Monitoring Guide
Advanced health monitoring
Telegram Setup
Configure Telegram bot
Configuration
Complete configuration reference