feat(phase-0): setup Docker Compose with TypeScript monorepo structure
- Create monorepo structure with apps/ and packages/ - Add Docker Compose for api, web, db, redis, worker services - Migrate existing Express.js logic to TypeScript with 100% backward compatibility - Preserve all existing API endpoints (/api/track, /api/v1/track) with identical behavior - Setup development environment with hot reload and proper networking - Add comprehensive TypeScript configuration with path mapping - Include production-ready Dockerfiles with multi-stage builds - Maintain existing rate limiting (100 req/hour/IP) and response formats - Add health checks and graceful shutdown handling - Setup Turbo for efficient monorepo builds and development
This commit is contained in:
23
.env.example
Normal file
23
.env.example
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# Database
|
||||||
|
DATABASE_URL="postgresql://postgres:postgres@localhost:5432/redirect_intelligence"
|
||||||
|
|
||||||
|
# Redis
|
||||||
|
REDIS_URL="redis://localhost:6379"
|
||||||
|
|
||||||
|
# API
|
||||||
|
PORT=3333
|
||||||
|
NODE_ENV=development
|
||||||
|
JWT_SECRET="your-super-secret-jwt-key-change-in-production"
|
||||||
|
|
||||||
|
# Frontend
|
||||||
|
WEB_URL="http://localhost:3000"
|
||||||
|
REACT_APP_API_URL="http://localhost:3333"
|
||||||
|
|
||||||
|
# Optional: Google Safe Browsing API
|
||||||
|
GOOGLE_SAFE_BROWSING_API_KEY=""
|
||||||
|
|
||||||
|
# Logging
|
||||||
|
LOG_LEVEL=info
|
||||||
|
|
||||||
|
# Worker
|
||||||
|
WORKER_CONCURRENCY=5
|
||||||
119
README_v2.md
Normal file
119
README_v2.md
Normal file
@@ -0,0 +1,119 @@
|
|||||||
|
# Redirect Intelligence v2
|
||||||
|
|
||||||
|
A comprehensive URL redirect tracking and analysis platform built with Node.js, React, and PostgreSQL.
|
||||||
|
|
||||||
|
## 🚀 Quick Start
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
|
||||||
|
- Node.js 20+
|
||||||
|
- Docker & Docker Compose
|
||||||
|
- Git
|
||||||
|
|
||||||
|
### Development Setup
|
||||||
|
|
||||||
|
1. **Clone and setup environment:**
|
||||||
|
```bash
|
||||||
|
git clone <your-repo-url>
|
||||||
|
cd redirect-intelligence-v2
|
||||||
|
cp .env.example .env
|
||||||
|
```
|
||||||
|
|
||||||
|
2. **Start development environment:**
|
||||||
|
```bash
|
||||||
|
npm run dev
|
||||||
|
```
|
||||||
|
|
||||||
|
This will start all services:
|
||||||
|
- API: http://localhost:3333
|
||||||
|
- Web: http://localhost:3000
|
||||||
|
- PostgreSQL: localhost:5432
|
||||||
|
- Redis: localhost:6379
|
||||||
|
|
||||||
|
3. **Access the application:**
|
||||||
|
- Main App: http://localhost:3000
|
||||||
|
- API Docs: http://localhost:3333/api/docs
|
||||||
|
- Health Check: http://localhost:3333/health
|
||||||
|
|
||||||
|
## 📋 Project Structure
|
||||||
|
|
||||||
|
```
|
||||||
|
├── apps/
|
||||||
|
│ ├── api/ # Express.js API server (TypeScript)
|
||||||
|
│ ├── web/ # React frontend (Chakra UI)
|
||||||
|
│ └── worker/ # BullMQ background worker
|
||||||
|
├── packages/
|
||||||
|
│ ├── database/ # Prisma schema & migrations
|
||||||
|
│ └── shared/ # Shared types & utilities
|
||||||
|
└── docker-compose.yml
|
||||||
|
```
|
||||||
|
|
||||||
|
## 🔄 Backward Compatibility
|
||||||
|
|
||||||
|
All existing API endpoints are preserved:
|
||||||
|
|
||||||
|
- `POST /api/track` (legacy)
|
||||||
|
- `POST /api/v1/track`
|
||||||
|
- `GET /api/v1/track`
|
||||||
|
|
||||||
|
Rate limiting: 100 requests/hour per IP (unchanged)
|
||||||
|
|
||||||
|
## 🛠️ Development Commands
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Start all services
|
||||||
|
npm run dev
|
||||||
|
|
||||||
|
# Individual services
|
||||||
|
npm run dev:api
|
||||||
|
npm run dev:web
|
||||||
|
|
||||||
|
# Database operations
|
||||||
|
npm run db:migrate
|
||||||
|
npm run db:studio
|
||||||
|
npm run db:seed
|
||||||
|
|
||||||
|
# Build & test
|
||||||
|
npm run build
|
||||||
|
npm run test
|
||||||
|
npm run lint
|
||||||
|
```
|
||||||
|
|
||||||
|
## 📖 API Documentation
|
||||||
|
|
||||||
|
Visit http://localhost:3333/api/docs for interactive API documentation.
|
||||||
|
|
||||||
|
## 🔍 Current Status
|
||||||
|
|
||||||
|
**Phase 0: Complete** ✅
|
||||||
|
- Docker Compose setup
|
||||||
|
- TypeScript migration
|
||||||
|
- Monorepo structure
|
||||||
|
- Backward compatibility preserved
|
||||||
|
|
||||||
|
**Next: Phase 1** - PostgreSQL + Prisma + Authentication
|
||||||
|
|
||||||
|
## 🧪 Testing
|
||||||
|
|
||||||
|
Test the backward compatibility:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Test legacy endpoint
|
||||||
|
curl -X POST http://localhost:3333/api/track \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{"url": "github.com"}'
|
||||||
|
|
||||||
|
# Test v1 endpoint
|
||||||
|
curl -X POST http://localhost:3333/api/v1/track \
|
||||||
|
-H "Content-Type: application/json" \
|
||||||
|
-d '{"url": "github.com", "method": "GET"}'
|
||||||
|
```
|
||||||
|
|
||||||
|
## 📚 Documentation
|
||||||
|
|
||||||
|
- [Implementation Plan](./IMPLEMENTATION_PLAN.md) - Detailed phase-by-phase plan
|
||||||
|
- [Original Plan](./redirect_intelligence_v2_plan.md) - High-level requirements
|
||||||
|
|
||||||
|
## 🤝 Contributing
|
||||||
|
|
||||||
|
See [IMPLEMENTATION_PLAN.md](./IMPLEMENTATION_PLAN.md) for development phases and guidelines.
|
||||||
78
apps/api/Dockerfile
Normal file
78
apps/api/Dockerfile
Normal file
@@ -0,0 +1,78 @@
|
|||||||
|
# Multi-stage build for production optimization
|
||||||
|
FROM node:20-alpine AS base
|
||||||
|
|
||||||
|
# Install dependencies only when needed
|
||||||
|
FROM base AS deps
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy package files
|
||||||
|
COPY package*.json ./
|
||||||
|
COPY apps/api/package*.json ./apps/api/
|
||||||
|
COPY packages/database/package*.json ./packages/database/
|
||||||
|
COPY packages/shared/package*.json ./packages/shared/
|
||||||
|
|
||||||
|
# Install dependencies
|
||||||
|
RUN npm ci --only=production && npm cache clean --force
|
||||||
|
|
||||||
|
# Development stage
|
||||||
|
FROM base AS dev
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy package files
|
||||||
|
COPY package*.json ./
|
||||||
|
COPY apps/api/package*.json ./apps/api/
|
||||||
|
COPY packages/database/package*.json ./packages/database/
|
||||||
|
COPY packages/shared/package*.json ./packages/shared/
|
||||||
|
|
||||||
|
# Install all dependencies including devDependencies
|
||||||
|
RUN npm ci
|
||||||
|
|
||||||
|
# Copy source code
|
||||||
|
COPY apps/api ./apps/api
|
||||||
|
COPY packages/database ./packages/database
|
||||||
|
COPY packages/shared ./packages/shared
|
||||||
|
|
||||||
|
WORKDIR /app/apps/api
|
||||||
|
|
||||||
|
EXPOSE 3333
|
||||||
|
|
||||||
|
CMD ["npm", "run", "dev"]
|
||||||
|
|
||||||
|
# Build stage
|
||||||
|
FROM base AS builder
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy everything needed for build
|
||||||
|
COPY package*.json ./
|
||||||
|
COPY apps/api ./apps/api
|
||||||
|
COPY packages/database ./packages/database
|
||||||
|
COPY packages/shared ./packages/shared
|
||||||
|
|
||||||
|
# Install dependencies and build
|
||||||
|
RUN npm ci
|
||||||
|
WORKDIR /app/apps/api
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
# Production stage
|
||||||
|
FROM base AS production
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy production dependencies
|
||||||
|
COPY --from=deps /app/node_modules ./node_modules
|
||||||
|
COPY --from=deps /app/apps/api/node_modules ./apps/api/node_modules
|
||||||
|
|
||||||
|
# Copy built application
|
||||||
|
COPY --from=builder /app/apps/api/dist ./apps/api/dist
|
||||||
|
COPY --from=builder /app/apps/api/package.json ./apps/api/
|
||||||
|
|
||||||
|
WORKDIR /app/apps/api
|
||||||
|
|
||||||
|
# Create non-root user
|
||||||
|
RUN addgroup --system --gid 1001 nodejs
|
||||||
|
RUN adduser --system --uid 1001 nodejs
|
||||||
|
|
||||||
|
USER nodejs
|
||||||
|
|
||||||
|
EXPOSE 3333
|
||||||
|
|
||||||
|
CMD ["npm", "start"]
|
||||||
51
apps/api/package.json
Normal file
51
apps/api/package.json
Normal file
@@ -0,0 +1,51 @@
|
|||||||
|
{
|
||||||
|
"name": "@redirect-intelligence/api",
|
||||||
|
"version": "2.0.0",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"dev": "tsx watch --clear-screen=false src/index.ts",
|
||||||
|
"build": "tsc",
|
||||||
|
"start": "node dist/index.js",
|
||||||
|
"test": "jest",
|
||||||
|
"test:watch": "jest --watch",
|
||||||
|
"lint": "eslint src --ext .ts,.tsx",
|
||||||
|
"lint:fix": "eslint src --ext .ts,.tsx --fix",
|
||||||
|
"clean": "rm -rf dist"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"express": "^4.18.2",
|
||||||
|
"cors": "^2.8.5",
|
||||||
|
"cookie-parser": "^1.4.6",
|
||||||
|
"express-rate-limit": "^7.1.5",
|
||||||
|
"rate-limiter-flexible": "^3.0.8",
|
||||||
|
"axios": "^1.6.7",
|
||||||
|
"undici": "^6.2.1",
|
||||||
|
"zod": "^3.22.4",
|
||||||
|
"@prisma/client": "*",
|
||||||
|
"argon2": "^0.31.2",
|
||||||
|
"jsonwebtoken": "^9.0.2",
|
||||||
|
"bullmq": "^4.15.4",
|
||||||
|
"ioredis": "^5.3.2",
|
||||||
|
"helmet": "^7.1.0",
|
||||||
|
"compression": "^1.7.4",
|
||||||
|
"dotenv": "^16.3.1",
|
||||||
|
"jsdom": "^23.0.1",
|
||||||
|
"winston": "^3.11.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/express": "^4.17.21",
|
||||||
|
"@types/cors": "^2.8.17",
|
||||||
|
"@types/cookie-parser": "^1.4.6",
|
||||||
|
"@types/jsonwebtoken": "^9.0.5",
|
||||||
|
"@types/compression": "^1.7.5",
|
||||||
|
"@types/jsdom": "^21.1.6",
|
||||||
|
"@types/node": "^20.10.0",
|
||||||
|
"tsx": "^4.6.2",
|
||||||
|
"typescript": "^5.3.0",
|
||||||
|
"jest": "^29.7.0",
|
||||||
|
"@types/jest": "^29.5.8",
|
||||||
|
"ts-jest": "^29.1.1",
|
||||||
|
"supertest": "^6.3.3",
|
||||||
|
"@types/supertest": "^2.0.16"
|
||||||
|
}
|
||||||
|
}
|
||||||
331
apps/api/src/index.ts
Normal file
331
apps/api/src/index.ts
Normal file
@@ -0,0 +1,331 @@
|
|||||||
|
/**
|
||||||
|
* Redirect Intelligence v2 API Server
|
||||||
|
*
|
||||||
|
* This server maintains 100% backward compatibility with existing endpoints
|
||||||
|
* while providing a foundation for new v2 features.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import 'dotenv/config';
|
||||||
|
import express from 'express';
|
||||||
|
import cors from 'cors';
|
||||||
|
import helmet from 'helmet';
|
||||||
|
import compression from 'compression';
|
||||||
|
import cookieParser from 'cookie-parser';
|
||||||
|
import rateLimit from 'express-rate-limit';
|
||||||
|
import path from 'path';
|
||||||
|
import { logger } from '@/lib/logger';
|
||||||
|
import { trackRedirects } from '@/services/redirect-legacy.service';
|
||||||
|
|
||||||
|
const app = express();
|
||||||
|
const PORT = process.env.PORT || 3333;
|
||||||
|
|
||||||
|
// Security middleware
|
||||||
|
app.use(helmet({
|
||||||
|
contentSecurityPolicy: {
|
||||||
|
directives: {
|
||||||
|
defaultSrc: ["'self'"],
|
||||||
|
styleSrc: ["'self'", "'unsafe-inline'", "https://cdn.jsdelivr.net"],
|
||||||
|
scriptSrc: ["'self'", "https://cdn.jsdelivr.net"],
|
||||||
|
imgSrc: ["'self'", "data:", "https:"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Compression middleware
|
||||||
|
app.use(compression());
|
||||||
|
|
||||||
|
// CORS middleware
|
||||||
|
app.use(cors({
|
||||||
|
origin: process.env.WEB_URL || 'http://localhost:3000',
|
||||||
|
credentials: true,
|
||||||
|
optionsSuccessStatus: 200 // Some legacy browsers (IE11, various SmartTVs) choke on 204
|
||||||
|
}));
|
||||||
|
|
||||||
|
// Body parsing middleware
|
||||||
|
app.use(express.json({ limit: '10mb' }));
|
||||||
|
app.use(express.urlencoded({ extended: true, limit: '10mb' }));
|
||||||
|
app.use(cookieParser());
|
||||||
|
|
||||||
|
// Static files (preserve existing behavior)
|
||||||
|
app.use(express.static(path.join(__dirname, '../../../public')));
|
||||||
|
|
||||||
|
// Rate limiting (EXACT same configuration as before)
|
||||||
|
const apiLimiter = rateLimit({
|
||||||
|
windowMs: 60 * 60 * 1000, // 1 hour
|
||||||
|
max: 100, // limit each IP to 100 requests per windowMs
|
||||||
|
message: { error: 'Too many requests, please try again later.' },
|
||||||
|
standardHeaders: true, // Return rate limit info in the `RateLimit-*` headers
|
||||||
|
legacyHeaders: false, // Disable the `X-RateLimit-*` headers
|
||||||
|
});
|
||||||
|
|
||||||
|
// Health check endpoint
|
||||||
|
app.get('/health', (req, res) => {
|
||||||
|
res.json({
|
||||||
|
status: 'ok',
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
version: '2.0.0',
|
||||||
|
environment: process.env.NODE_ENV || 'development'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ============================================================================
|
||||||
|
// LEGACY ENDPOINTS - EXACT SAME BEHAVIOR AS BEFORE
|
||||||
|
// ============================================================================
|
||||||
|
|
||||||
|
// Original endpoint (deprecated but maintained for backward compatibility)
|
||||||
|
app.post('/api/track', async (req, res) => {
|
||||||
|
const { url, method = 'GET', userAgent } = req.body;
|
||||||
|
|
||||||
|
if (!url) {
|
||||||
|
return res.status(400).json({ error: 'URL is required' });
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Ensure URL has a protocol
|
||||||
|
let inputUrl = url;
|
||||||
|
if (!inputUrl.startsWith('http://') && !inputUrl.startsWith('https://')) {
|
||||||
|
inputUrl = 'http://' + inputUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up request options
|
||||||
|
const options = {
|
||||||
|
method: method.toUpperCase(),
|
||||||
|
userAgent
|
||||||
|
};
|
||||||
|
|
||||||
|
const redirectChain = await trackRedirects(inputUrl, [], options);
|
||||||
|
res.json({ redirects: redirectChain });
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('Legacy /api/track error:', error);
|
||||||
|
res.status(500).json({ error: 'Failed to track redirects' });
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// API v1 track endpoint (POST)
|
||||||
|
app.post('/api/v1/track', apiLimiter, async (req, res) => {
|
||||||
|
const { url, method = 'GET', userAgent } = req.body;
|
||||||
|
|
||||||
|
if (!url) {
|
||||||
|
return res.status(400).json({
|
||||||
|
error: 'URL is required',
|
||||||
|
status: 400,
|
||||||
|
success: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Ensure URL has a protocol
|
||||||
|
let inputUrl = url;
|
||||||
|
if (!inputUrl.startsWith('http://') && !inputUrl.startsWith('https://')) {
|
||||||
|
inputUrl = 'http://' + inputUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up request options
|
||||||
|
const options = {
|
||||||
|
method: method.toUpperCase(),
|
||||||
|
userAgent
|
||||||
|
};
|
||||||
|
|
||||||
|
const redirectChain = await trackRedirects(inputUrl, [], options);
|
||||||
|
|
||||||
|
// Format the response in a more standardized API format
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
status: 200,
|
||||||
|
data: {
|
||||||
|
url: inputUrl,
|
||||||
|
method: options.method,
|
||||||
|
redirectCount: redirectChain.length - 1,
|
||||||
|
finalUrl: redirectChain[redirectChain.length - 1]?.url,
|
||||||
|
finalStatusCode: redirectChain[redirectChain.length - 1]?.statusCode,
|
||||||
|
redirects: redirectChain
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('API v1 track error:', error);
|
||||||
|
res.status(500).json({
|
||||||
|
error: 'Failed to track redirects',
|
||||||
|
message: error instanceof Error ? error.message : 'Unknown error',
|
||||||
|
status: 500,
|
||||||
|
success: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// API v1 track endpoint with GET method support (for easy browser/curl usage)
|
||||||
|
app.get('/api/v1/track', apiLimiter, async (req, res) => {
|
||||||
|
const { url, method = 'GET', userAgent } = req.query;
|
||||||
|
|
||||||
|
if (!url) {
|
||||||
|
return res.status(400).json({
|
||||||
|
error: 'URL parameter is required',
|
||||||
|
status: 400,
|
||||||
|
success: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Ensure URL has a protocol
|
||||||
|
let inputUrl = url as string;
|
||||||
|
if (!inputUrl.startsWith('http://') && !inputUrl.startsWith('https://')) {
|
||||||
|
inputUrl = 'http://' + inputUrl;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up request options
|
||||||
|
const options = {
|
||||||
|
method: ((method as string) || 'GET').toUpperCase(),
|
||||||
|
userAgent: userAgent as string
|
||||||
|
};
|
||||||
|
|
||||||
|
const redirectChain = await trackRedirects(inputUrl, [], options);
|
||||||
|
|
||||||
|
// Format the response in a more standardized API format
|
||||||
|
res.json({
|
||||||
|
success: true,
|
||||||
|
status: 200,
|
||||||
|
data: {
|
||||||
|
url: inputUrl,
|
||||||
|
method: options.method,
|
||||||
|
redirectCount: redirectChain.length - 1,
|
||||||
|
finalUrl: redirectChain[redirectChain.length - 1]?.url,
|
||||||
|
finalStatusCode: redirectChain[redirectChain.length - 1]?.statusCode,
|
||||||
|
redirects: redirectChain
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (error) {
|
||||||
|
logger.error('API v1 track GET error:', error);
|
||||||
|
res.status(500).json({
|
||||||
|
error: 'Failed to track redirects',
|
||||||
|
message: error instanceof Error ? error.message : 'Unknown error',
|
||||||
|
status: 500,
|
||||||
|
success: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// API documentation endpoint (preserve existing)
|
||||||
|
app.get('/api/docs', (req, res) => {
|
||||||
|
const apiDocs = `
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title>URL Redirect Tracker API</title>
|
||||||
|
<style>
|
||||||
|
body { font-family: Arial, sans-serif; line-height: 1.6; max-width: 800px; margin: 0 auto; padding: 20px; }
|
||||||
|
pre { background: #f4f4f4; padding: 10px; border-radius: 5px; overflow-x: auto; }
|
||||||
|
table { border-collapse: collapse; width: 100%; }
|
||||||
|
th, td { text-align: left; padding: 8px; border: 1px solid #ddd; }
|
||||||
|
th { background-color: #f2f2f2; }
|
||||||
|
.method { display: inline-block; padding: 3px 8px; border-radius: 3px; color: white; font-weight: bold; }
|
||||||
|
.get { background-color: #61affe; }
|
||||||
|
.post { background-color: #49cc90; }
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h1>URL Redirect Tracker API Documentation</h1>
|
||||||
|
<p>This API allows you to programmatically track and analyze URL redirect chains with detailed information.</p>
|
||||||
|
|
||||||
|
<h2>Rate Limiting</h2>
|
||||||
|
<p>The API is limited to 100 requests per hour per IP address.</p>
|
||||||
|
|
||||||
|
<h2>Endpoints</h2>
|
||||||
|
|
||||||
|
<h3><span class="method post">POST</span> /api/v1/track</h3>
|
||||||
|
<p>Track a URL and get the full redirect chain using a POST request.</p>
|
||||||
|
|
||||||
|
<h4>Request Parameters (JSON Body)</h4>
|
||||||
|
<table>
|
||||||
|
<tr>
|
||||||
|
<th>Parameter</th>
|
||||||
|
<th>Type</th>
|
||||||
|
<th>Required</th>
|
||||||
|
<th>Description</th>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>url</td>
|
||||||
|
<td>string</td>
|
||||||
|
<td>Yes</td>
|
||||||
|
<td>The URL to track (e.g., "example.com")</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>method</td>
|
||||||
|
<td>string</td>
|
||||||
|
<td>No</td>
|
||||||
|
<td>HTTP method (GET, HEAD, POST). Default: "GET"</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>userAgent</td>
|
||||||
|
<td>string</td>
|
||||||
|
<td>No</td>
|
||||||
|
<td>Custom User-Agent header</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
|
||||||
|
<h4>Example Request</h4>
|
||||||
|
<pre>
|
||||||
|
curl -X POST http://localhost:${PORT}/api/v1/track \\
|
||||||
|
-H "Content-Type: application/json" \\
|
||||||
|
-d '{
|
||||||
|
"url": "github.com",
|
||||||
|
"method": "GET"
|
||||||
|
}'
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<h3><span class="method get">GET</span> /api/v1/track</h3>
|
||||||
|
<p>Track a URL and get the full redirect chain using a GET request with query parameters.</p>
|
||||||
|
|
||||||
|
<h4>Example Request</h4>
|
||||||
|
<pre>
|
||||||
|
curl "http://localhost:${PORT}/api/v1/track?url=github.com&method=GET"
|
||||||
|
</pre>
|
||||||
|
|
||||||
|
<p><a href="/">Back to URL Redirect Tracker</a></p>
|
||||||
|
</body>
|
||||||
|
</html>
|
||||||
|
`;
|
||||||
|
|
||||||
|
res.send(apiDocs);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Catch-all for serving the frontend (preserve existing behavior)
|
||||||
|
app.get('/', (req, res) => {
|
||||||
|
res.sendFile(path.join(__dirname, '../../../public', 'index.html'));
|
||||||
|
});
|
||||||
|
|
||||||
|
// Global error handler
|
||||||
|
app.use((err: Error, req: express.Request, res: express.Response, next: express.NextFunction) => {
|
||||||
|
logger.error('Unhandled error:', err);
|
||||||
|
res.status(500).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Internal server error',
|
||||||
|
message: process.env.NODE_ENV === 'development' ? err.message : 'Something went wrong'
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// 404 handler
|
||||||
|
app.use((req, res) => {
|
||||||
|
res.status(404).json({
|
||||||
|
success: false,
|
||||||
|
error: 'Not found',
|
||||||
|
message: `Route ${req.method} ${req.path} not found`
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Graceful shutdown
|
||||||
|
process.on('SIGTERM', () => {
|
||||||
|
logger.info('SIGTERM received, shutting down gracefully');
|
||||||
|
process.exit(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
process.on('SIGINT', () => {
|
||||||
|
logger.info('SIGINT received, shutting down gracefully');
|
||||||
|
process.exit(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.listen(PORT, () => {
|
||||||
|
logger.info(`🚀 Redirect Intelligence v2 API Server running on http://localhost:${PORT}`);
|
||||||
|
logger.info(`📖 API Documentation: http://localhost:${PORT}/api/docs`);
|
||||||
|
logger.info(`🏥 Health Check: http://localhost:${PORT}/health`);
|
||||||
|
});
|
||||||
|
|
||||||
|
export default app;
|
||||||
30
apps/api/src/lib/logger.ts
Normal file
30
apps/api/src/lib/logger.ts
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
import winston from 'winston';
|
||||||
|
|
||||||
|
const logger = winston.createLogger({
|
||||||
|
level: process.env.LOG_LEVEL || 'info',
|
||||||
|
format: winston.format.combine(
|
||||||
|
winston.format.timestamp(),
|
||||||
|
winston.format.errors({ stack: true }),
|
||||||
|
winston.format.json()
|
||||||
|
),
|
||||||
|
defaultMeta: { service: 'redirect-intelligence-api' },
|
||||||
|
transports: [
|
||||||
|
// Write all logs with importance level of `error` or less to `error.log`
|
||||||
|
new winston.transports.File({ filename: 'logs/error.log', level: 'error' }),
|
||||||
|
// Write all logs with importance level of `info` or less to `combined.log`
|
||||||
|
new winston.transports.File({ filename: 'logs/combined.log' }),
|
||||||
|
],
|
||||||
|
});
|
||||||
|
|
||||||
|
// If we're not in production then log to the `console` with the format:
|
||||||
|
// `${info.level}: ${info.message} JSON.stringify({ ...rest }) `
|
||||||
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
|
logger.add(new winston.transports.Console({
|
||||||
|
format: winston.format.combine(
|
||||||
|
winston.format.colorize(),
|
||||||
|
winston.format.simple()
|
||||||
|
)
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
|
export { logger };
|
||||||
17
apps/api/src/lib/prisma.ts
Normal file
17
apps/api/src/lib/prisma.ts
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
import { PrismaClient } from '@prisma/client';
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
// eslint-disable-next-line no-var
|
||||||
|
var __prisma: PrismaClient | undefined;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prevent multiple instances of Prisma Client in development
|
||||||
|
export const prisma =
|
||||||
|
globalThis.__prisma ??
|
||||||
|
new PrismaClient({
|
||||||
|
log: process.env.NODE_ENV === 'development' ? ['query', 'error', 'warn'] : ['error'],
|
||||||
|
});
|
||||||
|
|
||||||
|
if (process.env.NODE_ENV !== 'production') {
|
||||||
|
globalThis.__prisma = prisma;
|
||||||
|
}
|
||||||
223
apps/api/src/services/redirect-legacy.service.ts
Normal file
223
apps/api/src/services/redirect-legacy.service.ts
Normal file
@@ -0,0 +1,223 @@
|
|||||||
|
/**
|
||||||
|
* Legacy Redirect Service
|
||||||
|
*
|
||||||
|
* This service contains the EXACT same logic as the original index.js
|
||||||
|
* to ensure 100% backward compatibility for existing endpoints.
|
||||||
|
*
|
||||||
|
* DO NOT MODIFY - This preserves existing behavior exactly.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import axios from 'axios';
|
||||||
|
import https from 'https';
|
||||||
|
import { logger } from '@/lib/logger';
|
||||||
|
|
||||||
|
export interface LegacyRedirectOptions {
|
||||||
|
method?: string;
|
||||||
|
userAgent?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LegacyRedirectResult {
|
||||||
|
url: string;
|
||||||
|
timestamp: number;
|
||||||
|
isSSL: boolean;
|
||||||
|
duration?: number;
|
||||||
|
statusCode?: number;
|
||||||
|
statusText?: string;
|
||||||
|
metadata?: any;
|
||||||
|
responseBody?: string;
|
||||||
|
sslInfo?: any;
|
||||||
|
error?: string;
|
||||||
|
final?: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* EXACT replica of the original trackRedirects function from index.js
|
||||||
|
* This ensures 100% backward compatibility
|
||||||
|
*/
|
||||||
|
export async function trackRedirects(
|
||||||
|
url: string,
|
||||||
|
redirects: LegacyRedirectResult[] = [],
|
||||||
|
options: LegacyRedirectOptions = {}
|
||||||
|
): Promise<LegacyRedirectResult[]> {
|
||||||
|
const startTime = Date.now();
|
||||||
|
const currentRedirect: LegacyRedirectResult = {
|
||||||
|
url,
|
||||||
|
timestamp: startTime,
|
||||||
|
isSSL: url.toLowerCase().startsWith('https://')
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add the current URL to the redirects array
|
||||||
|
if (redirects.length === 0) {
|
||||||
|
redirects.push(currentRedirect);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Prepare request config
|
||||||
|
const config = {
|
||||||
|
method: options.method || 'GET',
|
||||||
|
url: url,
|
||||||
|
maxRedirects: 0,
|
||||||
|
validateStatus: (status: number) => status >= 200 && status < 600,
|
||||||
|
timeout: 15000,
|
||||||
|
responseType: 'text' as const,
|
||||||
|
decompress: true,
|
||||||
|
headers: {} as Record<string, string>
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add user agent if provided
|
||||||
|
if (options.userAgent) {
|
||||||
|
config.headers['User-Agent'] = options.userAgent;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add HTTPS agent for SSL info
|
||||||
|
config.httpsAgent = new https.Agent({
|
||||||
|
rejectUnauthorized: false, // Allow self-signed certs for testing
|
||||||
|
checkServerIdentity: (host, cert) => {
|
||||||
|
// Capture certificate info
|
||||||
|
return undefined; // Allow connection
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Make the request
|
||||||
|
const response = await axios(config);
|
||||||
|
|
||||||
|
// Calculate the duration for this request
|
||||||
|
const endTime = Date.now();
|
||||||
|
|
||||||
|
// Get response metadata
|
||||||
|
const metadata = {
|
||||||
|
status: response.status,
|
||||||
|
statusText: response.statusText,
|
||||||
|
headers: response.headers,
|
||||||
|
contentType: response.headers['content-type'],
|
||||||
|
contentLength: response.headers['content-length'],
|
||||||
|
server: response.headers['server'],
|
||||||
|
date: response.headers['date'],
|
||||||
|
protocol: response.request?.protocol,
|
||||||
|
method: config.method
|
||||||
|
};
|
||||||
|
|
||||||
|
// Get SSL certificate info if HTTPS
|
||||||
|
let sslInfo = null;
|
||||||
|
if (url.toLowerCase().startsWith('https://')) {
|
||||||
|
try {
|
||||||
|
const urlObj = new URL(url);
|
||||||
|
const socket = (response.request as any)?.socket;
|
||||||
|
|
||||||
|
if (socket && socket.getPeerCertificate) {
|
||||||
|
const cert = socket.getPeerCertificate(true);
|
||||||
|
sslInfo = {
|
||||||
|
valid: socket.authorized,
|
||||||
|
issuer: cert.issuer,
|
||||||
|
subject: cert.subject,
|
||||||
|
validFrom: cert.valid_from,
|
||||||
|
validTo: cert.valid_to,
|
||||||
|
fingerprint: cert.fingerprint
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
sslInfo = { error: 'Failed to retrieve SSL info' };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get response body (truncate if too large)
|
||||||
|
let responseBody = response.data;
|
||||||
|
if (typeof responseBody === 'string' && responseBody.length > 5000) {
|
||||||
|
responseBody = responseBody.substring(0, 5000) + '... [truncated]';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the current redirect with all the detailed info
|
||||||
|
if (redirects.length > 0 && redirects[redirects.length - 1]!.url === url) {
|
||||||
|
redirects[redirects.length - 1]!.duration = endTime - startTime;
|
||||||
|
redirects[redirects.length - 1]!.statusCode = response.status;
|
||||||
|
redirects[redirects.length - 1]!.statusText = response.statusText;
|
||||||
|
redirects[redirects.length - 1]!.metadata = metadata;
|
||||||
|
redirects[redirects.length - 1]!.responseBody = responseBody;
|
||||||
|
redirects[redirects.length - 1]!.sslInfo = sslInfo;
|
||||||
|
} else {
|
||||||
|
currentRedirect.duration = endTime - startTime;
|
||||||
|
currentRedirect.statusCode = response.status;
|
||||||
|
currentRedirect.statusText = response.statusText;
|
||||||
|
currentRedirect.metadata = metadata;
|
||||||
|
currentRedirect.responseBody = responseBody;
|
||||||
|
currentRedirect.sslInfo = sslInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if we have a redirect
|
||||||
|
if (response.status >= 300 && response.status < 400 && response.headers.location) {
|
||||||
|
// Get the next URL
|
||||||
|
let nextUrl = response.headers.location;
|
||||||
|
|
||||||
|
// Handle relative URLs
|
||||||
|
if (!nextUrl.startsWith('http')) {
|
||||||
|
const baseUrl = new URL(url);
|
||||||
|
nextUrl = new URL(nextUrl, baseUrl.origin).href;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create the next redirect object
|
||||||
|
const nextRedirect: LegacyRedirectResult = {
|
||||||
|
url: nextUrl,
|
||||||
|
timestamp: endTime,
|
||||||
|
isSSL: nextUrl.toLowerCase().startsWith('https://')
|
||||||
|
};
|
||||||
|
|
||||||
|
// Add to redirects array
|
||||||
|
redirects.push(nextRedirect);
|
||||||
|
|
||||||
|
// Continue following redirects (but always use GET for subsequent requests per HTTP spec)
|
||||||
|
const nextOptions = { ...options, method: 'GET' };
|
||||||
|
return await trackRedirects(nextUrl, redirects, nextOptions);
|
||||||
|
} else {
|
||||||
|
// This is the final page
|
||||||
|
if (redirects.length > 0 && redirects[redirects.length - 1]!.url === url) {
|
||||||
|
redirects[redirects.length - 1]!.final = true;
|
||||||
|
} else {
|
||||||
|
currentRedirect.final = true;
|
||||||
|
redirects.push(currentRedirect);
|
||||||
|
}
|
||||||
|
return redirects;
|
||||||
|
}
|
||||||
|
} catch (error: any) {
|
||||||
|
// Handle errors
|
||||||
|
const endTime = Date.now();
|
||||||
|
|
||||||
|
// Update the current redirect with the duration and error info
|
||||||
|
if (redirects.length > 0 && redirects[redirects.length - 1]!.url === url) {
|
||||||
|
redirects[redirects.length - 1]!.duration = endTime - startTime;
|
||||||
|
redirects[redirects.length - 1]!.error = error.message;
|
||||||
|
redirects[redirects.length - 1]!.final = true;
|
||||||
|
|
||||||
|
// Try to get response info from error object if available
|
||||||
|
if (error.response) {
|
||||||
|
redirects[redirects.length - 1]!.statusCode = error.response.status;
|
||||||
|
redirects[redirects.length - 1]!.statusText = error.response.statusText;
|
||||||
|
redirects[redirects.length - 1]!.metadata = {
|
||||||
|
status: error.response.status,
|
||||||
|
statusText: error.response.statusText,
|
||||||
|
headers: error.response.headers,
|
||||||
|
method: options.method
|
||||||
|
};
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
currentRedirect.duration = endTime - startTime;
|
||||||
|
currentRedirect.error = error.message;
|
||||||
|
currentRedirect.final = true;
|
||||||
|
|
||||||
|
// Try to get response info from error object if available
|
||||||
|
if (error.response) {
|
||||||
|
currentRedirect.statusCode = error.response.status;
|
||||||
|
currentRedirect.statusText = error.response.statusText;
|
||||||
|
currentRedirect.metadata = {
|
||||||
|
status: error.response.status,
|
||||||
|
statusText: error.response.statusText,
|
||||||
|
headers: error.response.headers,
|
||||||
|
method: options.method
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
redirects.push(currentRedirect);
|
||||||
|
}
|
||||||
|
|
||||||
|
return redirects;
|
||||||
|
}
|
||||||
|
}
|
||||||
46
apps/api/tsconfig.json
Normal file
46
apps/api/tsconfig.json
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES2022",
|
||||||
|
"module": "commonjs",
|
||||||
|
"lib": ["ES2022"],
|
||||||
|
"outDir": "./dist",
|
||||||
|
"rootDir": "./src",
|
||||||
|
"strict": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"resolveJsonModule": true,
|
||||||
|
"declaration": true,
|
||||||
|
"declarationMap": true,
|
||||||
|
"sourceMap": true,
|
||||||
|
"removeComments": true,
|
||||||
|
"noImplicitAny": true,
|
||||||
|
"strictNullChecks": true,
|
||||||
|
"strictFunctionTypes": true,
|
||||||
|
"noImplicitThis": true,
|
||||||
|
"noImplicitReturns": true,
|
||||||
|
"noFallthroughCasesInSwitch": true,
|
||||||
|
"noUncheckedIndexedAccess": true,
|
||||||
|
"noImplicitOverride": true,
|
||||||
|
"allowUnusedLabels": false,
|
||||||
|
"allowUnreachableCode": false,
|
||||||
|
"exactOptionalPropertyTypes": true,
|
||||||
|
"baseUrl": ".",
|
||||||
|
"paths": {
|
||||||
|
"@/*": ["./src/*"],
|
||||||
|
"@/lib/*": ["./src/lib/*"],
|
||||||
|
"@/types/*": ["./src/types/*"],
|
||||||
|
"@/services/*": ["./src/services/*"],
|
||||||
|
"@/routes/*": ["./src/routes/*"],
|
||||||
|
"@/middleware/*": ["./src/middleware/*"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"src/**/*"
|
||||||
|
],
|
||||||
|
"exclude": [
|
||||||
|
"node_modules",
|
||||||
|
"dist",
|
||||||
|
"**/*.test.ts"
|
||||||
|
]
|
||||||
|
}
|
||||||
63
apps/web/Dockerfile
Normal file
63
apps/web/Dockerfile
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
# Multi-stage build for production optimization
|
||||||
|
FROM node:20-alpine AS base
|
||||||
|
|
||||||
|
# Install dependencies only when needed
|
||||||
|
FROM base AS deps
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy package files
|
||||||
|
COPY package*.json ./
|
||||||
|
COPY apps/web/package*.json ./apps/web/
|
||||||
|
COPY packages/shared/package*.json ./packages/shared/
|
||||||
|
|
||||||
|
# Install dependencies
|
||||||
|
RUN npm ci --only=production && npm cache clean --force
|
||||||
|
|
||||||
|
# Development stage
|
||||||
|
FROM base AS dev
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy package files
|
||||||
|
COPY package*.json ./
|
||||||
|
COPY apps/web/package*.json ./apps/web/
|
||||||
|
COPY packages/shared/package*.json ./packages/shared/
|
||||||
|
|
||||||
|
# Install all dependencies including devDependencies
|
||||||
|
RUN npm ci
|
||||||
|
|
||||||
|
# Copy source code
|
||||||
|
COPY apps/web ./apps/web
|
||||||
|
COPY packages/shared ./packages/shared
|
||||||
|
|
||||||
|
WORKDIR /app/apps/web
|
||||||
|
|
||||||
|
EXPOSE 3000
|
||||||
|
|
||||||
|
CMD ["npm", "run", "dev"]
|
||||||
|
|
||||||
|
# Build stage
|
||||||
|
FROM base AS builder
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy everything needed for build
|
||||||
|
COPY package*.json ./
|
||||||
|
COPY apps/web ./apps/web
|
||||||
|
COPY packages/shared ./packages/shared
|
||||||
|
|
||||||
|
# Install dependencies and build
|
||||||
|
RUN npm ci
|
||||||
|
WORKDIR /app/apps/web
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
# Production stage
|
||||||
|
FROM nginx:alpine AS production
|
||||||
|
|
||||||
|
# Copy built application
|
||||||
|
COPY --from=builder /app/apps/web/dist /usr/share/nginx/html
|
||||||
|
|
||||||
|
# Copy nginx configuration
|
||||||
|
COPY apps/web/nginx.conf /etc/nginx/nginx.conf
|
||||||
|
|
||||||
|
EXPOSE 3000
|
||||||
|
|
||||||
|
CMD ["nginx", "-g", "daemon off;"]
|
||||||
54
apps/web/nginx.conf
Normal file
54
apps/web/nginx.conf
Normal file
@@ -0,0 +1,54 @@
|
|||||||
|
events {
|
||||||
|
worker_connections 1024;
|
||||||
|
}
|
||||||
|
|
||||||
|
http {
|
||||||
|
include /etc/nginx/mime.types;
|
||||||
|
default_type application/octet-stream;
|
||||||
|
|
||||||
|
server {
|
||||||
|
listen 3000;
|
||||||
|
server_name localhost;
|
||||||
|
root /usr/share/nginx/html;
|
||||||
|
index index.html index.htm;
|
||||||
|
|
||||||
|
# Handle client-side routing
|
||||||
|
location / {
|
||||||
|
try_files $uri $uri/ /index.html;
|
||||||
|
}
|
||||||
|
|
||||||
|
# API proxy
|
||||||
|
location /api/ {
|
||||||
|
proxy_pass http://api:3333;
|
||||||
|
proxy_http_version 1.1;
|
||||||
|
proxy_set_header Upgrade $http_upgrade;
|
||||||
|
proxy_set_header Connection 'upgrade';
|
||||||
|
proxy_set_header Host $host;
|
||||||
|
proxy_set_header X-Real-IP $remote_addr;
|
||||||
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
||||||
|
proxy_set_header X-Forwarded-Proto $scheme;
|
||||||
|
proxy_cache_bypass $http_upgrade;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Health check
|
||||||
|
location /health {
|
||||||
|
access_log off;
|
||||||
|
return 200 "healthy\n";
|
||||||
|
add_header Content-Type text/plain;
|
||||||
|
}
|
||||||
|
|
||||||
|
# Security headers
|
||||||
|
add_header X-Frame-Options "SAMEORIGIN" always;
|
||||||
|
add_header X-XSS-Protection "1; mode=block" always;
|
||||||
|
add_header X-Content-Type-Options "nosniff" always;
|
||||||
|
add_header Referrer-Policy "no-referrer-when-downgrade" always;
|
||||||
|
add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; style-src 'self' 'unsafe-inline' https://cdn.jsdelivr.net; img-src 'self' data: https:; font-src 'self';" always;
|
||||||
|
|
||||||
|
# Gzip compression
|
||||||
|
gzip on;
|
||||||
|
gzip_vary on;
|
||||||
|
gzip_min_length 1024;
|
||||||
|
gzip_proxied expired no-cache no-store private must-revalidate auth;
|
||||||
|
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml+rss application/javascript;
|
||||||
|
}
|
||||||
|
}
|
||||||
59
apps/web/package.json
Normal file
59
apps/web/package.json
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
{
|
||||||
|
"name": "@redirect-intelligence/web",
|
||||||
|
"version": "2.0.0",
|
||||||
|
"private": true,
|
||||||
|
"dependencies": {
|
||||||
|
"react": "^18.2.0",
|
||||||
|
"react-dom": "^18.2.0",
|
||||||
|
"@chakra-ui/react": "^2.8.2",
|
||||||
|
"@chakra-ui/icons": "^2.1.1",
|
||||||
|
"@emotion/react": "^11.11.1",
|
||||||
|
"@emotion/styled": "^11.11.0",
|
||||||
|
"framer-motion": "^10.16.16",
|
||||||
|
"@tanstack/react-query": "^5.17.9",
|
||||||
|
"@tanstack/react-query-devtools": "^5.17.9",
|
||||||
|
"react-router-dom": "^6.20.1",
|
||||||
|
"mermaid": "^10.6.1",
|
||||||
|
"axios": "^1.6.7",
|
||||||
|
"react-hook-form": "^7.48.2",
|
||||||
|
"@hookform/resolvers": "^3.3.2",
|
||||||
|
"zod": "^3.22.4",
|
||||||
|
"react-dropzone": "^14.2.3",
|
||||||
|
"date-fns": "^3.0.6"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/react": "^18.2.43",
|
||||||
|
"@types/react-dom": "^18.2.17",
|
||||||
|
"@types/node": "^20.10.0",
|
||||||
|
"@vitejs/plugin-react": "^4.2.1",
|
||||||
|
"typescript": "^5.3.0",
|
||||||
|
"vite": "^5.0.8",
|
||||||
|
"eslint": "^8.55.0",
|
||||||
|
"@typescript-eslint/eslint-plugin": "^6.14.0",
|
||||||
|
"@typescript-eslint/parser": "^6.14.0",
|
||||||
|
"eslint-plugin-react": "^7.33.2",
|
||||||
|
"eslint-plugin-react-hooks": "^4.6.0"
|
||||||
|
},
|
||||||
|
"scripts": {
|
||||||
|
"dev": "vite",
|
||||||
|
"build": "tsc && vite build",
|
||||||
|
"preview": "vite preview",
|
||||||
|
"lint": "eslint src --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
|
||||||
|
"lint:fix": "eslint src --ext ts,tsx --fix",
|
||||||
|
"test": "vitest",
|
||||||
|
"test:ui": "vitest --ui",
|
||||||
|
"clean": "rm -rf dist"
|
||||||
|
},
|
||||||
|
"browserslist": {
|
||||||
|
"production": [
|
||||||
|
">0.2%",
|
||||||
|
"not dead",
|
||||||
|
"not op_mini all"
|
||||||
|
],
|
||||||
|
"development": [
|
||||||
|
"last 1 chrome version",
|
||||||
|
"last 1 firefox version",
|
||||||
|
"last 1 safari version"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
36
apps/web/tsconfig.json
Normal file
36
apps/web/tsconfig.json
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES2020",
|
||||||
|
"useDefineForClassFields": true,
|
||||||
|
"lib": ["ES2020", "DOM", "DOM.Iterable"],
|
||||||
|
"module": "ESNext",
|
||||||
|
"skipLibCheck": true,
|
||||||
|
|
||||||
|
/* Bundler mode */
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"allowImportingTsExtensions": true,
|
||||||
|
"resolveJsonModule": true,
|
||||||
|
"isolatedModules": true,
|
||||||
|
"noEmit": true,
|
||||||
|
"jsx": "react-jsx",
|
||||||
|
|
||||||
|
/* Linting */
|
||||||
|
"strict": true,
|
||||||
|
"noUnusedLocals": true,
|
||||||
|
"noUnusedParameters": true,
|
||||||
|
"noFallthroughCasesInSwitch": true,
|
||||||
|
|
||||||
|
/* Path mapping */
|
||||||
|
"baseUrl": ".",
|
||||||
|
"paths": {
|
||||||
|
"@/*": ["./src/*"],
|
||||||
|
"@/components/*": ["./src/components/*"],
|
||||||
|
"@/pages/*": ["./src/pages/*"],
|
||||||
|
"@/hooks/*": ["./src/hooks/*"],
|
||||||
|
"@/types/*": ["./src/types/*"],
|
||||||
|
"@/lib/*": ["./src/lib/*"]
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"include": ["src"],
|
||||||
|
"references": [{ "path": "./tsconfig.node.json" }]
|
||||||
|
}
|
||||||
10
apps/web/tsconfig.node.json
Normal file
10
apps/web/tsconfig.node.json
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"composite": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"module": "ESNext",
|
||||||
|
"moduleResolution": "bundler",
|
||||||
|
"allowSyntheticDefaultImports": true
|
||||||
|
},
|
||||||
|
"include": ["vite.config.ts"]
|
||||||
|
}
|
||||||
27
apps/web/vite.config.ts
Normal file
27
apps/web/vite.config.ts
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
import { defineConfig } from 'vite'
|
||||||
|
import react from '@vitejs/plugin-react'
|
||||||
|
import path from 'path'
|
||||||
|
|
||||||
|
// https://vitejs.dev/config/
|
||||||
|
export default defineConfig({
|
||||||
|
plugins: [react()],
|
||||||
|
resolve: {
|
||||||
|
alias: {
|
||||||
|
'@': path.resolve(__dirname, './src'),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
server: {
|
||||||
|
port: 3000,
|
||||||
|
host: '0.0.0.0',
|
||||||
|
proxy: {
|
||||||
|
'/api': {
|
||||||
|
target: 'http://localhost:3333',
|
||||||
|
changeOrigin: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
build: {
|
||||||
|
outDir: 'dist',
|
||||||
|
sourcemap: true,
|
||||||
|
},
|
||||||
|
})
|
||||||
61
apps/worker/Dockerfile
Normal file
61
apps/worker/Dockerfile
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
# Multi-stage build for production optimization
|
||||||
|
FROM node:20-alpine AS base
|
||||||
|
|
||||||
|
# Install Playwright dependencies
|
||||||
|
RUN apk add --no-cache \
|
||||||
|
chromium \
|
||||||
|
nss \
|
||||||
|
freetype \
|
||||||
|
freetype-dev \
|
||||||
|
harfbuzz \
|
||||||
|
ca-certificates \
|
||||||
|
ttf-freefont
|
||||||
|
|
||||||
|
# Tell Playwright to use the installed chromium
|
||||||
|
ENV PLAYWRIGHT_SKIP_BROWSER_DOWNLOAD=1
|
||||||
|
ENV PLAYWRIGHT_CHROMIUM_EXECUTABLE_PATH=/usr/bin/chromium-browser
|
||||||
|
|
||||||
|
# Development stage
|
||||||
|
FROM base AS dev
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy package files
|
||||||
|
COPY package*.json ./
|
||||||
|
COPY apps/worker/package*.json ./apps/worker/
|
||||||
|
COPY packages/database/package*.json ./packages/database/
|
||||||
|
COPY packages/shared/package*.json ./packages/shared/
|
||||||
|
|
||||||
|
# Install all dependencies
|
||||||
|
RUN npm ci
|
||||||
|
|
||||||
|
# Copy source code
|
||||||
|
COPY apps/worker ./apps/worker
|
||||||
|
COPY packages/database ./packages/database
|
||||||
|
COPY packages/shared ./packages/shared
|
||||||
|
|
||||||
|
WORKDIR /app/apps/worker
|
||||||
|
|
||||||
|
CMD ["npm", "run", "dev"]
|
||||||
|
|
||||||
|
# Production stage
|
||||||
|
FROM base AS production
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
# Copy package files and install dependencies
|
||||||
|
COPY package*.json ./
|
||||||
|
COPY apps/worker ./apps/worker
|
||||||
|
COPY packages/database ./packages/database
|
||||||
|
COPY packages/shared ./packages/shared
|
||||||
|
|
||||||
|
# Install dependencies and build
|
||||||
|
RUN npm ci --only=production
|
||||||
|
WORKDIR /app/apps/worker
|
||||||
|
RUN npm run build
|
||||||
|
|
||||||
|
# Create non-root user
|
||||||
|
RUN addgroup --system --gid 1001 nodejs
|
||||||
|
RUN adduser --system --uid 1001 nodejs
|
||||||
|
|
||||||
|
USER nodejs
|
||||||
|
|
||||||
|
CMD ["npm", "start"]
|
||||||
31
apps/worker/package.json
Normal file
31
apps/worker/package.json
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
{
|
||||||
|
"name": "@redirect-intelligence/worker",
|
||||||
|
"version": "2.0.0",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"dev": "tsx watch --clear-screen=false src/index.ts",
|
||||||
|
"build": "tsc",
|
||||||
|
"start": "node dist/index.js",
|
||||||
|
"test": "jest",
|
||||||
|
"lint": "eslint src --ext .ts",
|
||||||
|
"lint:fix": "eslint src --ext .ts --fix",
|
||||||
|
"clean": "rm -rf dist"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"bullmq": "^4.15.4",
|
||||||
|
"ioredis": "^5.3.2",
|
||||||
|
"@prisma/client": "*",
|
||||||
|
"axios": "^1.6.7",
|
||||||
|
"playwright": "^1.40.1",
|
||||||
|
"dotenv": "^16.3.1",
|
||||||
|
"winston": "^3.11.0"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"@types/node": "^20.10.0",
|
||||||
|
"tsx": "^4.6.2",
|
||||||
|
"typescript": "^5.3.0",
|
||||||
|
"jest": "^29.7.0",
|
||||||
|
"@types/jest": "^29.5.8",
|
||||||
|
"ts-jest": "^29.1.1"
|
||||||
|
}
|
||||||
|
}
|
||||||
15
apps/worker/tsconfig.json
Normal file
15
apps/worker/tsconfig.json
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
{
|
||||||
|
"extends": "../api/tsconfig.json",
|
||||||
|
"compilerOptions": {
|
||||||
|
"outDir": "./dist",
|
||||||
|
"rootDir": "./src"
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"src/**/*"
|
||||||
|
],
|
||||||
|
"exclude": [
|
||||||
|
"node_modules",
|
||||||
|
"dist",
|
||||||
|
"**/*.test.ts"
|
||||||
|
]
|
||||||
|
}
|
||||||
29
docker-compose.dev.yml
Normal file
29
docker-compose.dev.yml
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
api:
|
||||||
|
volumes:
|
||||||
|
- ./apps/api:/app
|
||||||
|
- /app/node_modules
|
||||||
|
environment:
|
||||||
|
- NODE_ENV=development
|
||||||
|
- DEBUG=redirect:*
|
||||||
|
command: npm run dev
|
||||||
|
|
||||||
|
web:
|
||||||
|
volumes:
|
||||||
|
- ./apps/web:/app
|
||||||
|
- /app/node_modules
|
||||||
|
environment:
|
||||||
|
- NODE_ENV=development
|
||||||
|
- CHOKIDAR_USEPOLLING=true
|
||||||
|
command: npm run dev
|
||||||
|
|
||||||
|
worker:
|
||||||
|
volumes:
|
||||||
|
- ./apps/worker:/app
|
||||||
|
- /app/node_modules
|
||||||
|
environment:
|
||||||
|
- NODE_ENV=development
|
||||||
|
- DEBUG=worker:*
|
||||||
|
command: npm run dev
|
||||||
89
docker-compose.yml
Normal file
89
docker-compose.yml
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
version: '3.8'
|
||||||
|
|
||||||
|
services:
|
||||||
|
postgres:
|
||||||
|
image: postgres:15
|
||||||
|
environment:
|
||||||
|
POSTGRES_DB: redirect_intelligence
|
||||||
|
POSTGRES_USER: postgres
|
||||||
|
POSTGRES_PASSWORD: postgres
|
||||||
|
ports:
|
||||||
|
- "5432:5432"
|
||||||
|
volumes:
|
||||||
|
- postgres_data:/var/lib/postgresql/data
|
||||||
|
- ./packages/database/init.sql:/docker-entrypoint-initdb.d/init.sql
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD-SHELL", "pg_isready -U postgres"]
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: redis:7-alpine
|
||||||
|
ports:
|
||||||
|
- "6379:6379"
|
||||||
|
volumes:
|
||||||
|
- redis_data:/data
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "redis-cli", "ping"]
|
||||||
|
interval: 10s
|
||||||
|
timeout: 5s
|
||||||
|
retries: 5
|
||||||
|
|
||||||
|
api:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: apps/api/Dockerfile
|
||||||
|
ports:
|
||||||
|
- "3333:3333"
|
||||||
|
environment:
|
||||||
|
- DATABASE_URL=postgresql://postgres:postgres@postgres:5432/redirect_intelligence
|
||||||
|
- REDIS_URL=redis://redis:6379
|
||||||
|
- NODE_ENV=development
|
||||||
|
- PORT=3333
|
||||||
|
- JWT_SECRET=your-super-secret-jwt-key-change-in-production
|
||||||
|
- WEB_URL=http://localhost:3000
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
redis:
|
||||||
|
condition: service_healthy
|
||||||
|
restart: unless-stopped
|
||||||
|
healthcheck:
|
||||||
|
test: ["CMD", "curl", "-f", "http://localhost:3333/health"]
|
||||||
|
interval: 30s
|
||||||
|
timeout: 10s
|
||||||
|
retries: 3
|
||||||
|
|
||||||
|
web:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: apps/web/Dockerfile
|
||||||
|
ports:
|
||||||
|
- "3000:3000"
|
||||||
|
environment:
|
||||||
|
- REACT_APP_API_URL=http://localhost:3333
|
||||||
|
- NODE_ENV=development
|
||||||
|
depends_on:
|
||||||
|
api:
|
||||||
|
condition: service_healthy
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
worker:
|
||||||
|
build:
|
||||||
|
context: .
|
||||||
|
dockerfile: apps/worker/Dockerfile
|
||||||
|
environment:
|
||||||
|
- DATABASE_URL=postgresql://postgres:postgres@postgres:5432/redirect_intelligence
|
||||||
|
- REDIS_URL=redis://redis:6379
|
||||||
|
- NODE_ENV=development
|
||||||
|
depends_on:
|
||||||
|
postgres:
|
||||||
|
condition: service_healthy
|
||||||
|
redis:
|
||||||
|
condition: service_healthy
|
||||||
|
restart: unless-stopped
|
||||||
|
|
||||||
|
volumes:
|
||||||
|
postgres_data:
|
||||||
|
redis_data:
|
||||||
41
package.json
41
package.json
@@ -1,18 +1,31 @@
|
|||||||
{
|
{
|
||||||
"name": "catch_redirect",
|
"name": "redirect-intelligence-v2",
|
||||||
"version": "1.0.0",
|
"private": true,
|
||||||
"main": "index.js",
|
"workspaces": [
|
||||||
|
"apps/*",
|
||||||
|
"packages/*"
|
||||||
|
],
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node index.js",
|
"dev": "docker-compose -f docker-compose.yml -f docker-compose.dev.yml up --build",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"dev:api": "cd apps/api && npm run dev",
|
||||||
|
"dev:web": "cd apps/web && npm run dev",
|
||||||
|
"build": "turbo run build",
|
||||||
|
"test": "turbo run test",
|
||||||
|
"lint": "turbo run lint",
|
||||||
|
"db:migrate": "cd packages/database && npx prisma migrate dev",
|
||||||
|
"db:seed": "cd packages/database && npx prisma db seed",
|
||||||
|
"db:studio": "cd packages/database && npx prisma studio",
|
||||||
|
"clean": "turbo run clean && rm -rf node_modules"
|
||||||
},
|
},
|
||||||
"keywords": [],
|
"devDependencies": {
|
||||||
"author": "",
|
"turbo": "^1.13.0",
|
||||||
"license": "ISC",
|
"typescript": "^5.3.0",
|
||||||
"description": "",
|
"@types/node": "^20.10.0",
|
||||||
"dependencies": {
|
"prettier": "^3.1.0",
|
||||||
"axios": "^1.6.7",
|
"eslint": "^8.55.0"
|
||||||
"express": "^4.18.2",
|
},
|
||||||
"express-rate-limit": "^5.5.1"
|
"engines": {
|
||||||
|
"node": ">=20.0.0",
|
||||||
|
"npm": ">=10.0.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
25
packages/database/package.json
Normal file
25
packages/database/package.json
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
{
|
||||||
|
"name": "@redirect-intelligence/database",
|
||||||
|
"version": "2.0.0",
|
||||||
|
"private": true,
|
||||||
|
"scripts": {
|
||||||
|
"db:generate": "prisma generate",
|
||||||
|
"db:migrate": "prisma migrate dev",
|
||||||
|
"db:deploy": "prisma migrate deploy",
|
||||||
|
"db:studio": "prisma studio",
|
||||||
|
"db:seed": "tsx prisma/seed.ts",
|
||||||
|
"db:reset": "prisma migrate reset"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"@prisma/client": "^5.7.1"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"prisma": "^5.7.1",
|
||||||
|
"tsx": "^4.6.2",
|
||||||
|
"@types/node": "^20.10.0",
|
||||||
|
"typescript": "^5.3.0"
|
||||||
|
},
|
||||||
|
"prisma": {
|
||||||
|
"seed": "tsx prisma/seed.ts"
|
||||||
|
}
|
||||||
|
}
|
||||||
21
packages/shared/package.json
Normal file
21
packages/shared/package.json
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
{
|
||||||
|
"name": "@redirect-intelligence/shared",
|
||||||
|
"version": "2.0.0",
|
||||||
|
"private": true,
|
||||||
|
"main": "dist/index.js",
|
||||||
|
"types": "dist/index.d.ts",
|
||||||
|
"scripts": {
|
||||||
|
"build": "tsc",
|
||||||
|
"dev": "tsc --watch",
|
||||||
|
"clean": "rm -rf dist",
|
||||||
|
"lint": "eslint src --ext .ts",
|
||||||
|
"lint:fix": "eslint src --ext .ts --fix"
|
||||||
|
},
|
||||||
|
"dependencies": {
|
||||||
|
"zod": "^3.22.4"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"typescript": "^5.3.0",
|
||||||
|
"@types/node": "^20.10.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
18
packages/shared/src/index.ts
Normal file
18
packages/shared/src/index.ts
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
/**
|
||||||
|
* Shared utilities and types for Redirect Intelligence v2
|
||||||
|
*/
|
||||||
|
|
||||||
|
export * from './types/api';
|
||||||
|
|
||||||
|
export const constants = {
|
||||||
|
API_BASE_URL: process.env.REACT_APP_API_URL || 'http://localhost:3333',
|
||||||
|
RATE_LIMIT: {
|
||||||
|
WINDOW_MS: 60 * 60 * 1000, // 1 hour
|
||||||
|
MAX_REQUESTS: 100,
|
||||||
|
},
|
||||||
|
REDIRECT_LIMITS: {
|
||||||
|
MAX_HOPS: 20,
|
||||||
|
TIMEOUT_MS: 15000,
|
||||||
|
RESPONSE_BODY_LIMIT: 5000,
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
60
packages/shared/src/types/api.ts
Normal file
60
packages/shared/src/types/api.ts
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
/**
|
||||||
|
* Shared API types for Redirect Intelligence v2
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { z } from 'zod';
|
||||||
|
|
||||||
|
// Base API response
|
||||||
|
export const ApiResponseSchema = z.object({
|
||||||
|
success: z.boolean(),
|
||||||
|
status: z.number(),
|
||||||
|
data: z.any().optional(),
|
||||||
|
error: z.string().optional(),
|
||||||
|
message: z.string().optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type ApiResponse<T = any> = {
|
||||||
|
success: boolean;
|
||||||
|
status: number;
|
||||||
|
data?: T;
|
||||||
|
error?: string;
|
||||||
|
message?: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Legacy redirect result (for backward compatibility)
|
||||||
|
export const LegacyRedirectSchema = z.object({
|
||||||
|
url: z.string(),
|
||||||
|
timestamp: z.number(),
|
||||||
|
isSSL: z.boolean(),
|
||||||
|
duration: z.number().optional(),
|
||||||
|
statusCode: z.number().optional(),
|
||||||
|
statusText: z.string().optional(),
|
||||||
|
metadata: z.any().optional(),
|
||||||
|
responseBody: z.string().optional(),
|
||||||
|
sslInfo: z.any().optional(),
|
||||||
|
error: z.string().optional(),
|
||||||
|
final: z.boolean().optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type LegacyRedirect = z.infer<typeof LegacyRedirectSchema>;
|
||||||
|
|
||||||
|
// Track request schemas
|
||||||
|
export const TrackRequestSchema = z.object({
|
||||||
|
url: z.string().url(),
|
||||||
|
method: z.enum(['GET', 'HEAD', 'POST']).default('GET'),
|
||||||
|
userAgent: z.string().optional(),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type TrackRequest = z.infer<typeof TrackRequestSchema>;
|
||||||
|
|
||||||
|
// Track response schema
|
||||||
|
export const TrackResponseSchema = z.object({
|
||||||
|
url: z.string(),
|
||||||
|
method: z.string(),
|
||||||
|
redirectCount: z.number(),
|
||||||
|
finalUrl: z.string(),
|
||||||
|
finalStatusCode: z.number().optional(),
|
||||||
|
redirects: z.array(LegacyRedirectSchema),
|
||||||
|
});
|
||||||
|
|
||||||
|
export type TrackResponse = z.infer<typeof TrackResponseSchema>;
|
||||||
24
packages/shared/tsconfig.json
Normal file
24
packages/shared/tsconfig.json
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
{
|
||||||
|
"compilerOptions": {
|
||||||
|
"target": "ES2020",
|
||||||
|
"module": "commonjs",
|
||||||
|
"lib": ["ES2020"],
|
||||||
|
"outDir": "./dist",
|
||||||
|
"rootDir": "./src",
|
||||||
|
"strict": true,
|
||||||
|
"esModuleInterop": true,
|
||||||
|
"skipLibCheck": true,
|
||||||
|
"forceConsistentCasingInFileNames": true,
|
||||||
|
"resolveJsonModule": true,
|
||||||
|
"declaration": true,
|
||||||
|
"declarationMap": true,
|
||||||
|
"sourceMap": true
|
||||||
|
},
|
||||||
|
"include": [
|
||||||
|
"src/**/*"
|
||||||
|
],
|
||||||
|
"exclude": [
|
||||||
|
"node_modules",
|
||||||
|
"dist"
|
||||||
|
]
|
||||||
|
}
|
||||||
151
redirect_intelligence_v2_plan.md
Normal file
151
redirect_intelligence_v2_plan.md
Normal file
@@ -0,0 +1,151 @@
|
|||||||
|
# Redirect Intelligence — v2 Update (Chakra UI, Admin + Users, Postgres)
|
||||||
|
|
||||||
|
## 0) Goals & Strategy
|
||||||
|
|
||||||
|
- **Keep Node/Express** to minimize rewrite risk and preserve existing routes/limits/options (GET/POST `/api/v1/track`, legacy `/api/track`, 100 req/hr IP rate-limit, UA presets, SSL extraction, response-body truncation) [Comprehensive App Doc].
|
||||||
|
- **Add Postgres** via Prisma for users/orgs/projects/history/reports/jobs.
|
||||||
|
- **Upgrade UI to Chakra UI** with a modular app shell, light/dark, accessible components.
|
||||||
|
- **Expand analysis** to include robots/meta/canonical/mixed content and optional JS redirects; add export (MD/PDF), bulk jobs, API keys, monitoring—carried over from the earlier plan.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 1) Tech Stack (final)
|
||||||
|
|
||||||
|
- **Backend:** Node.js 20, Express (TS), **Prisma + PostgreSQL**, Zod (validation), **BullMQ + Redis** (bulk & monitoring), **Axios/undici** (HTTP), **Playwright** (optional JS redirects), **Puppeteer** (PDF export), `rate-limiter-flexible` (limits), `jsonwebtoken`/`jose` (auth).
|
||||||
|
- **Frontend:** React + TypeScript, **Chakra UI**, TanStack Query, React Router, Mermaid (client render) + server render for PDFs.
|
||||||
|
- **Auth:** Email/password (argon2), organization membership, roles (owner/admin/member), HttpOnly cookie sessions.
|
||||||
|
- **Billing (Phase later):** Stripe.
|
||||||
|
- **Packaging:** Docker Compose (api, web, db, redis, worker).
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 2) Data Model (PostgreSQL via Prisma)
|
||||||
|
|
||||||
|
**Core tables**
|
||||||
|
|
||||||
|
- `users(id, email unique, name, password_hash, created_at, last_login_at)`
|
||||||
|
- `organizations(id, name, plan, created_at)`
|
||||||
|
- `org_memberships(id, org_id fk, user_id fk, role enum(owner|admin|member))`
|
||||||
|
- `projects(id, org_id fk, name, settings_json, created_at)`
|
||||||
|
- `checks(id, project_id fk, input_url, method, headers_json, user_agent, started_at, finished_at, status enum(ok|error|timeout|loop), final_url, total_time_ms, report_id nullable)`
|
||||||
|
- `hops(id, check_id fk, hop_index, url, scheme, status_code, redirect_type enum(http_301|302|307|308|meta_refresh|js|final|other), latency_ms, content_type, reason, response_headers_json)`
|
||||||
|
- `ssl_inspections(id, check_id fk, host, valid_from, valid_to, days_to_expiry, issuer, protocol, warnings_json)`
|
||||||
|
- `seo_flags(id, check_id fk, robots_txt_status, robots_txt_rules_json, meta_robots, canonical_url, sitemap_present, noindex bool, nofollow bool)`
|
||||||
|
- `security_flags(id, check_id fk, safe_browsing_status, mixed_content enum(none|present|final_to_http), https_to_http bool)`
|
||||||
|
- `reports(id, check_id fk, markdown_path, pdf_path, created_at)`
|
||||||
|
- `bulk_jobs(id, project_id fk, upload_path, status enum(queued|running|done|error), progress_json, created_at, completed_at)`
|
||||||
|
- `api_keys(id, org_id fk, name, token_hash, perms_json, rate_limit_quota, created_at)`
|
||||||
|
- `audit_logs(id, org_id fk, actor_user_id fk, action, entity, entity_id, meta_json, created_at)`
|
||||||
|
|
||||||
|
**Indexes**
|
||||||
|
- `checks(project_id, started_at desc)`, `hops(check_id, hop_index)`, `api_keys(token_hash)`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 3) API (preserve current + extend)
|
||||||
|
|
||||||
|
**Keep existing behavior**
|
||||||
|
- `POST /api/v1/track`, `GET /api/v1/track` (query param mode), legacy `/api/track`, rate-limit (100/hr/IP), UA options, timing, SSL extraction, response truncation & metadata.
|
||||||
|
|
||||||
|
**New (MVP+)**
|
||||||
|
- `POST /api/v1/checks` (alias of `track` that persists history & returns `check_id`)
|
||||||
|
- `GET /api/v1/checks/:id` (check + hops + ssl + seo + security + diagram)
|
||||||
|
- `POST /api/v1/reports/:check_id/export` (`{format: md|pdf}`)
|
||||||
|
- `GET /api/v1/projects/:id/checks` (paginated history, filters)
|
||||||
|
- `POST /api/v1/bulk-jobs` (CSV upload), `GET /api/v1/bulk-jobs/:id` (progress, download bundle)
|
||||||
|
- `POST /api/v1/auth/login`, `POST /api/v1/auth/logout`, `GET /api/v1/auth/me`
|
||||||
|
- `POST /api/v1/users` (admin), `GET/PUT /api/v1/users/:id`
|
||||||
|
- `POST/GET/DELETE /api/v1/api-keys` (org-scoped), public namespace: `POST /public/checks`, `GET /public/checks/:id` with `x-api-key`
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 4) Analysis Pipeline (Node)
|
||||||
|
|
||||||
|
**HTTP chain (non-JS) now**
|
||||||
|
- Manual redirect following with Axios/undici (turn off auto-follow to record hops), keep per-hop timeouts, normalize URLs, loop set, capture headers. Preserves existing behavior.
|
||||||
|
|
||||||
|
**Additions**
|
||||||
|
- **Meta refresh** parse when 200 HTML with `<meta http-equiv="refresh">`.
|
||||||
|
- **SEO/Security**: fetch/parse robots.txt; parse `<meta name="robots">`, canonical; detect HTTP→HTTPS / HTTPS→HTTP; scan final HTML for `http://` subresources = mixed content.
|
||||||
|
- **Optional JS redirects** (phase later): Playwright worker with hard caps; record `window.location` changes as `js` hops.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 5) Exports
|
||||||
|
|
||||||
|
- **Markdown** via Nunjucks/Jinja-like templates in Node.
|
||||||
|
- **PDF** via Puppeteer (server-side render of a report page); embed Mermaid SVG graph; org branding for paid tiers.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 6) Frontend — Chakra UI
|
||||||
|
|
||||||
|
**App Shell (Chakra)**
|
||||||
|
- Header (org/project switcher, theme toggle), left Nav (Dashboard, Checks, Bulk, Projects, API, Monitoring, **Admin**), content area with responsive grid.
|
||||||
|
|
||||||
|
**Key Screens**
|
||||||
|
1) **Home / New Check**
|
||||||
|
2) **Check Detail**: Chain Table + Mermaid Diagram, flags, export.
|
||||||
|
3) **History (Project)**
|
||||||
|
4) **Bulk Jobs**
|
||||||
|
5) **Monitoring**
|
||||||
|
6) **API**
|
||||||
|
7) **Admin Panel**
|
||||||
|
|
||||||
|
**Chakra Components**
|
||||||
|
- `Container`, `Grid`, `Card`, `Table`, `Badge`, `Tabs`, `Accordion`, `Alert`, `Stat`, `FormControl`, `Select`, `Drawer`, `Modal`, `Menu`, `Progress`, `Skeleton`, `Toast`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 7) Security & Limits
|
||||||
|
|
||||||
|
- Preserve **100/hr/IP** default limit on public endpoints; add per-org/user buckets for authenticated usage.
|
||||||
|
- HttpOnly cookie sessions; CSRF on mutations; password hashing (argon2).
|
||||||
|
- Redact sensitive headers from persistence/logs.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 8) Phased Build Plan (Cursor-Ready)
|
||||||
|
|
||||||
|
### Phase 0 — Repo & Env (Dockerized)
|
||||||
|
### Phase 1 — Postgres + Prisma + Auth
|
||||||
|
### Phase 2 — Persisted Checks (Non-JS Chain)
|
||||||
|
### Phase 3 — SSL/SEO/Security Flags
|
||||||
|
### Phase 4 — Chakra UI Upgrade Complete
|
||||||
|
### Phase 5 — Exports (MD & PDF)
|
||||||
|
### Phase 6 — Bulk CSV + Worker
|
||||||
|
### Phase 7 — Request Options UI
|
||||||
|
### Phase 8 — API Keys + Public API + Quotas
|
||||||
|
### Phase 9 — Optional JS Redirects
|
||||||
|
### Phase 10 — Monitoring & Alerts
|
||||||
|
### Phase 11 — Admin Panel
|
||||||
|
### Phase 12 — Billing (Stripe)
|
||||||
|
### Phase 13 — Hardening & Perf
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 9) UI Map (Chakra)
|
||||||
|
|
||||||
|
- **Layout:** AppShell (Sidebar, Header, Main).
|
||||||
|
- **Home/New Check:** Input, Select, Drawer for headers.
|
||||||
|
- **Detail:** Tabs, Accordion for hops, Badges for flags, Stats, Buttons.
|
||||||
|
- **History:** Table with filters.
|
||||||
|
- **Bulk:** Dropzone, Progress, job list.
|
||||||
|
- **Admin:** Table for users, Modal invite, Select roles, Form for org & settings.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 10) Back-Compat & Migration
|
||||||
|
|
||||||
|
- Keep existing **endpoint semantics** and response shape for `/api/v1/track` and legacy `/api/track`.
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
## 11) Acceptance & KPIs
|
||||||
|
|
||||||
|
- Single URL (non-JS) median end-to-end < 1.2s; p95 < 3s.
|
||||||
|
- Meta refresh detection ≥ 99% test corpus.
|
||||||
|
- PDF export success ≥ 99%/1k reports.
|
||||||
|
- Bulk throughput ≥ 50 URLs/s with 5 workers.
|
||||||
|
|
||||||
|
---
|
||||||
30
turbo.json
Normal file
30
turbo.json
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
{
|
||||||
|
"$schema": "https://turbo.build/schema.json",
|
||||||
|
"globalDependencies": ["**/.env.*local"],
|
||||||
|
"pipeline": {
|
||||||
|
"build": {
|
||||||
|
"dependsOn": ["^build"],
|
||||||
|
"outputs": ["dist/**", ".next/**", "!.next/cache/**"]
|
||||||
|
},
|
||||||
|
"lint": {
|
||||||
|
"outputs": []
|
||||||
|
},
|
||||||
|
"dev": {
|
||||||
|
"cache": false,
|
||||||
|
"persistent": true
|
||||||
|
},
|
||||||
|
"test": {
|
||||||
|
"outputs": ["coverage/**"],
|
||||||
|
"dependsOn": ["^build"]
|
||||||
|
},
|
||||||
|
"clean": {
|
||||||
|
"cache": false
|
||||||
|
},
|
||||||
|
"db:generate": {
|
||||||
|
"cache": false
|
||||||
|
},
|
||||||
|
"db:migrate": {
|
||||||
|
"cache": false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user