2 * @file routes/agentVersionTxt.js
3 * @module routes/agentVersionTxt
5 * Agent version text file endpoint for update checks.
6 * Serves plain text version number from distribution folder for agent auto-update checks.
9 * - Unauthenticated endpoint for agent update checks
10 * - Supports external and local distribution paths
11 * - Returns plain text version string (e.g., "1.0.12")
12 * - Used by agents to compare current version with latest
14 * **Path Resolution:**
15 * - Primary: `/opt/apps/IBG_HUB_dist/agent/version.txt`
16 * - Fallback: `../agent/dist/version.txt` (development)
20 * @author RMM-PSA Development Team
24const express = require('express');
25const router = express.Router();
26const fs = require('fs');
27const path = require('path');
30 * Resolve agent distribution path with fallback.
31 * Checks external production path first, then local development path.
32 * @param {string} relPath Relative path within distribution folder
33 * @returns {string | null} Resolved absolute path, or null if not found
36function resolveDistPath(relPath) {
37 const externalPath = path.resolve('/opt/apps/IBG_HUB_dist/agent', relPath);
38 const localPath = path.resolve(__dirname, '../agent/dist', relPath);
39 if (fs.existsSync(externalPath)) return externalPath;
40 if (fs.existsSync(localPath)) return localPath;
45 * @api {get} /agent/version.txt Get agent version (text)
46 * @apiName GetAgentVersionTxt
47 * @apiGroup AgentUpdate
49 * Retrieve current agent version as plain text file.
50 * Used by agents for automated update checks. No authentication required.
51 * Resolves from production or development distribution paths.
52 * @apiSuccess {string} version Plain text version string (e.g., "1.0.12")
53 * @apiError (404) {String} error="version.txt not found" Version file missing
54 * @apiError (500) {String} error="Failed to read version.txt" File read error
55 * @apiExample {curl} Example usage:
56 * curl -X GET https://api.example.com/agent/version.txt
57 * @apiSuccessExample {text} Success-Response:
59 * Content-Type: text/plain
63router.get('/version.txt', (req, res) => {
64 const versionPath = resolveDistPath('version.txt');
65 if (!versionPath || !fs.existsSync(versionPath)) {
66 return res.status(404).send('version.txt not found');
69 const version = fs.readFileSync(versionPath, 'utf8').trim();
70 res.type('text/plain').send(version);
72 console.error('[AgentVersion] Failed to read version.txt:', err);
73 res.status(500).send('Failed to read version.txt');
77module.exports = router;