-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
134 lines (110 loc) · 3.78 KB
/
server.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
require('dotenv').config();
const express = require('express');
const app = express();
const AWS = require('aws-sdk');
// port on which the server listens
const port = process.env.PORT;
const region = process.env.REGION;
const accessKeyId = process.env.ACCESS_KEY;
const secretAccessKey = process.env.SECRET_ACCESS_KEY;
const sessionToken = process.env.SESSION_TOKEN;
const TableName = process.env.TABLE_NAME;
let client = new AWS.DynamoDB({
region: region,
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
sessionToken: sessionToken,
});
let count = -1;
let max = -1;
let req_count = 0;
function returnListPage(req, res) {
req_count++;
client.scan({ TableName }, async (err, data) => {
res.contentType = 'text/html; charset=utf-8';
if (err) {
console.log(err);
res.write('<!DOCTYPE html> ');
res.write('<html lang="en"><body> ERROR. See server logs for more info. </body></html>');
} else {
if (count==-1) count = data.Items.length;
const items = [];
for (const i in data.Items) {
items.push(data.Items[i]['DATA'].S);
if (max < data.Items[i]['SORT_KEY'].N) max = data.Items[i]['SORT_KEY'].N;
if (req_count >= 20) {
await new Promise((resolve, _reject) => {
setTimeout(() => resolve(null), 10); // NO_CHECK_IN add delay of 10ms per record to test latency
});
}
}
// send the obtained rows onto the response
res.write(`
<!DOCTYPE html>
<html lang="en">
<head>
<title>Simple Node Server as Terraform and Maeztro Demo</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>
<body>
<div class="container">
<h2 style="margin-top: 24pt;">Node Server</h2>
<div class="well">
<p>Hello. I am a simple Node server managed by Maeztro.</p>
<p>I am connecting to a DynamoDB table and retrieving data. ${items.length} items found.</p>
<form action="/add" method="post">
<button name="add_record" value="true">Add Item</button>
</form>
<br/>
`);
if(items.length !== 0) {
res.write(`
<table class="table table-hover table-striped">
<thead><tr><td>Here is the data:</td></tr></thead>
<tbody>${items.map(item => `
<tr><td>${item}</tr></td>`).join('')}
</tbody>
</table>
`);
} else {
res.write(`
<p>There is no data.</p>
`);
}
res.write(`
</div>
</div>
</body>
</html>
`);
}
res.end();
});
};
app.get("/", returnListPage)
app.get("/request-count" , (req, res) => {
res.write(`${req_count}`);
res.end();
});
app.post("/add", (req, res) => {
client.putItem(
{
TableName,
"Item": {
"HASH_KEY": {"S": "web"},
"SORT_KEY": {"N": `${++max}`},
"DATA": {"S": `web added record ${max}`},
}
},
(err, data) => {
if (err) console.warn("ERR", err);
res.redirect('/');
});
});
app.listen(port, () => {
console.log(`Listening on port ${port}`);
});