-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogdata.js
131 lines (112 loc) · 4.72 KB
/
logdata.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
'use strict';
//Copyright 2017 Tim Coates
//
//Licensed under the Apache License, Version 2.0 (the "License");
//you may not use this file except in compliance with the License.
//You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//Unless required by applicable law or agreed to in writing, software
//distributed under the License is distributed on an "AS IS" BASIS,
//WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//See the License for the specific language governing permissions and
//limitations under the License.
var AWS = require("aws-sdk");
var favicon = require ('./favicon.js');
var docClient = null;
var tblName = null;
module.exports.entrypoint = (event, context, callback) => {
// Instantiate any static stuff for speed
setup();
// Log our incoming event for ease of debugging
console.log("Event: " + JSON.stringify(event));
// Check whether the event was just a wakeup from SNS (to warm this lambda up)
if(typeof event.Records != 'undefined') {
// It was, we've done setup, so now quit quickly.
context.callbackWaitsForEmptyEventLoop = false;
callback(null, null);
} else {
// It's a real request
if(event.queryStringParameters == null) {
// Here we've been asked for all the data, as JSON
var params = {
TableName: tblName,
ProjectionExpression: "id, SOAPAction, request_time",
Limit: 1000
};
console.log("Params: " + JSON.stringify(params));
docClient.scan(params, function (err, result) {
if(err) {
console.log("Error: " + JSON.stringify(err));
callback(err, "ERROR");
} else {
var reply = {
"statusCode": 200,
"headers": { "Content-Type": "application/json" },
"body": JSON.stringify(result.Items)
};
console.log("Reply: " + JSON.stringify(reply));
callback(null, reply);
}
});
} else {
// Here we're detailing one log item, so we need to go a getItem rather than a scan
var params = {
TableName: tblName,
Key:{ "id": event.queryStringParameters.id }
};
console.log("Params: " + JSON.stringify(params));
docClient.get(params, function(err, data) {
if (err) {
console.error("Unable to read item. Error JSON:", JSON.stringify(err, null, 2));
callback(err, "Error");
} else {
var b64Data = favicon.b64Data;
var body = "<html><head>\n" +
"<meta name='viewport' content='width=device-width, initial-scale=1, shrink-to-fit=no'>\n" +
"<link rel='stylesheet' href='https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-beta/css/bootstrap.min.css' " +
"integrity='sha384-/Y6pD6FV/Vv2HJnA6t+vslU6fwYXjCFtcEpHbNJ0lyAFsXTsjBbfaDjzALeQsN6M' crossorigin='anonymous'>\n" +
"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js'></script>\n" +
"<link rel='stylesheet' href='https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css'>\n" +
"<script src='https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js'></script>\n" +
"<link id='favicon' rel='shortcut icon' type='image/png' href='data:image/png;base64," + b64Data + "'>\n" +
"<title>SMSP Mock - Log item: " + data.Item.id + "</title>\n"+
"</head>\n"+
"<body>\n" +
"<div class='container'>\n" +
" <div class='jumbotron'><h1><a href='/'>Log Item: " + data.Item.id + "</a></h1></div>\n" +
" <div><h2>ID:</h2>" + data.Item.id + "</div>\n" +
" <div><h2>SOAP Action:</h2>" + data.Item.SOAPAction + "</div>\n" +
" <div><h2>Request time:</h2>" + new Date(Date.parse(data.Item.request_time)).toLocaleString() + "</div>\n" +
" <div><h2>Request:</h2><figure class='highlight'><pre class='.pre-scrollable' style='font-size: x-small;'>" + htmlEntities(data.Item.request) + "</pre></figure></div>\n" +
" <div><h2>Response time:</h2>" + new Date(Date.parse(data.Item.response_time)).toLocaleString() + "</div>\n" +
" <div><h2>Response:</h2><figure class='highlight'><pre class='.pre-scrollable' style='font-size: x-small;'>" + htmlEntities(data.Item.response) + "</pre></figure></div>\n" +
"</div>\n"+
"</body>\n" +
"</html>";
// Generate the response object
var reply = {
"statusCode": 200,
"headers": { "Content-Type": "text/html" },
"body": body
};
callback(null, reply);
}
});
}
}
}
// Helper function to make XML safe to display in html
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
// Here we create some global stuff, eg DynamoDB Client, to improve warm performance.
function setup() {
if(docClient == null) {
docClient = new AWS.DynamoDB.DocumentClient();
}
if(tblName == null) {
tblName = process.env.stageName + '-pds-messages';
}
}