-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
75 lines (63 loc) · 2 KB
/
index.js
File metadata and controls
75 lines (63 loc) · 2 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
"use strict";
function ppobj(obj) {
if (typeof obj === "object") {
return JSON.stringify(obj, null, 4);
} else {
return obj;
}
}
let loggerConfig = {
"appenders": [
{ "type":"console"},
{
"type": "file",
"filename": "check_conn.log",
"maxLogSize": 1000000,
"backups": 10
}
]
};
let log4js = require("log4js");
log4js.configure(loggerConfig);
let logger = log4js.getLogger("main");
let mlogger = require("mongodb").Logger;
mlogger.setLevel("debug");
mlogger.setCurrentLogger(function (msg, context) {
logger.debug("MongoDBLog: " + ppobj(context));
});
let express = require("express");
let mongo = require("mongodb");
let mongoClient = require("mongodb").MongoClient;
let dbUrl = process.env.CHECK_CONN_DB_URL;
let app = express();
let mongoDb = undefined;
app.get("/ntokens", function (req, res) {
logger.info("return how many tokens in the push_notification database");
return res.status(200).json({"status": "yea!!"});
});
app.get("/dbstats", function (req, res) {
logger.info("return dbStats for push_notification database");
mongoDb.command({"dbStats": 1}, function (err, results) {
return res.status(200).json({"error": err, "results": results});
});
});
let http_port = process.env.CHECK_CONN_PORT || 5788;
var server = app.listen(http_port, function () {
logger.info("server is running on http port %d", http_port);
logger.info("attempt to connect to MongoDB URL: ", dbUrl);
// I'm not sure these are setup correclty, they don't seem to have any affect
let dbOptions = {
autoReconnect: true,
reconnectTries: 10,
reconnectInterval: 1000,
readPreference: mongo.ReadPreference.NEAREST
};
mongoClient.connect(dbUrl, dbOptions, function (err, db) {
if (err) {
logger.error("error connecting to DB: ", err);
} else {
logger.info("connected to DB");
mongoDb = db;
}
});
});