-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.js
57 lines (49 loc) · 1.72 KB
/
init.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
const knex = require("knex");
const bodyParser = require("body-parser");
const compression = require("compression");
const moment = require('moment');
const { knexSnakeCaseMappers } = require("objection");
const dbConfig = require("./src/config/knexfile");
const syslog = require("./src/services/log");
const errorHandler = require("./src/middleware/error");
const auth = require("./src/auth/router");
const passport = require("./src/services/passport");
const { UnauthorizedError } = require('./src/services/error');
module.exports = (app) => {
syslog.info('Configuring DB ', process.env.NODE_ENV);
const opts = {
...dbConfig[process.env.NODE_ENV],
...knexSnakeCaseMappers()
};
const dbClient = knex(opts);
const DATE_OID = 1082;
dbClient.client.driver.types
.setTypeParser(DATE_OID, (val) => (val == null ? null : moment.tz(val, 'America/Denver')));
app.use(compression());
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(passport.initialize());
syslog.info("Registering modules");
const baseUrl = `/${process.env.VERSION}`;
app.use(baseUrl, auth);
app.use((req, res, next) => {
const tokenError = new UnauthorizedError(`Invalid token provided`);
passport.authenticate('JWT', {
session: false
}, (err, user, info) => {
if (err) {
console.log('Error in token : ', err);
next(tokenError);
} else {
if (!user) {
next(tokenError);
}
req.user = user;
next();
}
})(req, res, next);
});
app.use(baseUrl, errorHandler);
};