2021-06-30 21:18:42 +00:00
|
|
|
// if new relic license key exists. Then load the key.
|
2021-07-06 11:34:39 +00:00
|
|
|
if (process.env.NEW_RELIC_LICENSE_KEY) {
|
2021-06-30 21:18:42 +00:00
|
|
|
require('newrelic');
|
|
|
|
}
|
|
|
|
|
2020-03-27 11:30:27 +00:00
|
|
|
const { NODE_ENV } = process.env;
|
|
|
|
|
|
|
|
if (!NODE_ENV || NODE_ENV === 'development') {
|
|
|
|
// Load env vars from /backend/.env
|
|
|
|
require('custom-env').env();
|
|
|
|
}
|
|
|
|
|
2021-01-26 13:48:33 +00:00
|
|
|
process.on('exit', () => {
|
2021-07-08 19:33:17 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2021-01-26 13:48:33 +00:00
|
|
|
console.log('Probe Shutting Shutdown');
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('unhandledRejection', err => {
|
2021-07-08 19:33:17 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2021-01-26 13:48:33 +00:00
|
|
|
console.error('Unhandled rejection in probe process occurred');
|
2021-07-08 19:33:17 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2021-01-26 13:48:33 +00:00
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
|
|
|
|
process.on('uncaughtException', err => {
|
2021-07-08 19:33:17 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2021-01-26 13:48:33 +00:00
|
|
|
console.error('Uncaught exception in probe process occurred');
|
2021-07-08 19:33:17 +00:00
|
|
|
// eslint-disable-next-line no-console
|
2021-01-26 13:48:33 +00:00
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
|
2020-02-16 23:23:27 +00:00
|
|
|
const express = require('express');
|
2021-09-09 12:23:03 +00:00
|
|
|
const Sentry = require('@sentry/node');
|
2020-02-16 23:23:27 +00:00
|
|
|
const app = express();
|
|
|
|
const http = require('http').createServer(app);
|
|
|
|
const cors = require('cors');
|
2019-11-21 13:05:28 +00:00
|
|
|
const Main = require('./workers/main');
|
|
|
|
const cron = require('node-cron');
|
|
|
|
const config = require('./utils/config');
|
|
|
|
|
2021-09-09 12:23:03 +00:00
|
|
|
Sentry.init({
|
|
|
|
dsn: process.env.SENTRY_DSN,
|
|
|
|
release: `probe@${process.env.npm_package_version}`,
|
|
|
|
environment: process.env.NODE_ENV,
|
2021-09-09 19:38:09 +00:00
|
|
|
tracesSampleRate: 0.0,
|
2021-09-13 13:50:03 +00:00
|
|
|
integrations: [
|
|
|
|
new Sentry.Integrations.OnUncaughtException({
|
|
|
|
onFatalError() {
|
|
|
|
// override default behaviour
|
|
|
|
return;
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
],
|
2021-09-09 12:23:03 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// Sentry: The request handler must be the first middleware on the app
|
|
|
|
app.use(Sentry.Handlers.requestHandler());
|
|
|
|
|
2020-02-24 13:07:26 +00:00
|
|
|
const cronMinuteStartTime = Math.floor(Math.random() * 50);
|
|
|
|
|
2019-11-21 13:05:28 +00:00
|
|
|
app.use(cors());
|
|
|
|
app.set('port', process.env.PORT || 3008);
|
|
|
|
|
2021-08-12 12:43:05 +00:00
|
|
|
const monitorStore = {};
|
|
|
|
|
2021-12-14 20:40:38 +00:00
|
|
|
// handle probe1 status
|
|
|
|
app.get(['/probe1/status', '/status'], function(req, res) {
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
res.send(
|
|
|
|
JSON.stringify({
|
|
|
|
status: 200,
|
|
|
|
message: 'Service Status - OK',
|
|
|
|
serviceType: 'oneuptime-probe',
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
// handle probe2 status
|
|
|
|
app.get(['/probe2/status', '/status'], function(req, res) {
|
2019-11-21 13:05:28 +00:00
|
|
|
res.setHeader('Content-Type', 'application/json');
|
2020-02-27 11:15:46 +00:00
|
|
|
res.send(
|
|
|
|
JSON.stringify({
|
|
|
|
status: 200,
|
|
|
|
message: 'Service Status - OK',
|
2021-11-22 11:45:11 +00:00
|
|
|
serviceType: 'oneuptime-probe',
|
2020-02-27 11:15:46 +00:00
|
|
|
})
|
|
|
|
);
|
2019-11-21 13:05:28 +00:00
|
|
|
});
|
|
|
|
|
2021-12-16 07:49:29 +00:00
|
|
|
app.get(['/probe1/monitorCount', '/monitorCount'], function(req, res) {
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
res.send(
|
|
|
|
JSON.stringify({
|
|
|
|
monitorCount: Object.keys(monitorStore).length,
|
|
|
|
monitors: monitorStore,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
app.get(['/probe2/monitorCount', '/monitorCount'], function(req, res) {
|
2021-08-12 12:39:52 +00:00
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
res.send(
|
|
|
|
JSON.stringify({
|
|
|
|
monitorCount: Object.keys(monitorStore).length,
|
|
|
|
monitors: monitorStore,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-01-25 16:31:08 +00:00
|
|
|
//App Version
|
|
|
|
app.get(['/probe/version', '/version'], function(req, res) {
|
|
|
|
res.setHeader('Content-Type', 'application/json');
|
|
|
|
res.send({ probeVersion: process.env.npm_package_version });
|
|
|
|
});
|
|
|
|
|
2021-09-09 12:23:03 +00:00
|
|
|
app.use(Sentry.Handlers.errorHandler());
|
|
|
|
global.Sentry = Sentry;
|
|
|
|
|
2022-01-18 16:38:06 +00:00
|
|
|
setTimeout(() => {
|
|
|
|
Main.runJob(monitorStore);
|
|
|
|
}, cronMinuteStartTime * 1000);
|
2019-11-21 13:05:28 +00:00
|
|
|
|
2021-12-14 17:31:23 +00:00
|
|
|
http.listen(app.get('port'), function() {
|
|
|
|
// eslint-disable-next-line
|
|
|
|
console.log(
|
|
|
|
`Probe with Probe Name ${config.probeName} and Probe Key ${
|
|
|
|
config.probeKey
|
|
|
|
} Started on port ${app.get('port')}. OneUptime API URL: ${
|
|
|
|
config.serverUrl
|
|
|
|
}`
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2019-11-21 13:05:28 +00:00
|
|
|
module.exports = app;
|