-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
330 lines (280 loc) · 10.5 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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
"use strict";
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* A program to compare the structure of two databases and list the differences. */
/* */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
const Koa = require('koa'); // Koa framework
const body = require('koa-body'); // body parser
const mysql = require('mysql2/promise'); // fast mysql driver
const debug = require('debug')('app'); // small debugging utility
const cors = require('koa2-cors'); // CORS for Koa 2
const jwt = require('jsonwebtoken'); // JSON Web Token implementation
const bunyan = require('bunyan'); // logging
const koaLogger = require('koa-bunyan'); // logging
const views = require('koa-views'); // koa template rendering middleware
const Twig = require('twig'); // twig php html templates
const twig = Twig.twig;
const serve = require('koa-static-server');
// const render = require('koa-views-render');
require('dotenv').config(); // loads environment variables from .env file (if available - eg dev env)
const app = new Koa();
app.env = process.env.ENVIRONMENT;
// MySQL connection pool (set up on app initialization)
// const config = {
// host: process.env.DB_HOST,
// port: process.env.DB_PORT || 3306,
// user: process.env.DB_USER,
// password: process.env.DB_PASSWORD,
// database: process.env.DB_DATABASE,
// charset: 'utf8mb4',
// };
// MySQL connection pool (set up on app initialization)
const configCompareDB1 = {
host: process.env.DB_COMPARE_1_HOST,
port: process.env.DB_COMPARE_1_PORT || 3306,
user: process.env.DB_COMPARE_1_USER,
password: process.env.DB_COMPARE_1_PASSWORD,
database: process.env.DB_COMPARE_1_DATABASE,
charset: 'utf8mb4',
};
// MySQL connection pool (set up on app initialization)
const configCompareDB2 = {
host: process.env.DB_COMPARE_2_HOST,
port: process.env.DB_COMPARE_2_PORT || 3306,
user: process.env.DB_COMPARE_2_USER,
password: process.env.DB_COMPARE_2_PASSWORD,
database: process.env.DB_COMPARE_2_DATABASE,
charset: 'utf8mb4',
};
// global.connectionPool = mysql.createPool(config); // put in global to pass to sub-apps
global.connectionPoolCompareDB1 = mysql.createPool(configCompareDB1); // put in global to pass to sub-apps
global.connectionPoolCompareDB2 = mysql.createPool(configCompareDB2); // put in global to pass to sub-apps
/* set up middleware which will be applied to each request - - - - - - - - - - - - - - - - - - - */
// return response time in X-Response-Time header
app.use(async function responseTime(ctx, next) {
const t1 = Date.now();
await next();
const t2 = Date.now();
ctx.set('X-Response-Time', Math.ceil(t2 - t1) + 'ms');
});
// HTTP compression
// app.use(compress({}));
// only search-index www subdomain
app.use(async function robots(ctx, next) {
await next();
ctx.response.set('X-Robots-Tag', 'noindex, nofollow');
});
// SERVE STATIC FILES
app.use(serve({rootDir: 'public', rootPath: '/public'}));
// parse request body into ctx.request.body
app.use(body());
// body should only ever be JSON so if it is a string then parse it.
app.use(async (ctx, next) => {
try {
if (typeof ctx.request.body === 'string') {
ctx.request.body = JSON.parse(ctx.request.body);
}
} catch (e) {
// if it is not JSON then we just let it go.
}
await next();
});
// sometimes useful to be able to track each request...
app.use(async function (ctx, next) {
debug(ctx.method + ' ' + ctx.url);
await next();
});
// handle thrown or uncaught exceptions anywhere down the line
app.use(async function handleErrors(ctx, next) {
try {
await next();
} catch (e) {
ctx.status = e.status || 500;
switch (ctx.status) {
case 204: // No Content
console.log('status', ctx.status);
break;
case 401: // Unauthorized
ctx.set('WWW-Authenticate', 'Bearer');
// ctx.body = { message: "login error"};
break;
case 403: // Forbidden
case 404: // Not Found
case 406: // Not Acceptable
case 409: // Conflict
ctx.body = {
message: e.message
};
break;
default:
case 500: // Internal Server Error (for uncaught or programming errors)
console.error(ctx.status, e.message);
ctx.body = {
message: e.message
};
if (app.env !== 'production') ctx.body.stack = e.stack;
ctx.app.emit('error', e, ctx); // github.com/koajs/koa/wiki/Error-Handling
break;
}
}
});
// app.use(cors());
// set up MySQL connection - App DB
// app.use(async function mysqlConnection(ctx, next) {
// try {
// // keep copy of ctx.state.db in global for access from models
// ctx.state.db = global.db = await global.connectionPool.getConnection();
// ctx.state.db.connection.config.namedPlaceholders = true;
// // traditional mode ensures not null is respected for unsupplied fields, ensures valid JavaScript dates, etc
// await ctx.state.db.query('SET SESSION sql_mode = "TRADITIONAL"');
// await next();
// ctx.state.db.release();
// } catch (e) {
// // note if getConnection() fails we have no this.state.db, but if anything downstream throws,
// // we need to release the connection
// if (ctx.state.db) ctx.state.db.release();
// throw e;
// }
// });
// set up MySQL connection - CompareDB1
app.use(async function mysqlConnectionCompareDB1(ctx, next) {
try {
// keep copy of ctx.state.db in global for access from models
ctx.state.comparedb1 = global.comparedb1 = await global.connectionPoolCompareDB1.getConnection();
ctx.state.comparedb1.connection.config.namedPlaceholders = true;
// traditional mode ensures not null is respected for unsupplied fields, ensures valid JavaScript dates, etc
await ctx.state.comparedb1.query('SET SESSION sql_mode = "TRADITIONAL"');
await next();
ctx.state.comparedb1.release();
} catch (e) {
// note if getConnection() fails we have no this.state.db, but if anything downstream throws,
// we need to release the connection
if (ctx.state.comparedb1) ctx.state.comparedb1.release();
throw e;
}
});
// set up MySQL connection - CompareDB2
app.use(async function mysqlConnectionCompareDB2(ctx, next) {
try {
// keep copy of ctx.state.db in global for access from models
ctx.state.comparedb2 = global.comparedb2 = await global.connectionPoolCompareDB2.getConnection();
ctx.state.comparedb2.connection.config.namedPlaceholders = true;
// traditional mode ensures not null is respected for unsupplied fields, ensures valid JavaScript dates, etc
await ctx.state.comparedb2.query('SET SESSION sql_mode = "TRADITIONAL"');
await next();
ctx.state.comparedb2.release();
} catch (e) {
// note if getConnection() fails we have no this.state.db, but if anything downstream throws,
// we need to release the connection
if (ctx.state.comparedb2) ctx.state.comparedb2.release();
throw e;
}
});
// logging
const access = {
type: 'rotating-file',
path: './logs/api-access.log',
level: 'trace',
period: '1d',
count: 4,
};
const error = {
type: 'rotating-file',
path: './logs/api-error.log',
level: 'error',
period: '1d',
count: 4,
};
const logger = bunyan.createLogger({
name: 'api',
streams: [access, error]
});
app.use(koaLogger(logger, {}));
// ------------ routing
// public (unsecured) modules first
// app.use(async function cleanJSON(ctx, next) {
// if (!typeof ctx.request.body === 'object') {
// ctx.request.body = JSON.parse(ctx.request.body);
// }
// await next();
// });
// const options = {
// extensions: [
// {
// file: '/Users/darin/Documents/node/comparedbstructure/views/index.twig',
// func: 'myTwigExtension'
// }
// ]
// };
// // Must be used before any router is used
// app.use(views(__dirname + '/views', {
// map: {
// html: 'underscore'
// }
// }));
// app.use(async function (ctx, next) {
// ctx.state = {
// session: this.session,
// title: 'app'
// };
// await renderFile('/Users/darin/Documents/node/comparedbstructure/views/index.twig', options, function (error, template) {
// {
// message : "Hello World"
// }
// });
// });
// renderFile('/Users/darin/Documents/node/comparedbstructure/views/index.twig', options, function (error, template) {
// });
// This section is optional and used to configure twig.
// app.set("twig options", {
// allow_async: true, // Allow asynchronous compiling
// strict_variables: false
// });
// koa-views-render
// app.use(async function (ctx) {
// await ctx.render('template.twig')
// });
// SET VIEWS DIRECTORY
app.use(views(__dirname + '/views', { map: {html: 'twig', twig: 'twig' }}));
app.use(require('./routes/routes-root.js'));
app.use(require('./routes/routes-dbcompare.js'));
//app.use(require('./routes/routes-auth.js'));
//app.use(require('./routes/routes-util.js'));
// remaining routes require JWT auth (obtained from /auth and supplied in bearer authorization header)
// app.use(async function verifyJwt(ctx, next) {
// if (!ctx.header.authorization) ctx.throw(401, 'Authorisation required');
// const [ scheme, token ] = ctx.header.authorization.split(' ');
// if (scheme != 'Bearer') ctx.throw(401, 'Invalid authorisation');
//
// try {
// const payload = jwt.verify(token, process.env.JWT_KEY); // throws on invalid token
// // valid token: accept it...
//
// let sqla = `select status from user where id = ${payload.id}`;
// const [[res]] = await global.db.query(sqla);
//
// if (res.status === 0){
// ctx.body = "Account Disabled"
// throw({ status: 401, message: 'Account Disabled' });
// }
//
// ctx.state.user = payload; // for user id to look up user details
// } catch (e) {
// if (e.message === 'invalid token') ctx.throw(401, 'Invalid JWT'); // Unauthorized
// ctx.throw(e.status || 401, e.message); // Internal Server Error
// }
//
// await next();
//
// });
// app.use(renderFile('./views/template.twig', options, function (error, template) {
// // ... do something with the rendered template. :)
// }));
/* create server - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
app.listen(process.env.PORT || 3000);
console.info(
`${process.version} listening on port ${process.env.PORT || 3000} (${app.env}/${/*config.database*/ "database"})
http://localhost:${process.env.PORT || 3000}`
);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
module.exports = app;