import { FastifyPluginAsync } from 'fastify' import { readFile } from 'fs/promises' import { join } from 'path' const agentRelease: FastifyPluginAsync = async (fastify) => { fastify.get('/api/agents/latest-release', { onRequest: [fastify.authenticate] }, async (_req, reply) => { const updatesDir = process.env.AGENT_UPDATES_DIR ?? join(process.cwd(), '..', 'agent-updates') try { const yaml = await readFile(join(updatesDir, 'latest.yml'), 'utf8') // Parse version: line like "version: 1.0.6" const versionMatch = yaml.match(/^version:\s*(.+)$/m) const pathMatch = yaml.match(/^path:\s*(.+)$/m) if (!versionMatch) return reply.code(404).send({ error: 'latest.yml not found or invalid' }) const version = versionMatch[1].trim() const fileName = pathMatch?.[1].trim() ?? `HotelSync Agent Setup ${version}.exe` const host = process.env.API_PUBLIC_URL ?? 'https://api.hotelsync.ru' return { version, fileName, downloadUrl: `${host}/agent-updates/${encodeURIComponent(fileName)}` } } catch { return reply.code(404).send({ error: 'No release available' }) } }) } export default agentRelease