-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
164 lines (137 loc) · 3.99 KB
/
main.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
154
155
156
157
158
159
160
161
162
163
164
require('dotenv').config();
const path = require('path');
const express = require('express');
const redis = require('redis');
const { Pool } = require('pg');
const withApi = require('./middleware/withApi');
const withRedirects = require('./middleware/withRedirects');
const withCaching = require('./middleware/withCaching');
const withLogging = require('./middleware/withLogging');
const withRateLimiter = require('./middleware/withRateLimiter');
const lw = require('@google-cloud/logging-winston');
const { logger, loggingWinston } = require('./util/logger');
const d = require('debug')('app:main');
/**
* Global application settings are exported here
*/
let settings;
if (process.env.DOCKER) {
d('(DOCKER)');
settings = require('./ocurl.conf.docker.json');
} else if (process.env.NODE_ENV && process.env.NODE_ENV === 'production') {
d('(PRODUCTION)');
settings = require('./ocurl.conf.prod.json');
} else {
d('(DEVELOPMENT)');
settings = require('./ocurl.conf.dev.json');
}
module.exports.settings = settings;
global.__PROJECT_PATH_ROOT = path.resolve(__dirname);
const { withMapping } = require('./middleware/withMapping');
const {
INIT_URL_MAPPING,
HEALTH_CHECK,
} = require('./util/database/statements2');
const helmet = require('helmet');
/**
* Database connection
*/
const pgPool = new Pool();
// Run an intial query to test the pool
pgPool.query(HEALTH_CHECK);
var pgConnectionStatus = 0;
pgPool.on('connect', (client) => {
d('Connection established to postgresql');
client.query(INIT_URL_MAPPING);
pgConnectionStatus = 1;
});
pgPool.on('error', (err) => {
d('Connection to postgresql lost', err);
pgConnectionStatus = 0;
});
module.exports.pgConnectionStatus = () => pgConnectionStatus;
/**
* Cache connection
*/
//#region cache
const redisClient = redis.createClient(
settings.redis.port,
settings.redis.hostname
);
// 0 : Not connected
// 1 : Connected
var redisConnectionStatus = 0;
redisClient.on('error', function (err) {
redisConnectionStatus = 0;
d('Redis Error occured', err);
});
redisClient.on('connect', function () {
redisConnectionStatus = 1;
d(`Connected to Redis at ${settings.redis.hostname}:${settings.redis.post}`);
});
redisClient.on('ready', function () {
redisConnectionStatus = 1;
d(`Redis is ready ${settings.redis.hostname}:${settings.redis.post}`);
});
redisClient.on('end', function () {
redisConnectionStatus = 0;
d(`Redis connected ended ${settings.redis.hostname}:${settings.redis.post}`);
});
module.exports.redisConnectionStatus = () => redisConnectionStatus;
//#endregion
const app = express();
const port = process.env.PORT || 3000;
app.use(
helmet({
contentSecurityPolicy: {
directives: {
defaultSrc: ["'self'"],
scriptSrc: [
"'self'",
'*.bootstrapcdn.com',
'*.googletagmanager.com',
'*.jsdelivr.net',
'code.jquery.com',
'https://www.google-analytics.com',
'www.googletagmanager.com',
],
styleSrc: ["'self'", '*.bootstrapcdn.com'],
imgSrc: [
"'self'",
'https://www.google-analytics.com',
'www.googletagmanager.com',
],
},
},
})
);
if (logger && loggingWinston) {
lw.express
.makeMiddleware(logger, loggingWinston)
.then((middleware, err) => {
if (!err) {
gcpLogger = middleware;
app.use(gcpLogger);
d('Initialized Stackdriver logging');
}
})
.catch(() => { d('No Stackdriver logging'); });
}
app.use(express.static(path.join(__dirname, 'public')));
app.use(withLogging(app));
app.use(withRateLimiter(redisClient));
app.use(withCaching(redisClient));
app.use(withMapping(pgPool, redisClient));
app.use(withApi(pgPool, redisClient));
app.use(withRedirects());
app.enable('trust proxy', settings.trust_proxy);
app.listen(port, settings.hostname, () => {
d(`One Click URL (ocurl) server started on port ${port}`);
});
process.on('exit', () => {
pgPool.end();
});
/**
* Singleton access point for middlewares
*/
module.exports.app = app;