-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
executable file
·100 lines (85 loc) · 2.78 KB
/
index.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
#!/usr/bin/env node
const path = require('path');
const express = require('express');
const bff = require('express-bff');
const passport = require('passport');
const { getReasonPhrase } = require('http-status-codes');
const { dev, http: httpConf, backend, PORT } = require('./config');
const sseMiddleware = require('./middlewares/sse');
const { USERS_TABLE, selectFirstUser } = require('./services/db');
const { middleware: apiTokenMiddleware } = require('./middlewares/apiToken');
const { serializer, stringify } = require('./_helpers');
const app = express();
if (httpConf.trust_proxy) {
app.set('trust proxy', 1); // if behind proxy
}
passport.serializeUser((user, done) => done(null, user.id));
passport.deserializeUser(async (id, done) => {
try {
const user = await selectFirstUser({ [`${USERS_TABLE}.id`]: id });
done(null, user);
} catch (err) {
done(err, null);
}
});
// apiTokenMiddleware must be registered before bff.sse
// see https://github.com/PaulMaly/express-bff/blob/d217d0ad1e11d977fe87db28fe59511b3ef26611/index.js#L15-L16
app.use(apiTokenMiddleware);
app.use(express.json({ limit: '5mb' }));
bff(app, {
security: {
cors: {
credentials: true,
origin: true,
},
csrf: false,
secure: httpConf.force_https,
},
session: {
persist: false,
resave: true,
cookie: {
secure: false, // TODO FIXME?
httpOnly: true,
sameSite: false,
maxAge: 86400000,
},
},
sse: {
path: '/stream',
serializer: serializer(),
},
api: {
dir: path.join(__dirname, 'routes'),
},
proxy: {
target: backend.baseURL,
},
static: false,
ssr: false,
middlewares: [passport.initialize(), passport.session(), sseMiddleware],
});
app.use((err, req, res, next) => {
const DB = err.code !== 'ECONNREFUSED';
const status = err.status || (!req.user && DB ? 401 : 500);
const error = err || { status, error: getReasonPhrase(status) };
console.error(error);
if (!req.user)
req.session &&
req.logout(function (err) {
if (err) {
return next(err);
}
});
if (res.headersSent) {
res.sse.sendTo({ reqId: req.id, data: [error] }, 'errors');
} else {
res.status(status).set('Content-Type', 'application/json').send(stringify(error));
}
});
console.log(`***Running ${process.execPath} as of ${process.version}`);
app.listen(PORT, () => {
// NB app.listen(PORT, "0.0.0.0") to exhibit to the outside
console.log(`***BFF in ${dev ? 'development' : 'production'} mode listens to port ${PORT}`);
console.log(`***Backend is expected to run at ${backend.baseURL}`);
});