-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
153 lines (130 loc) · 5.19 KB
/
app.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
'use strict';
const SwaggerExpress = require('swagger-express-mw');
const swaggerUi = require('swagger-ui-express');
const yaml = require('js-yaml');
const fs = require('fs');
const app = require('express')();
const mongoose = require('mongoose');
const logger = require('./api/util/logger.js');
const cors = require('cors');
const morgan = require('morgan');
const agendash = require('agendash');
const agenda = require('./api/jobs/jobs');
/*// Searching a memory leak
const memwatch = require('memwatch-next');
const heapdump = require('heapdump');
const util = require('util');
let snapshotTaken = false,
hd;
memwatch.on('leak', function(info) {
heapdump.writeSnapshot(function(err, filename) {
logger.error({leak: "dump written to " + filename});
});
logger.error({leak: info});
let diff = hd.end();
snapshotTaken = false;
logger.error({leak: util.inspect(diff, {showHidden:false, depth:4})});
});
memwatch.on('stats', function(stats) {
logger.error({stats: stats});
if(snapshotTaken===false){
heapdump.writeSnapshot(function(err, filename) {
logger.error({stats: "dump written to " + filename});
hd = new memwatch.HeapDiff();
snapshotTaken = true;
});
}/!* else {
var diff = hd.end();
snapshotTaken = false;
logger.info(util.inspect(diff, {showHidden:false, depth:4}));
}*!/
});*/
module.exports = app; // for testing
let db = mongoose.connection;
db.on('error', console.error);
db.once('open', function() {
logger.info("DB successfully opened.")
});
// Configuring Passport
let passport = require('./api/util/passport.js');
const expressSession = require('express-session');
const MongoStore = require('connect-mongo')(expressSession);
let config = require("./config/config.js");
if(process.argv.indexOf("test") > 0 ){
config = Object.assign(config, require("./test/api/config.js"));
}
mongoose.set(config.LOG_LEVEL, function (collectionName, method, query, doc){
logger.info("MONGOOSE", collectionName + "." + method + "(" + JSON.stringify(query) + ")");
});
logger.info("Running config:", {config : JSON.stringify(config)});
let uri = "mongodb://" + config.DB.HOST + ":" + config.DB.PORT + "/" + config.DB.SCHEMA;
//mongoose.connect(uri);
const options = {
useNewUrlParser: true,
//useCreateIndex: true,
//useFindAndModify: false,
//autoIndex: false, // Don't build indexes
reconnectTries: Number.MAX_VALUE, // Never stop trying to reconnect
reconnectInterval: 500, // Reconnect every 500ms
poolSize: 10, // Maintain up to 10 socket connections
// If not connected, return errors immediately rather than waiting for reconnect
bufferMaxEntries: 0,
connectTimeoutMS: 10000, // Give up initial connection after 10 seconds
socketTimeoutMS: 45000, // Close sockets after 45 seconds of inactivity
family: 4,// Use IPv4, skip trying IPv6
useMongoClient: true
};
// global.Promise;
mongoose.Promise = require('bluebird');
mongoose.connect(uri, options);
let swaggerDocument = yaml.safeLoad(fs.readFileSync('./api/swagger/swagger.yaml', 'utf8'));
swaggerDocument.host = config.HOST;
swaggerDocument.basePath = config.BASEPATH;
swaggerDocument.securityDefinitions = null;
app.use('/docs', swaggerUi.serve, swaggerUi.setup(swaggerDocument, false, {validatorUrl : null, }));
app.use('/agendash', agendash(agenda));
SwaggerExpress.create({appRoot: __dirname, securityHandlers: {
basicAuth: function (req, authOrSecDef, callback){
passport.isAuthenticated(req, req.res, callback);
},
login: function (req, authOrSecDef, callback){
passport.authenticate('local', function (err, user, info) {
if (err) {
callback(new Error('Error in passport authenticate'));
} else if (!user) {
callback(new Error('Failed to authenticate'));
} else {
req.user = user;
callback();
}
})(req, null, callback);
}
}}, function(err, swaggerExpress) {
if (err) { throw err; }
app.use(expressSession({secret: 'mySecretKey', resave: false, saveUninitialized: false, store: new MongoStore({mongooseConnection: mongoose.connection })}));
app.use(passport.initialize());
app.use(passport.session());
app.use(cors({origin: "http://localhost:4200", credentials: true}));
// enable http logging
morgan.token('username', function (req, res) {
return req.user ? req.user.username : "";
});
morgan.token('userid', function (req, res) {
return req.user ? req.user._id : "";
});
morgan.token('body', function (req, res) {
return req.body ? JSON.stringify(req.body) : "";
});
app.use(morgan(':method :url :status :response-time ms - :res[content-length] - :username :userid - :body',{
skip: function (req, res) {
return res.statusCode < 400
}, stream: logger.streamError
}));
app.use(morgan(':method :url :status :response-time ms - :res[content-length] - :username :userid - :body',{
skip: function (req, res) {
return res.statusCode >= 400
}, stream: logger.streamInfo
}));
swaggerExpress.register(app);
app.listen(config.PORT);
});