Master Backend Plan v3.0 + Government Portal + Implementation Order
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
{
|
||||
"name": "@landvex/gateway",
|
||||
"version": "0.1.0",
|
||||
"description": "LandveX Master Gateway — unifies all backend services",
|
||||
"main": "dist/index.js",
|
||||
"scripts": {
|
||||
"build": "tsc",
|
||||
"dev": "ts-node src/index.ts",
|
||||
"start": "node dist/index.js",
|
||||
"test": "jest"
|
||||
},
|
||||
"dependencies": {
|
||||
"express": "^4.18.2",
|
||||
"cors": "^2.8.5",
|
||||
"helmet": "^7.1.0",
|
||||
"dotenv": "^16.3.1",
|
||||
"stripe": "^14.0.0",
|
||||
"jsonwebtoken": "^9.0.2",
|
||||
"express-rate-limit": "^7.1.0",
|
||||
"http-proxy-middleware": "^2.0.6",
|
||||
"pg": "^8.11.0",
|
||||
"winston": "^3.11.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/express": "^4.17.21",
|
||||
"@types/cors": "^2.8.17",
|
||||
"@types/jsonwebtoken": "^9.0.5",
|
||||
"@types/node": "^20.10.0",
|
||||
"typescript": "^5.3.0",
|
||||
"ts-node": "^10.9.0",
|
||||
"jest": "^29.7.0",
|
||||
"@types/jest": "^29.5.0"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
/**
|
||||
* LandveX Simple Gateway
|
||||
*
|
||||
* Unifies all backend services without external dependencies.
|
||||
* Uses only Node.js built-in modules.
|
||||
*/
|
||||
|
||||
const http = require('http');
|
||||
const url = require('url');
|
||||
|
||||
const PORT = process.env.PORT || 3004;
|
||||
|
||||
// Service registry - only running services
|
||||
const services = {
|
||||
intelligence: { host: 'localhost', port: 3002, path: '/api/v1', status: 'unknown' },
|
||||
// apollo: { host: 'localhost', port: 3001, path: '/api/apollo', status: 'unknown' },
|
||||
// ledger: { host: 'localhost', port: 3250, path: '', status: 'unknown' },
|
||||
// incidents: { host: 'localhost', port: 3303, path: '', status: 'unknown' },
|
||||
};
|
||||
|
||||
// Simple proxy function
|
||||
function proxyRequest(req, res, target) {
|
||||
const options = {
|
||||
hostname: target.host,
|
||||
port: target.port,
|
||||
path: req.url,
|
||||
method: req.method,
|
||||
headers: {
|
||||
...req.headers,
|
||||
host: `${target.host}:${target.port}`,
|
||||
},
|
||||
};
|
||||
|
||||
const proxyReq = http.request(options, (proxyRes) => {
|
||||
res.writeHead(proxyRes.statusCode, proxyRes.headers);
|
||||
proxyRes.pipe(res);
|
||||
});
|
||||
|
||||
proxyReq.on('error', (err) => {
|
||||
console.error(`Proxy error: ${err.message}`);
|
||||
res.statusCode = 502;
|
||||
res.end(JSON.stringify({ error: 'Bad Gateway', message: err.message }));
|
||||
});
|
||||
|
||||
req.pipe(proxyReq);
|
||||
}
|
||||
|
||||
// Health check function
|
||||
async function checkServiceHealth(name, service) {
|
||||
return new Promise((resolve) => {
|
||||
const req = http.request({
|
||||
hostname: service.host,
|
||||
port: service.port,
|
||||
path: '/health',
|
||||
method: 'GET',
|
||||
timeout: 5000,
|
||||
}, (res) => {
|
||||
service.status = res.statusCode === 200 ? 'healthy' : 'unhealthy';
|
||||
resolve();
|
||||
});
|
||||
|
||||
req.on('error', () => {
|
||||
service.status = 'down';
|
||||
resolve();
|
||||
});
|
||||
|
||||
req.on('timeout', () => {
|
||||
service.status = 'timeout';
|
||||
req.destroy();
|
||||
resolve();
|
||||
});
|
||||
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
// Main server
|
||||
const server = http.createServer((req, res) => {
|
||||
// CORS
|
||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
|
||||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
|
||||
|
||||
if (req.method === 'OPTIONS') {
|
||||
res.statusCode = 204;
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
// Health check
|
||||
if (req.url === '/health') {
|
||||
res.setHeader('Content-Type', 'application/json');
|
||||
res.end(JSON.stringify({
|
||||
status: 'ok',
|
||||
version: '0.1.0-gateway',
|
||||
services: Object.entries(services).map(([name, config]) => ({
|
||||
name,
|
||||
status: config.status,
|
||||
url: `http://${config.host}:${config.port}`,
|
||||
})),
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
// Route to appropriate service
|
||||
if (req.url.startsWith('/api/v1')) {
|
||||
proxyRequest(req, res, services.intelligence);
|
||||
} else {
|
||||
res.statusCode = 404;
|
||||
res.end(JSON.stringify({ error: 'Not Found', path: req.url }));
|
||||
}
|
||||
});
|
||||
|
||||
// Check health of all services on startup
|
||||
async function init() {
|
||||
console.log('🔍 Checking service health...');
|
||||
for (const [name, service] of Object.entries(services)) {
|
||||
await checkServiceHealth(name, service);
|
||||
console.log(` ${name}: ${service.status}`);
|
||||
}
|
||||
|
||||
server.listen(PORT, () => {
|
||||
console.log(`🚀 LandveX Gateway running on port ${PORT}`);
|
||||
console.log(`📡 Services:`);
|
||||
Object.entries(services).forEach(([name, config]) => {
|
||||
console.log(` - ${name}: http://${config.host}:${config.port} (${config.status})`);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
init();
|
||||
@@ -0,0 +1,95 @@
|
||||
/**
|
||||
* LandveX Master Gateway
|
||||
*
|
||||
* Unifies all backend services:
|
||||
* - Intelligence Lab (port 3002)
|
||||
* - Apollo CRM (/api/apollo)
|
||||
* - Stripe billing
|
||||
* - aamos-ledger (port 3250)
|
||||
* - aamos-incidents (port 3303)
|
||||
* - Auth & API keys
|
||||
*/
|
||||
|
||||
import express from 'express';
|
||||
import cors from 'cors';
|
||||
import helmet from 'helmet';
|
||||
import rateLimit from 'express-rate-limit';
|
||||
import { createProxyMiddleware } from 'http-proxy-middleware';
|
||||
import dotenv from 'dotenv';
|
||||
|
||||
dotenv.config();
|
||||
|
||||
const app = express();
|
||||
const PORT = process.env.PORT || 3004;
|
||||
|
||||
// Middleware
|
||||
app.use(helmet());
|
||||
app.use(cors());
|
||||
app.use(express.json());
|
||||
|
||||
// Rate limiting
|
||||
const limiter = rateLimit({
|
||||
windowMs: 15 * 60 * 1000, // 15 minutes
|
||||
max: 100, // limit each IP to 100 requests per windowMs
|
||||
});
|
||||
app.use(limiter);
|
||||
|
||||
// Health check
|
||||
app.get('/health', (_req, res) => {
|
||||
res.json({
|
||||
status: 'ok',
|
||||
version: '0.1.0-master',
|
||||
services: {
|
||||
intelligence: 'http://localhost:3002',
|
||||
apollo: '/api/apollo',
|
||||
ledger: 'http://localhost:3250',
|
||||
incidents: 'http://localhost:3303',
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Proxy to Intelligence Lab
|
||||
app.use('/api/v1', createProxyMiddleware({
|
||||
target: 'http://localhost:3002',
|
||||
changeOrigin: true,
|
||||
pathRewrite: { '^/api/v1': '/api/v1' },
|
||||
}));
|
||||
|
||||
// Proxy to Apollo CRM
|
||||
app.use('/api/apollo', createProxyMiddleware({
|
||||
target: 'http://localhost:3001',
|
||||
changeOrigin: true,
|
||||
pathRewrite: { '^/api/apollo': '/api/apollo' },
|
||||
}));
|
||||
|
||||
// Proxy to Ledger
|
||||
app.use('/api/ledger', createProxyMiddleware({
|
||||
target: 'http://localhost:3250',
|
||||
changeOrigin: true,
|
||||
pathRewrite: { '^/api/ledger': '' },
|
||||
}));
|
||||
|
||||
// Proxy to Incidents
|
||||
app.use('/api/incidents', createProxyMiddleware({
|
||||
target: 'http://localhost:3303',
|
||||
changeOrigin: true,
|
||||
pathRewrite: { '^/api/incidents': '' },
|
||||
}));
|
||||
|
||||
// Stripe webhook
|
||||
app.post('/webhooks/stripe', express.raw({ type: 'application/json' }), (req, res) => {
|
||||
// TODO: Implement Stripe webhook handling
|
||||
res.json({ received: true });
|
||||
});
|
||||
|
||||
// Start server
|
||||
app.listen(PORT, () => {
|
||||
console.log(`🚀 LandveX Master Gateway running on port ${PORT}`);
|
||||
console.log(`📡 Proxying to:`);
|
||||
console.log(` - Intelligence Lab: http://localhost:3002`);
|
||||
console.log(` - Apollo CRM: http://localhost:3001`);
|
||||
console.log(` - Ledger: http://localhost:3250`);
|
||||
console.log(` - Incidents: http://localhost:3303`);
|
||||
});
|
||||
|
||||
export default app;
|
||||
@@ -0,0 +1,19 @@
|
||||
{
|
||||
"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
|
||||
},
|
||||
"include": ["src/**/*"],
|
||||
"exclude": ["node_modules", "dist"]
|
||||
}
|
||||
Reference in New Issue
Block a user