feat: INS hotel technology landing page with lead capture #1
No files matched your search
@@ -29,6 +29,7 @@ docker run -p 3000:3000 -v ins-leads:/data ins-landing-page
|
||||
- Run exactly **1 replica** (SQLite file, single writer). Use `strategy: Recreate` so two pods never hold the volume at once during a rollout.
|
||||
- The container runs as user `node` (uid 1000); set `securityContext.fsGroup: 1000` so it can write to the volume.
|
||||
- Health check: `GET /` returns 200.
|
||||
- **Required: rate-limit `POST /api/leads` at the ingress.** The form is public and the honeypot only stops naive bots; without a limit a script can fill the volume. For ingress-nginx, e.g. `nginx.ingress.kubernetes.io/limit-rpm: "30"` (per client IP, applies to the whole Ingress) on this Ingress.
|
||||
|
||||
## Exporting leads
|
||||
|
||||
|
||||
+2
-2
@@ -74,8 +74,8 @@
|
||||
.section-head h2 { color: var(--navy); font-size: clamp(1.6rem, 3vw, 2.1rem); }
|
||||
.section-head p { color: var(--muted); }
|
||||
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 20px; }
|
||||
.card { background: #fff; border: 1px solid var(--line); border-top: 4px solid var(--yellow); border-radius: 8px; padding: 24px; }
|
||||
.card h3 { color: var(--navy); font-size: 1.15rem; }
|
||||
.card { background: #fff; border: 1px solid var(--line); border-top: 4px solid var(--yellow); padding: 24px; }
|
||||
.card h3 { color: var(--navy); font-size: 1.3rem; }
|
||||
.card p { color: var(--muted); font-size: .95rem; }
|
||||
.card ul { margin: 0; padding-left: 18px; font-size: .9rem; }
|
||||
.split { display: grid; grid-template-columns: 1fr 1fr; gap: 48px; align-items: center; }
|
||||
|
||||
@@ -8,12 +8,14 @@ const REQUIRED = ['name', 'email', 'phone', 'company'];
|
||||
const OPTIONAL = ['industry', 'message', 'referrer', 'utm_source', 'utm_medium', 'utm_campaign'];
|
||||
const MAX_LEN = { message: 2000, referrer: 500 };
|
||||
const EMAIL = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
const INDEX = path.join(__dirname, 'public', 'index.html');
|
||||
const STATIC = { '/logo.png': ['logo.png', 'image/png'] };
|
||||
const PAGES = { '/': ['index.html', 'text/html; charset=utf-8'], '/logo.png': ['logo.png', 'image/png'] };
|
||||
|
||||
function openDb(dbPath) {
|
||||
fs.mkdirSync(path.dirname(dbPath), { recursive: true });
|
||||
const db = new DatabaseSync(dbPath);
|
||||
// WAL: an export (reader) never blocks inserts. busy_timeout blocks the event loop while waiting,
|
||||
// ponytail: fine for one low-traffic pod; move to Postgres if this ever needs concurrency.
|
||||
db.exec('PRAGMA journal_mode = WAL; PRAGMA busy_timeout = 5000;');
|
||||
db.exec(`CREATE TABLE IF NOT EXISTS leads (
|
||||
id INTEGER PRIMARY KEY,
|
||||
created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
@@ -58,6 +60,11 @@ function readBody(req) {
|
||||
}
|
||||
|
||||
function createServer(dbPath) {
|
||||
// load static files up front: a missing file fails at startup instead of crashing on a request
|
||||
const files = {};
|
||||
for (const [url, [file, type]] of Object.entries(PAGES)) {
|
||||
files[url] = { body: fs.readFileSync(path.join(__dirname, 'public', file)), type };
|
||||
}
|
||||
const db = openDb(dbPath);
|
||||
const insert = db.prepare(`INSERT INTO leads
|
||||
(name, email, phone, company, industry, message, referrer, utm_source, utm_medium, utm_campaign)
|
||||
@@ -66,14 +73,9 @@ function createServer(dbPath) {
|
||||
const server = http.createServer(async (req, res) => {
|
||||
const url = req.url.split('?')[0];
|
||||
|
||||
if (req.method === 'GET' && url === '/') {
|
||||
res.writeHead(200, { 'content-type': 'text/html; charset=utf-8' });
|
||||
return fs.createReadStream(INDEX).pipe(res);
|
||||
}
|
||||
if (req.method === 'GET' && STATIC[url]) {
|
||||
const [file, type] = STATIC[url];
|
||||
res.writeHead(200, { 'content-type': type, 'cache-control': 'public, max-age=86400' });
|
||||
return fs.createReadStream(path.join(__dirname, 'public', file)).pipe(res);
|
||||
if (req.method === 'GET' && files[url]) {
|
||||
res.writeHead(200, { 'content-type': files[url].type });
|
||||
return res.end(files[url].body);
|
||||
}
|
||||
if (req.method === 'POST' && url === '/api/leads') {
|
||||
let lead;
|
||||
@@ -105,7 +107,9 @@ function createServer(dbPath) {
|
||||
module.exports = { createServer };
|
||||
|
||||
if (require.main === module) {
|
||||
const port = Number(process.env.PORT) || 3000;
|
||||
createServer(process.env.DB_PATH || path.join(__dirname, 'data', 'leads.db'))
|
||||
.listen(port, () => console.log(`listening on :${port}`));
|
||||
const port = Number(process.env.PORT ?? 3000);
|
||||
const server = createServer(process.env.DB_PATH || path.join(__dirname, 'data', 'leads.db'));
|
||||
server.listen(port, () => console.log(`listening on :${port}`));
|
||||
// node as PID 1 ignores SIGTERM by default; exit cleanly so k8s rollouts don't wait 30s
|
||||
process.on('SIGTERM', () => server.close(() => process.exit(0)));
|
||||
}
|
||||
@@ -93,3 +93,40 @@ test('honeypot submission is accepted but not stored', async () => {
|
||||
assert.strictEqual(res.status, 200);
|
||||
assert.strictEqual(rows().length, before);
|
||||
});
|
||||
|
||||
const { spawn } = require('node:child_process');
|
||||
|
||||
test('missing page file fails at startup, not on a request', () => {
|
||||
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'ins-nopage-'));
|
||||
fs.copyFileSync(path.join(__dirname, 'server.js'), path.join(tmp, 'server.js'));
|
||||
const { createServer: create } = require(path.join(tmp, 'server.js'));
|
||||
assert.throws(() => create(path.join(tmp, 'leads.db')), /ENOENT/);
|
||||
fs.rmSync(tmp, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
test('insert waits for a lock held by another process (e.g. an export)', async () => {
|
||||
const lock = spawn(process.execPath, ['--disable-warning=ExperimentalWarning', '-e', `
|
||||
const { DatabaseSync } = require('node:sqlite');
|
||||
const db = new DatabaseSync(${JSON.stringify(dbPath)});
|
||||
db.exec('BEGIN EXCLUSIVE');
|
||||
console.log('locked');
|
||||
setTimeout(() => { db.exec('COMMIT'); db.close(); }, 500);
|
||||
`]);
|
||||
await new Promise((r) => lock.stdout.once('data', r));
|
||||
const res = await post(valid);
|
||||
assert.strictEqual(res.status, 201);
|
||||
});
|
||||
|
||||
test('exits promptly on SIGTERM', async () => {
|
||||
const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'ins-term-'));
|
||||
const child = spawn(process.execPath, ['--disable-warning=ExperimentalWarning', path.join(__dirname, 'server.js')], {
|
||||
env: { ...process.env, PORT: '0', DB_PATH: path.join(tmp, 'leads.db') },
|
||||
});
|
||||
await new Promise((r) => child.stdout.once('data', r));
|
||||
const start = Date.now();
|
||||
child.kill('SIGTERM');
|
||||
const code = await new Promise((r) => child.on('exit', (c) => r(c)));
|
||||
assert.strictEqual(code, 0);
|
||||
assert.ok(Date.now() - start < 2000);
|
||||
fs.rmSync(tmp, { recursive: true, force: true });
|
||||
});
|
||||
Reference in new issue
Block a user