-
Notifications
You must be signed in to change notification settings - Fork 31
/
server.js
333 lines (300 loc) · 10.9 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
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
331
332
333
const serverless = require('serverless-http')
const fs = require('fs')
const cors = require('cors')
const express = require('express')
const ReactDOMServer = require('react-dom/server')
const App = require('./dist/index.server.bundle.js')
const bodyParser = require('body-parser')
const AWS = require('aws-sdk')
const app = express()
const template = fs.readFileSync(`${__dirname}/dist/index.html`, 'utf8') // stupid simple template.
const port = process.env.SERVER_PORT || 3000
const tableName = process.env.TODO_TABLE || 'todos'
const eventQueueName = process.env.TODO_EVENT_QUEUE || 'todos-events'
const awsRegion = process.env.AWS_REGION || 'eu-west-1'
// Lazy initialize it later
let eventQueueUrl = null
// Set default region
process.env.AWS_REGION = awsRegion
AWS.config.update({ region: awsRegion })
const SQS = new AWS.SQS({ apiVersion: '2012-11-05' })
const dynamoDb = new AWS.DynamoDB.DocumentClient()
// These are loaded if you call POST /api/init
const initialTodos = [
{ id: 'ed0bcc48-bbbe-5f06-c7c9-2ccb0456ceba', title: 'Wake Up.', completed: true },
{ id: '42582304-3c6e-311e-7f88-7e3791caf88c', title: 'Grab a brush and put a little makeup.', completed: true },
{ id: '036af7f9-1181-fb8f-258f-3f06034c020f', title: 'Write a blog post.', completed: false },
{ id: '1cf63885-5f75-8deb-19dc-9b6765deae6c', title: 'Create a demo repository.', completed: false },
{ id: '63a871b2-0b6f-4427-9c35-304bc680a4b7', title: '??????', completed: false },
{ id: '63a871b2-0b6f-4422-9c35-304bc680a4b7', title: 'Profit.', completed: false },
]
app.use(cors())
app.use(bodyParser.json({ strict: false }))
// Disable 304 support, works wrong IMO
app.set('etag', false)
// Always send last-modified as current time
app.get('/*', function(req, res, next){
res.setHeader('Last-Modified', (new Date()).toUTCString())
next()
})
// Static files (disable etag)
app.use(express.static(`${__dirname}/dist`, { etag: false, index: false }))
app.use('/assets', express.static(`${__dirname}/dist/assets`, { etag: false }))
app.use('/css', express.static(`${__dirname}/dist/css`, { etag: false }))
// Obtain record
app.get('/api/todo/:id', function (req, res) {
console.log('Getting record id = ' + req.params.id)
dynamoDb.get({ TableName: tableName, Key: { id: req.params.id }}).promise().then((result) => {
if (result.Item) {
const { id, title, completed } = result.Item
res.json({ id, title, completed })
} else {
res.status(404).json({ error: 'Record not found id = ' + req.params.id })
}
}).catch(error => {
console.error('Failed to get record id = ' + req.params.id, error)
res.status(500).json({ error: 'Failed to get record id = ' + req.params.id })
})
})
app.delete('/api/todo/:id', function (req, res) {
console.log('Deleting ', req.params.id)
dynamoDb.delete({ TableName: tableName, Key: { id: req.params.id }}).promise().then(() => {
res.json({ deleted: req.params.id })
}).catch(error => {
console.error('Failed to delete', error)
res.status(500).json({ error: 'Failed to delete: ' + JSON.stringify(error) })
})
})
// Clear DynamodDB
function initDynamoDB(res, callback) {
console.log('Initializing DynamoDB', tableName)
dynamoDb.scan({ TableName: tableName }).promise().then(result => {
// All promises to delete
let del = []
if (result.Items && result.Items.length > 0) {
del = result.Items.map(it => dynamoDb.delete({ TableName: tableName, Key: { id: it.id }}).promise())
}
// Wait for delete
console.log('Deleting ' + del.length + ' records')
Promise.all(del).then(() => {
console.log('Deleted ' + del.length + ' records')
// Wait for create
console.log('Adding ' + initialTodos.length + ' records')
const add = initialTodos.map(it => dynamoDb.put({ TableName: tableName, Item: it }).promise())
Promise.all(add).then(() => {
console.log('Added ' + initialTodos.length + ' records')
if (res) res.json({ count: add.length })
if (callback) callback()
}).catch(console.error)
}).catch(console.error)
}).catch (error => {
console.error('Failed to get records', error)
if (res) res.status(500).json({ error: 'Failed to get records: ' + JSON.stringify(error) })
if (callback) callback()
})
}
// Clear DynamoDB
app.post('/api/init', function (req, res) {
initDynamoDB(res)
})
// List all records
app.get('/api/todo', function (req, res) {
console.time('todo-scan')
dynamoDb.scan({ TableName: tableName }).promise().then(result => {
if (result.Items && result.Items.length > 0) {
const all = result.Items.map(item => {
return { id, title, completed } = item
})
res.json(all)
} else {
res.json([])
}
console.timeEnd('todo-scan')
}).catch(error => {
console.log('Failed to get records', error)
res.status(500).json({ error: 'Failed to get records: ' + JSON.stringify(error) })
})
})
// Add new record
app.post('/api/todo', async function (req, res) {
console.log('Adding new todo', req.body)
let { id, title, completed } = req.body
if (typeof id !== 'string') {
res.status(400).json({ error: 'id must be a string: ' + JSON.stringify(req.body) })
return
} else
if (typeof title !== 'string') {
res.status(400).json({ error: 'title must be a string: ' + JSON.stringify(req.body) })
return
}
completed = !!completed // convert to boolean
const params = {
TableName: tableName,
Item: {
id, title, completed
},
}
if (title.startsWith('!')) {
const msg = title.substring(1)
if (eventQueueUrl == null) {
console.log(`Looking for queue url: ${eventQueueName}`)
await SQS.getQueueUrl({ QueueName: eventQueueName }).promise().then(result => {
eventQueueUrl = result.QueueUrl
console.log(`Got todo event queue url: ${eventQueueUrl}`)
}).catch(console.error)
}
console.log(`Sending message to ${eventQueueUrl}: ${msg}`)
SQS.sendMessage({ QueueUrl: eventQueueUrl, MessageBody: msg }).promise().
then(console.log).
catch(console.error)
res.json({ ok: 'ok' })
} else {
dynamoDb.put(params).promise().then(result => {
res.json({ id, title, completed })
}).catch(error => {
console.log('Cant add record: ' + JSON.stringify(params.Item), error)
res.status(500).json({ error: 'Cant add record: ' + JSON.stringify(params.Item) })
})
}
})
app.get('/api/queue', async (req, res) => {
if (eventQueueUrl == null) {
console.log(`Looking for queue url: ${eventQueueName}`)
await SQS.getQueueUrl({ QueueName: eventQueueName }).promise().then(result => {
eventQueueUrl = result.QueueUrl
console.log(`Got todo event queue url: ${eventQueueUrl}`)
}).catch(console.error)
}
SQS.receiveMessage({ QueueUrl: eventQueueUrl }).promise().then(result => {
res.json(result)
}).catch(console.error)
})
// Update existing record
app.post('/api/todo/:id', function (req, res) {
let { id, title, completed } = req.body
if (typeof id !== 'string') {
res.status(400).json({ error: 'id must be a string: ' + JSON.stringify(req.body) })
return
} else
if (typeof title !== 'string') {
res.status(400).json({ error: 'title must be a string: ' + JSON.stringify(req.body) })
return
} else
if (id !== req.params.id) {
res.status(400).json({ error: 'id in body must match id in url' })
}
completed = !!completed // convert to boolean
const params = {
TableName: tableName,
Key: {
id: req.params.id,
},
Item: {
id, title, completed
}
}
dynamoDb.put(params).promise().then(_ => {
res.json({ id, title, completed })
}).catch(error => {
console.log('Failed to update record id = ' + req.params.id, error)
res.status(500).json({ error: 'Failed to update record id = ' + req.params.id })
})
})
// Catchall 404
app.post('/api/*', function (req, res) {
res.status(404).json({ error: 'Not found' })
})
// Render HTML
app.get('/*', (req, res) => {
const props = {}
App.default(req.url, props).then((reactComponent) => {
const result = ReactDOMServer.renderToString(reactComponent)
const html = template.replace('{{thing}}', result).replace('{{props}}', JSON.stringify(props))
res.send(html)
res.end()
}).catch(console.error)
})
// Do something when AWS lambda started
if (process.env.AWS_EXECUTION_ENV !== undefined) {
// _HANDLER contains specific invocation handler for this NodeJS instance
console.log('AWS Lambda started, handler:', process.env._HANDLER)
} else {
// Do something when serverless offline started
if (process.env.IS_OFFLINE === 'true') {
console.log('Serverless offline started.')
} else {
app.listen(port, () => {
console.log(`Listening on port: ${port}`)
})
}
}
process.on('beforeExit', (code) => {
console.log("NodeJS exiting")
})
process.on('SIGINT', _ => {
console.log("Caught interrupt signal")
process.exit(1)
})
module.exports.serverless = serverless(app, {
binary: headers => {
let ct = headers['content-type']
if (ct === undefined) {
console.error('No content-type header: ' + JSON.stringify(headers))
return false
}
// cut ; charset=UTF-8
if (ct.indexOf(';') > 0) {
ct = ct.substring(0, ct.indexOf(';'))
}
let binary = String(ct).match(/image\/.*/) ? true : false
console.log('binary: ' + ct + ' -> binary: ' + binary)
return binary
},
request: function(request, event, context) {
const { method, url } = request
request.__started = new Date().getTime()
console.log(`--> ${method} ${url}`)
},
response: function(response, event, context) {
const { statusCode, statusMessage } = response
const { method, url } = response.req
const now = new Date().getTime()
const elapsed = now - response.req.__started
console.log(`<-- ${statusCode} ${statusMessage} ${method} ${url} Δ ${elapsed}ms`)
}
})
/**
* Subscribed to SQS, returns event in the form of
* {
* Records: [
* {
* messageId: 'fec4fd9f-1a09-4449-8e1a-6e379dca4d2b',
* receiptHandle: 'base64-encoded-something',
* body: 'dasdsa',
* attributes: {},
* messageAttributes: {},
* md5OfBody: '7c1cadb6887373dacb595c47166bfbd9',
* eventSource: 'aws:sqs',
* eventSourceARN: 'arn:aws:sqs:eu-west-1:666123456:todos-events',
* awsRegion: 'eu-west-1'
* ]
* }
*/
module.exports.receiveEvent = handler = function (event, context, callback) {
if (event && event.Records) {
console.log(`Got ${event.Records.length} events`)
event.Records.forEach(event => {
console.log('Got event: ', JSON.stringify(event))
if (event.body == 'reset') {
initDynamoDB(null, _ => {
callback(null, 'Processing complete')
})
} else {
console.error('Unknown event: ' + event.body)
}
})
}
// If you successfully invoke callback, all sent message will be deleted automatically
// WARNING: If you call it BEFORE all async code is done, it will be ignored
callback(null, 'Event processing complete')
}