-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
111 lines (90 loc) · 2.99 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
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
var express = require('express');
var session = require('express-session');
var fs = require('fs');
var bodyParser = require('body-parser');
var https = require('https');
var exphbs = require('express-handlebars');
var { createPaymentRequest } = require("./requests/createPaymentRequest");
var { getBalance } = require("./requests/getBalance");
var { getCoins } = require("./requests/getCoins");
var { getPaymentStatus } = require("./requests/getPaymentStatus");
var { getTransactions } = require("./requests/getTransactions");
var { payment } = require("./requests/payment");
var { redeem } = require("./requests/redeem");
var { register } = require("./requests/register");
var { setConfig } = require("./requests/setConfig");
var db = require('./db');
var {
dbConnection
} = require("./config.json");
var {
authMiddleware,
corsMiddleware,
} = require('./middlewares');
var { panelRoute } = require('./routes');
var app = express();
Date.prototype.addSeconds = function (s) {
this.setSeconds(this.getSeconds() + parseInt(s));
return this;
}
// Prepare templating for Control Panel
app.engine('handlebars', exphbs({
defaultLayout: 'main',
// Specify helpers which are only registered on this instance.
helpers: {
json: function (context) { return JSON.stringify(context); },
}
}));
app.set('view engine', 'handlebars');
app.use(express.static(__dirname + '/assets'));
// Middelwares and config for REST API
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(corsMiddleware);
app.use(authMiddleware);
app.use(session({
secret: 'btc-express',
resave: true,
saveUninitialized: true
}));
// Connect to Mongo on start
db.connect(dbConnection, function (err) {
if (err) {
console.log('Unable to connect to MongoDB.')
process.exit(1)
return;
}
app.get('/', (req, res) => {
res.render('index');
});
app.all('/panel/*', panelRoute);
app.post('/createPaymentRequest', createPaymentRequest);
app.get('/getBalance', getBalance);
app.post('/getCoins', getCoins);
app.get('/getPaymentStatus', getPaymentStatus);
app.get('/getTransactions', getTransactions);
app.post('/payment', payment);
app.post('/redeem', redeem);
app.post('/register', register);
app.post('/setConfig', setConfig);
var privateKey = fs.readFileSync('./sslcert/bitcoinexpress.key', 'utf8');
var certificate = fs.readFileSync('./sslcert/bitcoinexpress.crt', 'utf8');
var httpsServer = https.createServer({
key: privateKey,
cert: certificate,
passphrase: 'bitcoinexpress'
}, app);
httpsServer.listen(8443, function() {
console.log('Listening on port 8443...');
});
setInterval(() => {
var now = new Date().addSeconds(30); // 30 sec
var query = {
expires: { $lt: now.toISOString() },
status: { $in: ["initial", "timeout"] },
};
db.remove("payments", query).then((resp) => {
console.log('SCHEDULER - Removing expired requests before ' + now.toISOString(), resp);
});
}, 5 * 60 * 1000); // interval of 5 min
})