This repository has been archived by the owner on Dec 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsnapnotify-server.js
170 lines (154 loc) · 6.04 KB
/
snapnotify-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
//@author Sagar Karandikar
//@web http://sagark.org/snapnotify/
//@about Node.js server for SnapNotify
var http = require('http');
var gcm = require('node-gcm');
var fs = require('fs');
//load settings from file, store it to a settings object
eval(fs.readFileSync('snapserver.settings', encoding="ascii"));
//common vars
var registrationIds = [];
var url = settings.url; //find a way to get this for heroku?
var storedreg = "";
//liveness checker for heroku (prevent idle)
function liveness(){
//here, we want to post to ourselves to prevent heroku idle
var opts = {
host: url,
port: 80,
path: '/liveness',
method: 'POST'
};
var req = http.request(opts, function(res) {
//do nothing
});
req.on('error', function(e) {
console.log('problem with request: ' + e.message);
});
req.end();
}
if (settings.heroku){
console.log('running in heroku mode');
var port = process.env.PORT;
console.log("started liveness checker for heroku");
setInterval(liveness, 60*20*1000); //keep server alive on heroku
} else {
console.log('running in "Own Server" mode');
var port = 1337;
console.log('not starting liveness checker');
}
//more common vars
var urlport = url + ":" + port;
//load from file and populate registrationIds
/* NOTE: if you want to use this with heroku, you'll need to git add
the registration_store file (populated with your device ids), since heroku
operates with a read-only filesystem */
fs.readFile('registrations_store_file', 'ascii', function(err, data){
if(err) {
console.log("no registration file found");
console.log("if you're running on heroku, see the note about loading from file in snapnotify-server.js");
} else {
storedreg = data;
storedreg = storedreg.split(",");
console.log("loaded registrations from file:");
for (x = 0; x < storedreg.length; x++){
registrationIds.push(storedreg[x]);
console.log(storedreg[x]);
}
}
});
http.createServer(function (req, res) {
switch(req.url) {
case '/':
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Welcome to snapnotify-server! Your server is now running at: ' + urlport + '\n');
break;
case '/register':
if (req.method == 'POST'){
recstr = "";
req.on('data', function(chunk) {
recstr += chunk.toString();
});
req.on('end', function() {
var newToken = JSON.parse(recstr).token
console.log(newToken);
if (registrationIds.indexOf(newToken) == -1) {
registrationIds.push(newToken);
console.log("registered");
} else {
console.log("registration exists");
}
console.log("writing registration ids to file");
var stream = fs.createWriteStream("registrations_store_file");
stream.once('open', function(fd) {
for (x = 0; x<(registrationIds.length-1); x++){
stream.write(registrationIds[x] + ",");
}
stream.write(registrationIds[registrationIds.length-1]);
});
console.log("writing complete");
res.writeHead(200, "OK", {'Content-Type': 'application/json'});
res.write(JSON.stringify({'status': true}));
res.end();
});
} else {
console.log("REGISTRATION FAILURE");
res.writeHead(400, "KO", {'Content-Type': 'text/html'});
res.end();
}
break;
case '/message':
if (req.method == 'POST') {
console.log('posted');
recstr = "";
req.on('data', function(chunk) {
recstr += chunk.toString();
});
req.on('end', function() {
var jsonObject = JSON.parse(recstr);
res.writeHead(200, "OK", {'Content-Type': 'text/html'});
res.end();
var sender = new gcm.Sender(settings.apikey);
var message = new gcm.Message({
collapseKey: 'demo',
priority: 'high',
contentAvailable: true,
delayWhileIdle: true,
timeToLive: 3,
data: {
action: jsonObject.action,
ref: jsonObject.ref,
before: jsonObject.before,
after: jsonObject.after,
commits: jsonObject.commits
}
});
console.log(message);
sender.send(message, registrationIds, function (err, response) {
if(err) {
console.error(err);
} else {
console.log(response);
}
});
});
} else {
console.log("Uh oh, you should have used a POST.");
}
break;
case '/setup':
res.writeHead(200, "OK", {'Content-Type': 'text/html'});
res.end("Currently just filler, will eventually show a qrcode for easy config.");
break;
case '/liveness':
if (req.method == 'POST') {
console.log('server alive');
req.on('end', function() {
res.writeHead(200, "OK", {'Content-Type': 'text/html'});
res.end();
});
}
break;
};
}).listen(port, '0.0.0.0');
console.log('Server running at: ' + urlport);