2020-05-06 15:25:35 +00:00
|
|
|
import {json, Router} from 'express';
|
2020-07-05 18:19:52 +00:00
|
|
|
import {CurrentStatus, State} from './app/_data/data';
|
|
|
|
import {existsSync, readFileSync, writeFileSync} from 'fs';
|
2020-07-05 20:09:28 +00:00
|
|
|
import {join} from 'path';
|
|
|
|
import {JSONPath} from 'jsonpath-plus';
|
2020-05-06 15:25:35 +00:00
|
|
|
|
|
|
|
interface Cache {
|
2020-07-05 18:19:52 +00:00
|
|
|
[id: string]: State;
|
2020-05-06 15:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface Config {
|
|
|
|
authToken: string;
|
|
|
|
title: string;
|
|
|
|
description: string;
|
2020-07-16 20:08:02 +00:00
|
|
|
servicesPath?: string;
|
|
|
|
idPath?: string;
|
|
|
|
statePath?: string;
|
2020-07-05 18:19:52 +00:00
|
|
|
stateValues: {
|
|
|
|
operational: string[];
|
|
|
|
maintenance: string[];
|
|
|
|
};
|
2020-05-06 15:25:35 +00:00
|
|
|
groups: {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
2020-07-16 20:08:02 +00:00
|
|
|
url?: string;
|
2020-05-06 15:25:35 +00:00
|
|
|
services: {
|
|
|
|
id: string;
|
|
|
|
name: string;
|
2020-07-16 20:08:02 +00:00
|
|
|
url?: string;
|
2020-07-05 18:19:52 +00:00
|
|
|
statePath?: string;
|
2020-05-06 15:25:35 +00:00
|
|
|
}[];
|
|
|
|
}[];
|
|
|
|
}
|
|
|
|
|
|
|
|
const api = Router();
|
|
|
|
api.use(json());
|
|
|
|
|
2020-07-05 20:09:28 +00:00
|
|
|
const serviceStates = existsSync(join(process.cwd(), 'cache.json')) ? JSON.parse(readFileSync(join(process.cwd(), 'cache.json'), {encoding: 'utf-8'})) : {} as Cache;
|
|
|
|
const config = JSON.parse(readFileSync(join(process.cwd(), 'config.json'), {encoding: 'utf-8'})) as Config;
|
2020-07-16 20:08:02 +00:00
|
|
|
const serviceStatePaths: { [service: string]: string } = config.groups
|
2020-07-05 18:19:52 +00:00
|
|
|
.map(g => g.services).reduce((x, y) => x.concat(y), [])
|
2020-07-16 20:08:02 +00:00
|
|
|
.filter(s => s.statePath)
|
2020-07-05 18:19:52 +00:00
|
|
|
.reduce((services, service) => {
|
2020-07-16 20:08:02 +00:00
|
|
|
services[service.id] = service.statePath;
|
2020-07-05 18:19:52 +00:00
|
|
|
return services;
|
|
|
|
}, {});
|
2020-05-06 15:25:35 +00:00
|
|
|
|
|
|
|
let cache: CurrentStatus;
|
|
|
|
updateCache();
|
|
|
|
|
|
|
|
api.post('/update/health', (req, res) => {
|
|
|
|
const token = req.query.token;
|
|
|
|
if (token !== config.authToken) {
|
|
|
|
return res.status(401).send('invalid token');
|
|
|
|
}
|
|
|
|
const serviceId = req.query.service as string;
|
2020-07-16 20:08:02 +00:00
|
|
|
let services: { id: string, state: string }[] = [];
|
|
|
|
if (serviceId) {
|
|
|
|
services = [{id: serviceId, state: JSONPath({path: serviceStatePaths[serviceId], json: req.body, wrap: false})}];
|
|
|
|
} else if (config.servicesPath && config.idPath && config.statePath) {
|
|
|
|
services = JSONPath({path: config.servicesPath, json: req.body})
|
|
|
|
.map(s => ({
|
|
|
|
id: JSONPath({path: config.idPath, json: s, wrap: false}),
|
|
|
|
state: JSONPath({path: config.statePath, json: s, wrap: false})
|
|
|
|
}));
|
2020-05-06 15:25:35 +00:00
|
|
|
}
|
|
|
|
|
2020-07-16 20:08:02 +00:00
|
|
|
services.forEach(s => {
|
|
|
|
if (config.stateValues.operational.includes(s.state)) {
|
|
|
|
serviceStates[s.id] = 'operational';
|
|
|
|
} else if (config.stateValues.maintenance.includes(s.state)) {
|
|
|
|
serviceStates[s.id] = 'maintenance';
|
|
|
|
} else {
|
|
|
|
serviceStates[s.id] = 'outage';
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-05-06 15:25:35 +00:00
|
|
|
updateCache();
|
|
|
|
|
2020-07-05 18:19:52 +00:00
|
|
|
writeFileSync('cache.json', JSON.stringify(serviceStates), {encoding: 'utf-8'});
|
2020-05-06 15:25:35 +00:00
|
|
|
|
|
|
|
return res.send('OK');
|
|
|
|
});
|
|
|
|
|
|
|
|
api.get('/status', (req, res) => {
|
|
|
|
return res.json(cache);
|
|
|
|
});
|
|
|
|
|
|
|
|
api.get('/info', (req, res) => {
|
|
|
|
return res.json({
|
|
|
|
title: config.title,
|
|
|
|
description: config.description
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
function updateCache(): void {
|
|
|
|
const groups = config.groups.map(group => {
|
|
|
|
const services = group.services.map(service => {
|
|
|
|
return {
|
|
|
|
id: service.id,
|
|
|
|
name: service.name,
|
|
|
|
url: service.url,
|
2020-07-05 18:19:52 +00:00
|
|
|
state: serviceStates[service.id] || 'operational'
|
|
|
|
};
|
2020-05-06 15:25:35 +00:00
|
|
|
});
|
|
|
|
return {
|
|
|
|
id: group.id,
|
|
|
|
name: group.name,
|
2020-07-16 20:08:02 +00:00
|
|
|
url: group.url,
|
2020-05-06 15:25:35 +00:00
|
|
|
state: calculateOverallState(services.map(s => s.state)),
|
|
|
|
services: services
|
2020-07-05 18:19:52 +00:00
|
|
|
};
|
2020-05-06 15:25:35 +00:00
|
|
|
});
|
|
|
|
cache = {
|
|
|
|
state: calculateOverallState(groups.map(g => g.state)),
|
|
|
|
groups: groups
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function calculateOverallState(states: State[]): State {
|
2020-07-05 18:19:52 +00:00
|
|
|
return states.includes('outage') ? 'outage' : states.includes('maintenance') ? 'maintenance' : 'operational';
|
2020-05-06 15:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export {api};
|