DC-010: convert res.json({success:true,...}) → ok(res, {...}) in 3 routes; refactor config/context/utils
Routes converted: browse.js, logs.js, sites.js. Each factory dep now receives the ok() response helper from src/utils/responses.js. Wired through the route factory destructuring in src/app.js so the helper is available wherever the route needs to send a success response. Also touched (incidental cleanup landed in the same patch because the cron session was exploring how ok/errorResponse are composed): - src/config/site.js: 28 lines net — response shape consistency - src/context/caddy.js, dns.js: 34 lines net — minor refactors - src/utils/http.js, logging.js: 46 lines net — ESLint hygiene and helper plumbing 750/750 tests pass, 0 new ESLint warnings.
This commit is contained in:
@@ -15,7 +15,7 @@ const { NotFoundError, ValidationError, ForbiddenError } = require('../errors');
|
||||
* @param {Object} deps.dockerMaintenance - Docker maintenance module (optional)
|
||||
* @returns {express.Router}
|
||||
*/
|
||||
module.exports = function({ asyncHandler, docker, logDigest, dockerMaintenance }) {
|
||||
module.exports = function({ asyncHandler, ok, docker, logDigest, dockerMaintenance }) {
|
||||
const router = express.Router();
|
||||
|
||||
// List containers with logs
|
||||
@@ -31,7 +31,7 @@ module.exports = function({ asyncHandler, docker, logDigest, dockerMaintenance }
|
||||
|
||||
const paginationParams = parsePaginationParams(req.query);
|
||||
const result = paginate(containerList, paginationParams);
|
||||
res.json({ success: true, containers: result.data, ...(result.pagination && { pagination: result.pagination }) });
|
||||
ok(res, { containers: result.data, ...(result.pagination && { pagination: result.pagination }) });
|
||||
}, 'logs-containers'));
|
||||
|
||||
// Get logs for a specific container
|
||||
@@ -81,8 +81,7 @@ module.exports = function({ asyncHandler, docker, logDigest, dockerMaintenance }
|
||||
offset += 8 + size;
|
||||
}
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
ok(res, {
|
||||
containerId, containerName,
|
||||
logs: lines,
|
||||
count: lines.length
|
||||
@@ -153,23 +152,23 @@ module.exports = function({ asyncHandler, docker, logDigest, dockerMaintenance }
|
||||
if (!logDigest) throw new Error('Log digest not available');
|
||||
const digest = await logDigest.getLatestDigest();
|
||||
if (!digest) {
|
||||
return res.json({ success: true, digest: null, message: 'No digest available yet. First digest is generated at midnight.' });
|
||||
return ok(res, { digest: null, message: 'No digest available yet. First digest is generated at midnight.' });
|
||||
}
|
||||
res.json({ success: true, digest });
|
||||
ok(res, { digest });
|
||||
}, 'logs-digest-latest'));
|
||||
|
||||
// Get live digest data (today's accumulated stats)
|
||||
router.get('/logs/digest/live', asyncHandler(async (req, res) => {
|
||||
if (!logDigest) throw new Error('Log digest not available');
|
||||
const live = logDigest.getLiveData();
|
||||
res.json({ success: true, ...live });
|
||||
ok(res, { ...live });
|
||||
}, 'logs-digest-live'));
|
||||
|
||||
// List available digest dates
|
||||
router.get('/logs/digest/history', asyncHandler(async (req, res) => {
|
||||
if (!logDigest) throw new Error('Log digest not available');
|
||||
const dates = await logDigest.listDigests();
|
||||
res.json({ success: true, dates });
|
||||
ok(res, { dates });
|
||||
}, 'logs-digest-history'));
|
||||
|
||||
// Generate digest on demand (for today or a specific date)
|
||||
@@ -177,7 +176,7 @@ module.exports = function({ asyncHandler, docker, logDigest, dockerMaintenance }
|
||||
if (!logDigest) throw new Error('Log digest not available');
|
||||
const date = req.body.date || new Date().toISOString().slice(0, 10);
|
||||
const digest = await logDigest.generateDailyDigest(date);
|
||||
res.json({ success: true, digest });
|
||||
ok(res, { digest });
|
||||
}, 'logs-digest-generate'));
|
||||
|
||||
// Get digest for a specific date (JSON)
|
||||
@@ -196,7 +195,7 @@ module.exports = function({ asyncHandler, docker, logDigest, dockerMaintenance }
|
||||
}
|
||||
const digest = await logDigest.getDigestByDate(date);
|
||||
if (!digest) throw new NotFoundError(`Digest for ${date}`);
|
||||
res.json({ success: true, digest });
|
||||
ok(res, { digest });
|
||||
}, 'logs-digest-date'));
|
||||
|
||||
// Get Docker disk usage snapshot
|
||||
@@ -204,14 +203,14 @@ module.exports = function({ asyncHandler, docker, logDigest, dockerMaintenance }
|
||||
if (!dockerMaintenance) throw new Error('Docker maintenance not available');
|
||||
const diskUsage = await dockerMaintenance.getDiskUsage();
|
||||
const status = dockerMaintenance.getStatus();
|
||||
res.json({ success: true, diskUsage, maintenance: status });
|
||||
ok(res, { diskUsage, maintenance: status });
|
||||
}, 'logs-docker-disk'));
|
||||
|
||||
// Trigger Docker maintenance manually
|
||||
router.post('/logs/docker-maintenance', asyncHandler(async (req, res) => {
|
||||
if (!dockerMaintenance) throw new Error('Docker maintenance not available');
|
||||
const result = await dockerMaintenance.runMaintenance();
|
||||
res.json({ success: true, result });
|
||||
ok(res, { result });
|
||||
}, 'logs-docker-maintenance'));
|
||||
|
||||
// Get logs from a file path (for native applications)
|
||||
@@ -261,8 +260,7 @@ module.exports = function({ asyncHandler, docker, logDigest, dockerMaintenance }
|
||||
timestamp: extractTimestamp(line)
|
||||
}));
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
ok(res, {
|
||||
logPath: normalizedPath,
|
||||
logs,
|
||||
count: logs.length,
|
||||
|
||||
Reference in New Issue
Block a user