This repository was archived by the owner on Apr 27, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathserver.mjs
More file actions
164 lines (135 loc) · 4.54 KB
/
server.mjs
File metadata and controls
164 lines (135 loc) · 4.54 KB
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
import express from 'express';
import consolidate from 'consolidate';
import https from 'https';
import i18next from 'i18next';
import Backend from 'i18next-fs-backend';
import path from 'path';
const app = express();
app.set('trust proxy', true);
// assign the mustache engine to .html files
app.engine('html', consolidate.mustache);
// set .html as the default extension
app.set('view engine', 'html');
app.set('views', './views');
app.use(express.static('views/public'))
function getIPInfo(callback, ip) {
const options = {
hostname: 'ipinfo.io',
port: 443,
path: '/' + ip + '/?token=' + process.env.GETINFO_TOKEN,
method: 'GET',
timeout: 3000 // Timeout in milliseconds (3 seconds)
};
const req = https.request(options, (resp) => {
let data = '';
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
callback(data);
});
});
req.on('timeout', () => {
console.error('Request timed out');
req.abort(); // Aborts the request
});
req.on('error', (err) => {
console.error("Error: " + err.message);
});
req.end();
}
function initI18n(lang) {
i18next
.use(Backend)
.init({
lng: lang,
ignoreRoutes: ['images/', 'public/', 'css/', 'js/'],
backend: {
loadPath: path.join(process.cwd(), 'locales', '{{lng}}', '{{ns}}.json'),
},
fallbackLng: 'en',
preload: ['en', 'pt'],
lowerCaseLng: true,
debug: false
});
}
function generateHomeViewData(lang, ip, info) {
return {
'title' : i18next.t('title'),
'ip': ip,
'ipinfo' : info,
'ipinfo_domain' : ip.includes(':') ? 'ipinfo.io': 'v6.ipinfo.io',
'home_description': i18next.t('home-description'),
'html_lang': lang == 'en' ? 'en' : 'pt_BR',
'your_current_ip': i18next.t('your-current-ip'),
'city': i18next.t('city'),
'region': i18next.t('region'),
'country': i18next.t('country'),
'provider': i18next.t('provider'),
'cookie_consent': i18next.t('cookie-consent'),
'see_more': i18next.t('see-more')
};
}
function generatePPViewData(lang) {
return {
'title' : i18next.t('title-privacy'),
'html_lang': lang == 'en' ? 'en' : 'pt_BR',
'cookie_consent': i18next.t('cookie-consent'),
'see_more': i18next.t('see-more'),
'pp_info': i18next.t('pp-info'),
'pp_info_1': i18next.t('pp-info-1'),
'pp_info_1_1': i18next.t('pp-info-1-1'),
'pp_info_1_2': i18next.t('pp-info-1-2'),
'pp_info_1_3': i18next.t('pp-info-1-3'),
'pp_use': i18next.t('pp-use'),
'pp_use_1': i18next.t('pp-use-1'),
'pp_use_1_1': i18next.t('pp-use-1-1'),
'pp_use_1_2': i18next.t('pp-use-1-2'),
'pp_use_1_3': i18next.t('pp-use-1-3'),
'pp_share': i18next.t('pp-share'),
'pp_share_description': i18next.t('pp-share-description'),
'pp_cookies': i18next.t('pp-cookies'),
'pp_cookies_description': i18next.t('pp-cookies-description'),
'pp_security': i18next.t('pp-security'),
'pp_security_description': i18next.t('pp-security-description'),
'pp_updates': i18next.t('pp-updates'),
'pp_updates_description': i18next.t('pp-updates-description'),
'pp_contact': i18next.t('pp-contact'),
'pp_contact_description': i18next.t('pp-contact-description')
};
}
app.get('/', async (req, res) => {
initI18n('pt');
const ip = req.ip;
getIPInfo((info) => {
info = JSON.parse(info);
var viewdata = generateHomeViewData('pt', ip, info);
res.render('index', viewdata);
}, ip)
});
app.get('/en', async (req, res) => {
initI18n('en');
const ip = req.ip;
getIPInfo((info) => {
info = JSON.parse(info);
var viewdata = generateHomeViewData('pt', ip, info);
res.render('index', viewdata);
}, ip)
});
app.get('/politica-de-privacidade', async (req, res) => {
initI18n('pt');
setTimeout(() => {
var viewdata = generatePPViewData('pt');
res.render('privacy-policy', viewdata);
}, 300);
});
app.get('/en/privacy-policy', async (req, res) => {
initI18n('en');
setTimeout(() => {
var viewdata = generatePPViewData('pt');
res.render('privacy-policy', viewdata);
}, 300);
});
app.listen(3001, () => console.log(`Server is listening on port 3001`));