backend: add static serving + multi-stage Dockerfile, website: add API test dashboard

This commit is contained in:
sumit-plena
2026-07-07 17:03:33 +05:30
parent 1dd97cf059
commit 317d60ed63
78 changed files with 6100 additions and 40 deletions

View File

@@ -1,12 +1,15 @@
import 'dotenv/config';
import path from 'path';
import { fileURLToPath } from 'url';
import cors from 'cors';
import express, { type NextFunction, type Request, type Response } from 'express';
import cron from 'node-cron';
import { buildApiPath, getConfig } from './config';
import { TursoClient } from './lib/db';
import { createHealthRouter } from './routes/health';
import { createTodosRouter } from './routes/todos';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const config = getConfig();
const app = express();
const db = new TursoClient(config);
@@ -19,24 +22,14 @@ const apiRoot = config.apiPrefix || '/';
app.use(apiRoot, createHealthRouter(config));
app.use(apiRoot, createTodosRouter(config, db));
// 1. Cron Job A: Runs every 3 minutes
cron.schedule('*/3 * * * *', () => {
console.log('[Cron A] Heartbeat executing every 3 minutes: ' + new Date().toISOString());
});
const publicDir = path.join(__dirname, '../public');
app.use(express.static(publicDir));
// 2. Cron Job B: Runs every 10 minutes
cron.schedule('*/10 * * * *', () => {
console.log('[Cron B] Heartbeat executing every 10 minutes: ' + new Date().toISOString());
});
// 3. Cloud Run Cron Tick endpoint (No-op to wake up event loop)
app.post('/_cron/tick', (req, res) => {
const authHeader = req.headers.authorization;
if (!authHeader || authHeader !== 'Bearer ' + process.env.CRON_TOKEN) {
return res.status(401).json({ success: false, error: 'Unauthorized' });
}
console.log('[Tick] Heartbeat received. Waking up event loop.');
res.status(200).json({ success: true });
app.get('*', (req, res, next) => {
if (req.path.startsWith(apiRoot)) return next();
res.sendFile(path.join(publicDir, 'index.html'), (err) => {
if (err) next();
});
});
app.use((_req, res) => {