-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathapp.js
86 lines (79 loc) · 2.78 KB
/
app.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
var http = require('http');
var url = require("url");
var async = require('async');
var AWS = require('aws-sdk');
var options = require('./config/setting.json');
AWS.config.loadFromPath('./config/aws.json');
var sns = new AWS.SNS();
var httpServer = http.createServer(handler);
var io = require('socket.io').listen(httpServer);
var fs = require('fs');
//asyncを使って順番に処理を実行
async.series([
//HTTPサーバ起動
function (callback) {
httpServer.listen(3000);
callback(null, 1);
},
//subscrive開始
function (callback) {
initSubscriber(callback);
callback(null, 2);
}
], function (err, results) {
if (err) {
throw err;
}
});
function handler(req, res) {
var path = url.parse(req.url).pathname;
if (path === "/httpsns") {
var body = '';
req.on('data', function (data) {
body += data;
});
req.on('end', function () {
res.writeHead(200, {
'Content-Type':'text/html'
});
var obj = JSON.parse(body);
if (obj.Type === "SubscriptionConfirmation") {
sns.confirmSubscription({ TopicArn:obj.TopicArn, Token:obj.Token}, function (err, data) {
console.log("confirmSubscription");
});
} else if (obj.Type === "Notification" && obj.Message !== undefined) {
console.log(obj.Subject + ":" + obj.Message);
io.sockets.emit('msg', obj.Message);
}
res.end('OK');
});
} else if ( path === "/dist/css/timeline.css" ) {
res.writeHead(200, {'Content-Type':'text/css'});
res.end(fs.readFileSync('dist/css/timeline.css'));
} else if ( path === "/dist/css/bootstrap.min.css" ) {
res.writeHead(200, {'Content-Type':'text/css'});
res.end(fs.readFileSync('dist/css/bootstrap.min.css'));
} else if ( path === "/dist/css/bootstrap.min.css.map" ) {
res.writeHead(200, {'Content-Type':'text/css'});
res.end(fs.readFileSync('dist/css/bootstrap.min.css.map'));
} else if ( path === "/dist/css/sb-admin-2.css" ) {
res.writeHead(200, {'Content-Type':'text/css'});
res.end(fs.readFileSync('dist/css/sb-admin-2.css'));
} else if ( path === "/data/worldmap.topojson" ) {
res.writeHead(200, {'Content-Type':'application/json'});
res.end(fs.readFileSync('data/worldmap.topojson'));
} else {
res.writeHead(200, {'Content-Type':'text/html'});
res.end(fs.readFileSync('index.html'));
}
}
function initSubscriber(callback) {
var args = {
TopicArn:options.topic_arn,
Protocol:"http",
Endpoint:options.sns_endpoint
};
sns.subscribe(args, function (err, data) {
console.log("subscribe start.");
});
}