sysinfo/api/server.js

69 řádky
1.4 KiB
JavaScript

'use strict';
const Hapi = require('@hapi/hapi');
const fs = require('fs')
const { execSync } = require('child_process')
const dir = '/corn/sftpgo/data/data/gweicz-sysinfo'
const SPEC_FILE = `${dir}/spec/services.yaml`
let spec = null
let mtime = null
async function buildSpec () {
console.log('Building spec ..')
const out = execSync('cd .. && npm run build', { env: { GWEICZ_SYSINFO_SPEC: SPEC_FILE }})
console.log(out.toString())
console.log('Spec builded. Done.')
mtime = new Date()
spec = JSON.parse(fs.readFileSync('../build/spec.json'))
}
async function checkLoaded () {
const specStat = fs.statSync(SPEC_FILE)
console.log(new Date(specStat.mtime), mtime)
if (new Date(specStat.mtime) > mtime) {
console.log('Found new version, rebuilding ..')
await buildSpec()
}
}
const init = async () => {
const server = Hapi.server({
port: 8033,
host: 'localhost'
});
server.route({
method: 'GET',
path: '/api/spec',
handler: (request, h) => {
return spec
}
});
await server.start();
console.log('Server running on %s', server.info.uri);
setInterval(async () => {
await buildSpec()
}, 1000 * 60 * 15)
setInterval(async () => {
await checkLoaded()
}, 1000 * 60)
await buildSpec()
};
process.on('unhandledRejection', (err) => {
console.log(err);
process.exit(1);
});
init();