-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
122 lines (91 loc) · 3.25 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
112
113
114
115
116
117
118
119
120
121
122
const express = require('express');
const https = require('https')
const fs = require('fs');
const geoip = require('geoip-lite');
//nohup node index.js >> app.log 2>&1 &
//init app
var app = express();
var server = require('http').createServer(app);
function wwwRedirect(req, res, next) {
if (req.headers.host.slice(0, 4) === 'www.') {
var newHost = req.headers.host.slice(4);
return res.redirect(301, req.protocol + '://' + newHost + req.originalUrl);
}
next();
};
app.set('trust proxy', true);
app.use(wwwRedirect);
app.use((req, res, next) => {
var ip = req.ip.replace('::ffff:', '');
var geo = geoip.lookup(ip);
var now = new Date().toString();
var log = `${now}: IP is ${ip} -- Country is ${geo.country} | ${req.method} ${req.url} -- Ref: ${req.headers.referer}`;
console.log(log);
fs.appendFile('server.log', log + '\n', (err) => {
if (err) {
console.log('Unable to append to server log');
}
});
next();
});
// app.use((req, res, next) => {
// res.render('maintenance.hbs');
// });
app.use(express.static(__dirname+'/build'));
app.use(express.static(__dirname+'/.well-known/acme-challenge'));
app.use(express.static(__dirname, { dotfiles: 'allow' } ));
app.use (function (req, res, next) {
if (req.secure) {
// request was via https, so do no special handling
next();
} else {
// request was via http, so redirect to https
res.redirect('https://' + req.headers.host + req.url);
}
});
app.get('*', (req, res) => {
res.sendFile('200.html', { root: __dirname+'/build/' });
});
app.listen(80, () => {
console.log('Listening on port 80.');
});
var server = https.createServer({
key: fs.readFileSync('/etc/letsencrypt/live/forkoasisdex.com/privkey.pem'),
cert: fs.readFileSync('/etc/letsencrypt/live/forkoasisdex.com/cert.pem'),
ca: fs.readFileSync('/etc/letsencrypt/live/forkoasisdex.com/chain.pem')
}, app).listen(443, () => {
console.log('Listening on port 443.');
})
//
// var lastBlock;
// function logEvery2Seconds(i) {
// setTimeout(() => {
// Block.find().limit(1).sort({timestamp: -1}).then((block) => {
// if (lastBlock == null) {
// io.sockets.emit('newBlock', block);
// console.log('pushed new block.');
// } else if (block[0].blockNumber != lastBlock) {
// io.sockets.emit('newBlock', block);
// console.log('pushed new block.');
// }
// lastBlock = block[0].blockNumber;
// }, (e) => {
// console.log('Error retrieving block.');
// });
//
// logEvery2Seconds(++i);
// }, 2000)
// }
// logEvery2Seconds(0);
// let i = 0;
// setInterval(() => {
// console.log('Infinite Loop Test interval n:', i++);
// }, 2000);
// // ES5 style
// const thorify = require("thorify").thorify;
// const Web3 = require("web3");
//
// const web3 = thorify(new Web3(), "http://localhost:8669");
//
// web3.eth.getBlock("latest").then(res => console.log(res));
// // Best block info will be displayed