-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (63 loc) · 1.8 KB
/
Copy pathindex.js
File metadata and controls
80 lines (63 loc) · 1.8 KB
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
'use strict';
const fs = require('fs');
const path = require('path');
const dotenv = require('dotenv');
const uuid = require('uuid');
const DBContext = require('./db_context');
const MongoContext = require('./mongo_context');
const RedisContext = require('./redis_context');
const Handler = require('./handler');
const AppError = require('./error');
AppError.initializeErrors();
async function bootstrap() {
const loadedEnv = dotenv.parse(fs.readFileSync(path.join(__dirname, '.env'), { encoding: 'utf8' }));
for (const key in loadedEnv) {
process.env[key] = loadedEnv[key];
}
DBContext.configure({
connection_string_key: 'DB_CONNECTION_STRING',
path: 'jakartajs-demo-models',
sync_db: true
});
MongoContext.configure({
connection_string_key: 'MONGO_CONNECTION_STRING',
db_name: 'jakartajs_demo_db'
});
RedisContext.configure({
connection_string_key: 'REDIS_CONNECTION_STRING',
connection_pool: {
min: 0,
max: 10
}
});
}
async function teardown() {
return Promise.all([
DBContext.closeContext(),
MongoContext.closeContext(),
RedisContext.closeContext()
]);
}
exports.handler = async (event, context) => {
context.request_id = uuid.v4();
await bootstrap();
let result = null;
let error = null;
try {
if (!Handler[event.action]) {
throw AppError.NotImplemented('No Such Action Defined');
}
result = await Handler[event.action](event, context);
} catch (err) {
error = err;
if (!error.code) {
error = new AppError.InternalServerError(error.message);
}
}
await teardown();
if (error) {
throw error;
}
return result;
};
module.exports = exports;