-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (65 loc) · 2.06 KB
/
index.js
File metadata and controls
81 lines (65 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*********************************
Uptime Website Monitor
By Boone Software
*********************************/
// Node dependencies
const _ = require('underscore');
const bodyParser = require('body-parser');
const express = require('express');
const http = require('q-io/http');
const json = require('json');
const mongoose = require('mongoose');
const path = require('path');
const q = require('q');
// Global application configuration
require('dotenv').config();
// Mongoose promise drop-in
mongoose.Promise = require('q').Promise;
// Classes
const EndpointCtrl = require('./controllers/endpoint');
const ResultCtrl = require('./controllers/result');
const Monitor = require('./monitor/index');
// Connect to DB
mongoose.connect(`mongodb://${process.env.DB_HOST}:${process.env.DB_PORT}/${process.env.DB_NAME}`);
mongoose.connection.on('connected', () => {
console.log(`Successfully connected to MongoDB on port ${process.env.DB_PORT}`);
});
// Set Mongoose config
mongoose.set('debug', true);
// Initialize Express
const app = express();
const port = process.env.PORT || 3000;
app.use(bodyParser.urlencoded({
extended: true,
}));
// Serve all static files in public folder
app.use('/', express.static(path.join(__dirname, 'public')));
app.get('/status', function (req, res) {
if (req.accepts('application/json')) {
var data = JSON.parse(req.query.data);
// Make sure we're going to have a website to check
if (Object.keys(data).length) {
// Define variables
var options = _.pick(data, ['url', 'method']);
// Send HTTP request
http.request(options).then((response) => {
// Send status code
res.sendStatus(response.status);
}, (error) => {
// We failed
res.sendStatus(404);
});
}
} else {
res.sendStatus(406);
}
});
// API
app.get('/api/monitors', EndpointCtrl.index);
app.get('/api/results', ResultCtrl.index);
app.post('/api/monitors', EndpointCtrl.post);
app.listen(port, () => {
var monitor = new Monitor();
monitor.init();
console.log('Successfully initialized Uptime');
});