-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
69 lines (54 loc) · 1.97 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
require('dotenv').config();
const Koa = require('koa');
const session = require('koa-session');
const passport = require('koa-passport');
const helmet = require('koa-helmet');
const koaEjs = require('koa-ejs');
const path = require('path');
const { cookieSecret, port } = require('./config');
const router = require('./router');
const {
configurePassport,
checkAuthenticatedMiddleware,
} = require('./passport');
const { proxyMiddleware, proxyWebSocket } = require('./proxy');
const {
refreshEksTokenMiddleware,
checkAwsCredentialsMiddleware,
} = require('./aws');
const log = require('./logger');
const startServer = async () => {
await configurePassport();
const app = new Koa();
// Some prerequisites and securtity features
app.keys = [cookieSecret];
app.proxy = true; // Trust down stream proxy header fields
// NOTE: Do not parse the body of the incoming request (using body parser or the like)
// or the proxy will not be able to forward it on.
app.use(session({}, app));
app.use(helmet());
// Set up EJS for rendering HTML
koaEjs(app, {
root: path.join(__dirname, '../templates'),
viewExt: 'ejs',
layout: false,
});
// Set up passport for authentication
app.use(passport.initialize());
app.use(passport.session());
// Redirect users to the login page if they are not authenticated
app.use(checkAuthenticatedMiddleware);
// Redirect users to the login page if their AWS creds have expired
app.use(checkAwsCredentialsMiddleware);
// Refresh the EKS auth token if it has expired
app.use(refreshEksTokenMiddleware);
// Handle routes (e.g. for login)
app.use(router.routes());
app.use(router.allowedMethods());
// Proxy all requests on any path not defined in the router to proxyHost
app.use(proxyMiddleware);
const server = app.listen(port);
server.on('upgrade', proxyWebSocket);
log.info(`Proxy server running on port ${port}`);
};
startServer();