-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
150 lines (123 loc) · 3.13 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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const fs = require("fs");
const express = require("express");
const fetch = require("node-fetch");
const { NginxConfFile } = require("nginx-conf");
const { exec } = require("child_process");
const {
configPath,
dirPath,
tildaAPI,
serverPort,
timeout,
} = require("./config");
const server = express();
function queueMachine() {
const queue = [];
let isHold = false;
function next() {
return queue.shift();
}
function add(item) {
console.log(`Page ${item} was added to queue!`);
return queue.push(item);
}
async function start() {
setInterval(async () => {
const item = queue[0];
if (!item || isHold) return;
else {
isHold = true;
if (await savePage(item)) {
next();
isHold = false;
console.log(`Page ${item} was removed from queue!`);
} else {
setTimeout(() => (isHold = false), timeout);
}
}
}, 1000);
}
start();
return { add };
}
async function NginxConf() {
return new Promise((resolve, reject) => {
NginxConfFile.create(configPath, function (err, conf) {
if (err || !conf) {
reject();
}
resolve(conf);
});
});
}
function restartNGINX() {
try {
const restart = exec("systemctl restart nginx");
restart.stderr.on("data", function (data) {
console.log("restart stderr: " + data);
});
restart.stdout.on("data", function (data) {
console.log("restart stdout: " + data);
});
restart.on("close", function (code) {
console.log("Restart NGINX with the code " + code);
});
} catch (e) {
console.log(e);
}
}
function checkPage(config, pageId) {
for (const location of config.nginx.location || []) {
for (const rewrite of location.rewrite || []) {
if (rewrite._value.includes(pageId)) return true;
}
}
return false;
}
async function savePage(pageId) {
if (!pageId) return;
console.log(`Saving page ${pageId}...`);
const { status, message, result } = await fetch(
tildaAPI.getPageFull + "&pageid=" + pageId
)
.then((response) => response.json())
.catch((error) => {
console.log(error);
return {
message: "Server drop request.",
};
});
if (status !== "FOUND") {
console.log(`Page ${pageId} wasn't loaded! `, message);
return false;
}
const { alias, html } = result || {};
const pagePath = pageId === "5234768" ? "index" : alias;
if (!pagePath) {
console.log(`Page ${pageId} have no alias!`);
return false;
}
if (!fs.existsSync(dirPath)) {
fs.mkdirSync(dirPath + "/" + pagePath, { recursive: true }, (err) => {
if (err) throw err;
});
}
const path = `${dirPath}/${pagePath}.html`;
fs.writeFileSync(path, html);
console.log(`Page ${pageId} was saved!`);
return true;
}
const queue = queueMachine();
server.get("/webhook", async (req, res) => {
const { pageid } = req.query;
if (!pageid) {
res.sendStatus(400);
return;
}
console.log("New change on Tilda. Page", pageid);
res.sendStatus(200);
queue.add(pageid);
});
server.listen(serverPort, () => {
console.log(`Server listening on port ${serverPort}`);
});