-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
133 lines (115 loc) · 4.23 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
(async () => {
const http = require('http');
const url = require('url');
const querystring = require('querystring');
const fsPromise = require('fs').promises;
const __ = require('./utils');
/* config */
const PORT = +process.env.PORT || 3020;
const COUNTER = +process.env.COUNTER || 0;
const DEBUG = process.env.DEBUG || false;
var track = '';
if (process.env.TRACKER_CONFIG_FILE)
var [err, track] = await __.to(fsPromise.readFile(process.env.TRACKER_CONFIG_FILE));
if (!track && process.env.TRACKER_SECRET_FILE)
var [err, track] = await __.to(fsPromise.readFile('/run/secrets/' + process.env.TRACKER_SECRET_FILE));
var embed = '';
if (process.env.EMBED_FILE) var [err, embed] = await __.to(fsPromise.readFile(process.env.EMBED_FILE));
const TRACKER = track || '';
const EMBED = embed || '';
console.log('APP_CONFIG', {
PORT,
COUNTER,
DEBUG,
TRACKER: !!TRACKER,
EMBED: !!EMBED,
});
/* prepare content */
let tsStart = new Date().getTime();
//css
let css = await fsPromise.readFile('./public/css/style.css');
css = __.minimizeCSS(css.toString());
//images
let favicon = await fsPromise.readFile('./public/favicon.ico');
let telegram_png = await fsPromise.readFile('./public/img/telegram-512.png');
//html
let htmlPages = {
indexPage: await fsPromise.readFile('./html/index.html'),
redirectPage: await fsPromise.readFile('./html/redirect.html'),
};
let globalVars = {
COUNTER,
TRACKER,
EMBED,
CSS: css,
};
//translation
let translatedHTML = {
indexPage: {},
redirectPage: {},
};
const LANG_DICT = require('./data/language.js');
Object.keys(LANG_DICT).forEach(lang => {
Object.keys(translatedHTML).forEach(page => {
translatedHTML[page][lang] = __.passVariables(
htmlPages[page].toString(),
Object.assign(LANG_DICT[lang], globalVars, { language: lang }),
);
//translatedHTML[page][lang] = __.minimizeHTML(translatedHTML[page][lang]);
});
});
//utility
let robots = await fsPromise.readFile('./public/robots.txt');
let ads = await fsPromise.readFile('./public/ads.txt');
let sitemap_ru = await fsPromise.readFile('./public/sitemap_ru.xml');
let sitemap_en = await fsPromise.readFile('./public/sitemap_en.xml');
//garbage
delete htmlPages;
delete globalVars;
delete css;
delete fsPromise;
delete LANG_DICT;
if (DEBUG) console.log('content prepeared', '+' + ((new Date().getTime() - tsStart) / 1000).toFixed(2) + ' sec');
/* serve requests */
const server = http.createServer(async (request, response) => {
let { pathname, query } = url.parse(request.url);
query = querystring.parse(query);
let queryLang = query.lang && (query.lang == 'ru' || query.lang == 'en') ? query.lang : false;
let language = queryLang ? queryLang : __.detectLanguage(request);
if (DEBUG) console.log('req', language, request.headers.host, pathname);
response.setHeader('Access-Control-Allow-Origin', '*');
response.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept');
response.setHeader('Content-Language', language);
response.setHeader('status', 200);
if (pathname == '/') {
response.setHeader('Content-Type', 'text/html; charset=utf-8');
response.end(translatedHTML.indexPage[language]);
} else if (pathname == '/favicon.ico') {
response.setHeader('Content-Type', 'image/x-icon');
response.end(favicon);
} else if (pathname == '/img/telegram.png') {
response.setHeader('Content-Type', 'image/png');
response.end(telegram_png);
} else if (pathname == '/robots.txt') {
response.setHeader('Content-Type', 'text/plain');
response.end(robots);
} else if (pathname == '/sitemap_ru.xml') {
response.setHeader('Content-Type', 'application/xml');
response.end(sitemap_ru);
} else if (pathname == '/sitemap_en.xml') {
response.setHeader('Content-Type', 'application/xml');
response.end(sitemap_en);
} else if (pathname == '/ads.txt') {
response.setHeader('Content-Type', 'aptext/plainml');
response.end(ads);
} else {
response.setHeader('Content-Type', 'text/html; charset=utf-8');
response.end(translatedHTML.redirectPage[language]);
}
});
/* start server */
server.listen(PORT, err => {
if (err) return console.log('something bad happened', err.message);
console.log(`server is listening on ${PORT}`);
});
})();