-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
113 lines (102 loc) · 2.82 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
/**
* @Author: Geoffrey Bauduin <[email protected]>
*/
(function () {
var express = require("express");
var config = require("./config.json");
var bodyParser = require("body-parser");
var ApplePush = require("./apple");
var Logger = require("./logger");
var app = express();
app.use(bodyParser.json());
app.use(function (req, res, next) {
res.setHeader("Access-Control-Allow-Origin", config.allowed_origins);
res.setHeader("Access-Control-Allow-Methods", config.allowed_methods);
res.setHeader("Access-Control-Allow-Headers", config.allowed_headers);
res.setHeader("Content-Type", "application/json");
next();
});
app.use(function (req, res, next) {
if (req.headers && req.headers.apikey && req.headers.apikey == config.api_key) {
next();
}
else {
res.status(403).send({});
}
});
function checkString(d) {
if (d === "" || typeof d != typeof "") {
return false
}
return true;
}
function checkInteger(d) {
if (typeof d != typeof 0) {
return false;
}
return true;
}
/**
* @api {post} /api/push/ios Push notification for iOS
* @apiName iOS
* @apiGroup Push
*
* @apiParam {String} token Push notification token to push to
* @apiParam {String} title Title of the push notification
* @apiParam {String} body Body of the push notification
* @apiParam {String} launch_image Launch image (not sure what it's used for)
* @apiParam {String} action_loc_key Action loc key (not sure what it's used for)
* @apiParam {String} expiration Expiration of the push notification
* @apiParam {String} sound Sound of the push notification
* @apiParam {Integer} badge Badge of the push notification
*/
app.post("/api/push/ios", function (req, res) {
var e = null;
for (var idx in ["app", "token", "title", "body", "launch_image", "action_loc_key", "expiration", "sound"]) {
if (req.body.hasOwnProperty(idx) && checkString(req.body[idx]) == false) {
e = idx;
break;
}
}
if (e == null) {
if (checkInteger(req.body.badge) == false) {
e = "badge";
}
else if (ApplePush.isValidApp(req.body.app) == false) {
e = "app";
}
}
if (e != null) {
res.status(400).send({
error: "BadRequest",
data: e
})
}
else {
ApplePush.send(req.body.app, {
token: req.body.token,
title: req.body.title,
body: req.body.body,
launch_image: req.body.launch_image,
action_loc_key: req.body.action_loc_key,
expiration: req.body.expiration,
sound: req.body.sound,
badge: req.body.badge
}, function (err) {
if (err != null) {
res.status(500).send({
success: false
});
}
else {
res.status(200).send({
success: true
});
}
})
}
});
app.listen(config.port, config.host, function () {
Logger.log("Server is listening on", config.host, "port", config.port);
});
}).call(this);