Retire 94.23.75.89/PM2 guidance in product docs; point operators at systemd moneyflix-redirector, NPM CT 102, the LXC runbook, and smoke script. Redact inline MariaDB password from redirector deploy notes. Paperclip-Issue: BIT-28102 Paperclip-Run: c3d1f836-ff51-40fa-a3be-c43a5b452ba1
210 lines
5.5 KiB
Markdown
210 lines
5.5 KiB
Markdown
# MoneyFlix Redirector
|
|
|
|
> **Production (BIT-28102):** Canonical host is **CT 241** `10.10.50.37` with **systemd** `moneyflix-redirector` (not PM2 / not `94.23.75.89`). Full ops: [README_MONEYFLIX.md](README_MONEYFLIX.md), Paperclip runbook `MONEYFLIX_LXC_DEPLOY_BIT24694.md`, smoke `CT_IP=10.10.50.37 bash tools/proxmox/smoke_moneyflix_prod.sh`.
|
|
|
|
High-performance NodeJS redirect service with MySQL analytics for the MoneyFlix CRM system.
|
|
|
|
## 🚀 Features
|
|
|
|
- **High Performance**: Handles millions of redirects with queue-based processing
|
|
- **MySQL Analytics**: Direct database logging with User Agent parsing
|
|
- **Real-time Cache**: 30-second redirect cache with automatic refresh
|
|
- **Queue System**: Batch processing with retry logic for reliability
|
|
- **User Agent Analysis**: Detailed device, browser, and OS detection
|
|
- **Health Monitoring**: Built-in status and health check endpoints
|
|
|
|
## 📁 Project Structure
|
|
|
|
```
|
|
moneyflix-redirector/
|
|
├── index.mjs # Main application entry point
|
|
├── db/
|
|
│ └── logUser.mjs # MySQL logging and queue system
|
|
├── helpers/
|
|
│ ├── helpers.mjs # CRM API integration
|
|
│ └── userAgentParser.mjs # User Agent parsing
|
|
├── ecosystem.config.js # PM2 configuration
|
|
├── package.json # Dependencies
|
|
└── README.md # This file
|
|
```
|
|
|
|
## 🛠️ Installation
|
|
|
|
1. **Install dependencies:**
|
|
```bash
|
|
npm install
|
|
```
|
|
|
|
2. **Configure environment:**
|
|
```bash
|
|
cp env.example .env
|
|
# Edit .env with your configuration
|
|
```
|
|
|
|
3. **Start with PM2:**
|
|
```bash
|
|
pm2 start ecosystem.config.js
|
|
```
|
|
|
|
## ⚙️ Configuration
|
|
|
|
### Environment Variables
|
|
|
|
| Variable | Default | Description |
|
|
|----------|---------|-------------|
|
|
| `NODE_ENV` | `production` | Environment mode |
|
|
| `PORT` | `3000` | Server port |
|
|
| `CRM_API_URL` | `https://redirect.moneyflix.com/api/redirects` | CRM API endpoint |
|
|
| `CRM_API_KEY` | - | API authentication key |
|
|
| `DB_HOST` | `127.0.0.1` | MySQL host |
|
|
| `DB_PORT` | `3306` | MySQL port |
|
|
| `DB_USER` | `root` | MySQL username |
|
|
| `DB_PASSWORD` | - | MySQL password |
|
|
| `DB_DATABASE` | `bitreport_redirects` | MySQL database name |
|
|
| `QUEUE_MAX_SIZE` | `1000` | Maximum queue size |
|
|
| `QUEUE_FLUSH_INTERVAL` | `5000` | Queue flush interval (ms) |
|
|
| `CACHE_TTL` | `30000` | Redirect cache TTL (ms) |
|
|
|
|
## 🔗 API Endpoints
|
|
|
|
### Redirect
|
|
```
|
|
GET /:slug
|
|
```
|
|
Redirects to the target URL for the given slug.
|
|
|
|
### Health Check
|
|
```
|
|
GET /health
|
|
```
|
|
Returns service health status and basic metrics.
|
|
|
|
### Status
|
|
```
|
|
GET /status
|
|
```
|
|
Returns detailed status including queue and database statistics.
|
|
|
|
## 📊 Database Schema
|
|
|
|
The service logs hits to the `redirect_hits` table:
|
|
|
|
```sql
|
|
CREATE TABLE redirect_hits (
|
|
id BIGINT AUTO_INCREMENT PRIMARY KEY,
|
|
redirect_id BIGINT NOT NULL,
|
|
ip_address VARCHAR(45) NOT NULL,
|
|
user_agent TEXT,
|
|
referer TEXT,
|
|
destination_url TEXT NOT NULL,
|
|
response_time INT DEFAULT 0,
|
|
status VARCHAR(50) DEFAULT 'redirected',
|
|
geo VARCHAR(10) DEFAULT 'Unknown',
|
|
browser VARCHAR(100) DEFAULT 'Unknown',
|
|
os VARCHAR(100) DEFAULT 'Unknown',
|
|
device VARCHAR(50) DEFAULT 'Unknown',
|
|
is_mobile TINYINT(1) DEFAULT 0,
|
|
is_bot TINYINT(1) DEFAULT 0,
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
|
INDEX idx_redirect_id (redirect_id),
|
|
INDEX idx_ip_address (ip_address),
|
|
INDEX idx_created_at (created_at),
|
|
INDEX idx_redirect_created (redirect_id, created_at)
|
|
);
|
|
```
|
|
|
|
## 🚀 Performance Features
|
|
|
|
### Queue System
|
|
- **Batch Processing**: Groups hits for efficient MySQL inserts
|
|
- **Auto Flush**: Processes queue every 5 seconds or when full
|
|
- **Retry Logic**: Failed hits are retried up to 3 times
|
|
- **Memory Efficient**: Configurable queue size limits
|
|
|
|
### Caching
|
|
- **Redirect Cache**: 30-second TTL for redirect definitions
|
|
- **Auto Refresh**: Background updates from CRM API
|
|
- **Memory Efficient**: Only stores active redirects
|
|
|
|
### User Agent Analysis
|
|
- **Device Detection**: Mobile, tablet, desktop classification
|
|
- **Browser Parsing**: Name, version, and engine detection
|
|
- **OS Detection**: Operating system and version parsing
|
|
- **Bot Detection**: Automatic crawler and bot identification
|
|
|
|
## 📈 Monitoring
|
|
|
|
### Queue Statistics
|
|
```javascript
|
|
{
|
|
queueSize: 42,
|
|
isProcessing: false,
|
|
maxSize: 1000,
|
|
flushInterval: 5000
|
|
}
|
|
```
|
|
|
|
### Database Statistics
|
|
```javascript
|
|
{
|
|
total_hits: 15420,
|
|
unique_redirects: 15,
|
|
unique_ips: 8923,
|
|
mobile_hits: 8234,
|
|
bot_hits: 234,
|
|
last_hit: "2024-01-15T10:30:45.000Z"
|
|
}
|
|
```
|
|
|
|
## 🔧 Development
|
|
|
|
### Local Development
|
|
```bash
|
|
npm run dev
|
|
```
|
|
|
|
### Production Deployment
|
|
```bash
|
|
npm start
|
|
# or
|
|
pm2 start ecosystem.config.js
|
|
```
|
|
|
|
### Testing
|
|
```bash
|
|
# Test redirect
|
|
curl http://localhost:3000/mf_bonus
|
|
|
|
# Check health
|
|
curl http://localhost:3000/health
|
|
|
|
# Check status
|
|
curl http://localhost:3000/status
|
|
```
|
|
|
|
## 📝 Logs
|
|
|
|
Logs are stored in the `logs/` directory:
|
|
- `combined.log` - All logs
|
|
- `out.log` - Standard output
|
|
- `error.log` - Error logs
|
|
|
|
## 🚨 Error Handling
|
|
|
|
- **Graceful Shutdown**: Flushes remaining hits on SIGTERM/SIGINT
|
|
- **Connection Pooling**: Automatic reconnection to MySQL
|
|
- **API Fallback**: Continues working even if CRM API is down
|
|
- **Queue Recovery**: Retries failed database operations
|
|
|
|
## 📊 Analytics Data
|
|
|
|
Each redirect hit includes:
|
|
- **Basic Info**: IP, User Agent, Referer, Response Time
|
|
- **Device Data**: Browser, OS, Device Type, Mobile Detection
|
|
- **Bot Detection**: Automatic crawler identification
|
|
- **Geographic**: Country code (if available)
|
|
- **Performance**: Response time and status
|
|
|
|
---
|
|
|
|
**Built by BitReport for MoneyFlix CRM System** |