instant-workstation/backend/workstations/workstations.js

39 lines
1.1 KiB
JavaScript
Raw Normal View History

2026-08-29 18:18:56 +03:00
import fs from 'node:fs';
import http from 'node:http';
import * as yaml from 'js-yaml';
const LOCALE = 'en';
const yamlFiles = fs.globSync(`${import.meta.dirname}/data/**/*.yaml`);
const yamlContents = yamlFiles.map(yamlFile => fs.readFileSync(yamlFile, 'utf8'));
const workstations = yamlContents.map(yamlContent => yaml.load(yamlContent));
workstations.sort(compareWorkstations);
const workstationsJSON = JSON.stringify(workstations);
const server = http.createServer(onRequest);
process.on('SIGINT', shutdown);
process.on('SIGTERM', shutdown);
server.listen(50000);
function compareWorkstations(first, second) {
const osComparison = first.os.localeCompare(second.os, LOCALE);
const versionComparison = second.version.localeCompare(first.version, LOCALE, { numeric: true });
return osComparison || versionComparison;
}
function onRequest(_request, response) {
response.setHeader('Content-Type', 'application/json');
response.end(workstationsJSON);
}
function shutdown() {
process.off('SIGINT', shutdown);
process.off('SIGTERM', shutdown);
server.close();
}