-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
173 lines (146 loc) · 5.25 KB
/
main.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
171
172
173
import {promises as fs} from 'fs';
import path from 'path';
import http from 'http';
import https from 'https';
import dotenv from 'dotenv';
import bodyParser from 'body-parser';
import express from 'express';
import OpenApiValidator from 'express-openapi-validator';
import lowdb from 'lowdb';
import FileSync from 'lowdb/adapters/FileSync.js';
import {v4 as uuidv4} from 'uuid';
import webPush from 'web-push';
if(process.env.NODE_ENV !== 'production'){
dotenv.config();
}
const route = process.env.ROUTE_ROOT;
const port = process.env.PORT || 80;
const sslPort = process.env.sslPort || 443;
let sslOptions;
if(process.env.USE_SSL){
sslOptions = await (async () => {
return {
key: await fs.readFile(process.env.SSL_PRIV_KEY_PATH).catch((e) => {console.error("An Error occured while reading SSL Key:\n" + e + '\n' + "Exiting..."); process.exit(-1)}),
cert: await fs.readFile(process.env.SSL_CERT_PATH).catch((e) => {console.error("An Error occured while reading SSL Certificate:\n" + e + '\n' + "Exiting..."); process.exit(-1)}),
}
})()}
const app = express();
const __dirname = path.dirname(import.meta.url);
const spec = path.join(__dirname, process.env.OPENAPI_SPEC_PATH);
const adapter = new FileSync('./db/db.json');
const db = lowdb(adapter);
if (!process.env.VAPID_PUBLIC_KEY || !process.env.VAPID_PRIVATE_KEY) {
console.log("You must set the VAPID_PUBLIC_KEY and VAPID_PRIVATE_KEY "+
"environment variables. You can use the following ones:");
console.log(webPush.generateVAPIDKeys());
process.exit(-1);
}
webPush.setVapidDetails(
process.env.VAPID_URL,
process.env.VAPID_PUBLIC_KEY,
process.env.VAPID_PRIVATE_KEY
);
db.defaults({subscriptions: []}).write();
app.use(bodyParser.json());
app.use(bodyParser.text());
app.use(bodyParser.urlencoded({extended: false}));
app.use('/', express.static('./static'));
app.use('/spec', express.static(process.env.OPENAPI_SPEC_PATH));
app.use('*', (req, res, next) =>{
next();
})
app.use(OpenApiValidator.middleware({
apiSpec: process.env.OPENAPI_SPEC_PATH,
})
);
app.get('/vapidPublicKey', function(req, res) {
res.send(process.env.VAPID_PUBLIC_KEY);
});
app.post('/test', (req, res, next) =>{
console.log("Called '" + req.originalUrl);
res.status(200);
res.send("200 OK");
})
app.post('/subscription', (req, res, next) => {
console.log("Called '" + req.originalUrl);
db.get('subscriptions').push(
{
id : uuidv4(),
product : req.body.product,
timestamp : req.body.timestamp,
subscription : req.body.subscription,
}).write();
let subscriptions = db.get('subscriptions').value();
subscriptions = subscriptions.sort((a, b) => {
return a.timestamp - b.timestamp;
})
let userSubscriptions = [];
db.get('subscriptions').value().forEach(subscription => {
//check whether the subscription object found in the request matches any subscription property of subscriptions in the database
if(subscription.subscription.endpoint === req.body.subscription.endpoint){
userSubscriptions.push(subscription);
}
})
res.status(200);
res.send({
subscriptions : userSubscriptions
})
})
app.get('/subscription', (req, res, next) => {
console.log("Called '" + req.originalUrl);
let userSubscriptions = [];
db.get('subscriptions').value().forEach(subscription => {
//check whether the subscription object found in the request matches any subscription property of subscriptions in the database
if(subscription.subscription.endpoint === req.body.subscription.endpoint){
userSubscriptions.push(subscription);
}
})
res.status(200);
res.send({
subscriptions : userSubscriptions
})
})
app.delete('/subscription/:id', (req, res, next) => {
console.log("Called '" + req.originalUrl);
db.get('subscriptions').remove(subscription => {
return subscription.id === req.body.id;
});
db.write();
let userSubscriptions = [];
db.get('subscriptions').forEach(subscription => {
//check whether the subscription object found in the request matches any subscription property of subscriptions in the database
if(subscription.subscription === req.body.subscription){
userSubscriptions.push(subscription);
}
})
res.status(200);
res.send({
subscriptions : userSubscriptions
})
})
app.use((err, req, res, next) => {
// format error
res.status(err.status || 500).json({
message: err.message,
errors: err.errors,
});
});
function checkForDueSubcriptions(subscriptions){
subscriptions.forEach((subscription => {
let subscriptionId = subscription.id;
let now = new Date();
if(subscription.timestamp < now.getTime()){
webPush.sendNotification(subscription.subscription, null);
db.get('subscriptions').remove({
id: subscriptionId
}).write();
}
}));
}
setInterval(checkForDueSubcriptions, 5000, db.get('subscriptions').value());
http.createServer(app).listen(port);
if(process.env.USE_SSL) {
https.createServer(sslOptions, app).listen(sslPort, () => {
console.log("Server listening on port " + sslPort)
});
}