-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
166 lines (141 loc) · 4.26 KB
/
index.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
// Set our environment.
process.env.NODE_ENV = 'staging';
// Intialize our modules.
var application_root = __dirname,
apnagent = require('apnagent'),
redis = require("redis"),
express = require("express"),
join = require('path').join,
redisClient = redis.createClient(),
app = express();
/**
* Use a MockAgent for dev/test envs
*/
app.configure('development', 'test', function () {
var agent = new apnagent.MockAgent();
// no configuration needed
// mount to app
app
.set('apn', agent)
.set('apn-env', 'mock');
});
/**
* Usa a live Agent with sandbox certificates
* for our staging environment.
*/
app.configure('staging', function () {
var agent = new apnagent.Agent();
// configure agent
agent
.set('cert file', join(__dirname, 'certs/apn/apnagent-dev-cert.pem'))
.set('key file', join(__dirname, 'certs/apn/apnagent-dev-key.pem'))
.enable('sandbox');
// mount to app
app
.set('apn', agent)
.set('apn-env', 'live-sandbox');
});
/**
* Use a live Agent with production certificates
* for our production environment.
*/
app.configure('production', function () {
var agent = new apnagent.Agent();
// configure agent
agent
.set('cert file', join(__dirname, 'certs/apn/prod-cert.pem'))
.set('key file', join(__dirname, 'certs/apn/prod-key.pem'));
// mount to app
app
.set('apn', agent)
.set('apn-env', 'live-production');
});
// Log any errors
redisClient.on("error", function (err) {
console.log("Error " + err);
});
// Default configuration for the express server
app.configure(function () {
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(join(application_root, "public")));
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
var agent = app.get('apn')
, env = app.get('apn-env');
// common settings
agent
.set('expires', '1d')
.set('reconnect delay', '1s')
.set('cache ttl', '30m');
// see error mitigation section
agent.on('message:error', function (err, msg) {
// ...
});
// connect needed to start message processing
agent.connect(function (err) {
if (err) throw err;
console.log('[%s] apn agent running', env);
});
});
// POST path for updating device token.
app.post('/api/token', function(req, res) {
res.type('json');
console.log(req.body);
if (req.body.hasOwnProperty('number') && req.body.hasOwnProperty('token')) {
var number = req.body.number.replace("+1", "");
redisClient.set(number, req.body.token, function(err, reply){
if (reply === "OK") {
console.log("Supposedly this thing updated ok..");
res.send(200, { status: 'OK' });
}
else {
res.send(500, { error: 'something blew up' });
}
});
}
else {
res.send(500, { error: 'something blew up' });
}
});
// POST path for updating device token.
app.post('/api/apn', function(req, res) {
var agent = app.get('apn');
if (req.body.hasOwnProperty('AccountSid')) {
var toNumber = req.body.To.replace("+1", "");
var fromNumber = req.body.From.replace("+1", "");
redisClient.get(toNumber, function(err, reply) {
// If we find a valid token create an APN message.
if (reply !== null) {
agent.createMessage()
.device(reply)
.alert(req.body.Body)
.set('from', fromNumber)
.set('sid', req.body.SmsMessageSid)
.badge(1)
.send(function (err) {
// handle apnagent custom errors
if (err && err.toJSON) {
res.json(400, { error: err.toJSON(false) });
}
// handle anything else (not likely)
else if (err) {
res.json(400, { error: err.message });
}
// it was a success
else {
res.json({ success: true });
}
});
}
else {
res.send(500);
}
});
}
else {
res.send(500);
}
});
// Launch server
app.listen(4242);