-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
80 lines (67 loc) · 2 KB
/
index.js
File metadata and controls
80 lines (67 loc) · 2 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
const fs = require("mz/fs");
const path = require("path");
const http = require("http");
const url = require("url");
const { Readable } = require("stream");
// Path to the directory containing the .txt files
const directory = "frames";
let frames = [];
// Utiliser async/await pour charger les frames de manière synchrone
async function loadFrames() {
try {
const files = await fs.readdir(directory);
// Trier les noms de fichiers par ordre alphabétique
files.sort((a, b) => {
const numA = parseInt(a.split(".")[0]); // Extraire le numéro du nom de fichier
const numB = parseInt(b.split(".")[0]);
return numA - numB;
});
for (const file of files) {
if (file.endsWith(".txt")) {
const filePath = path.join(directory, file);
const content = await fs.readFile(filePath, "utf8");
frames.push(content);
}
}
} catch (err) {
console.error("Error loading frames:", err);
}
}
// Appeler la fonction pour charger les frames
loadFrames();
const streamer = (stream) => {
let index = 0;
return setInterval(() => {
// Effacer l'écran
stream.push("\033[2J\033[3J\033[H");
stream.push(frames[index]);
index = (index + 1) % frames.length;
}, 90);
};
const server = http.createServer((req, res) => {
if (req.url === "/healthcheck") {
res.writeHead(200, { "Content-Type": "application/json" });
return res.end(JSON.stringify({ status: "ok" }));
}
if (
req.headers &&
req.headers["user-agent"] &&
!req.headers["user-agent"].includes("curl")
) {
res.writeHead(302, { Location: "https://github.com/Linux0Hat/pedro.live" });
return res.end();
}
const stream = new Readable();
stream._read = function noop() {};
stream.pipe(res);
const interval = streamer(stream);
req.on("close", () => {
stream.destroy();
clearInterval(interval);
});
});
const port = process.env.PARROT_PORT || 3000;
server.listen(port, (err) => {
if (err) throw err;
console.log(`Listening on localhost:${port}`);
});