-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtopology.js
99 lines (86 loc) · 2.49 KB
/
topology.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
'use strict'
var fs = require('fs')
var AWS = require('aws-sdk')
var uuid = require('uuid')
var Router = require('attak-router')
var AWSMqtt = require('aws-mqtt')
var nodePath = require('path')
var queryChats = require('./query_chats')
module.exports = {
name: 'attak-chat',
static: {
dir: './build',
permissions: {
invoke: ['chatHandler', 'recentChats']
},
auth: {
federated: {
google: {
// You should secure this key better in your projects. We're
// including it unsecured to make the example easier to use
key: '376999432431-gqpt0ikktc9iveg0435tnk1ufj0j48st.apps.googleusercontent.com'
}
}
}
},
api: 'endpoint',
provision: require('./provision'),
processors: {
endpoint: new Router({
routes: {
'GET /': 'home',
'GET /api/chats/recent': 'recentChats',
'GET /bundle.js': 'bundle',
}
}),
chatHandler: function(event, context, callback) {
console.log("CHAT HANDLER", context.aws.endpoints.iot, event)
context.emit('messages', event)
var dynamo = new AWS.DynamoDB({
endpoint: context.aws.endpoints.dynamodb,
params: {
TableName: 'attak-chat'
}
})
var itemParams = {
Item: {
id: {S: uuid.v1()},
author: {S: event.author},
channel: {S: 'default'},
message: {S: event.message},
timestamp: {N: new Date().getTime().toString()},
}
}
console.log("PUT ITEM", itemParams)
dynamo.putItem(itemParams, function(err, data) {
console.log("PUT RESULTS", err, data)
callback(err, data)
})
},
chatEmitter: function(event, context, callback) {
console.log("CHAT EMITTER", event)
var iotData = new AWS.IotData({
endpoint: 'a3cp5tc4mlo56h.iot.us-east-1.amazonaws.com'
})
var params = {
topic: '/chat',
payload: new Buffer(JSON.stringify(event.data)),
qos: 0
};
iotData.publish(params, function(err, results) {
console.log("EMITTED", err, results)
callback(err, results)
});
},
recentChats: function(event, context, callback) {
console.log("RECENT CHATS", event)
queryChats(context, 'default', new Date().getTime() - 200000, new Date().getTime(), function(err, results) {
console.log("QUERY RESULTS", err, results)
callback(err, results)
})
},
},
streams: [
['chatHandler', 'chatEmitter']
]
}