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:
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"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user