fix: harden server per final review
- load static files at startup so a missing file fails fast instead of crashing on request - WAL + busy_timeout so a manual export cannot fail live submissions - exit cleanly on SIGTERM (node is PID 1 in the container) - honour PORT=0 - document a required ingress rate limit - square card corners and larger card titles Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01EFUzfmHGGiYUjNcvJoyzdf
This commit is contained in:
1 parent
c98c6da2ab
commit
f75da6d928
4 files changed
+57
-15
No files matched your search
@@ -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)));
|
||||
}
|
||||
Reference in new issue
Block a user