forked from journey-ad/Moe-Counter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
161 lines (131 loc) · 3.86 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
151
152
153
154
155
156
157
158
159
160
161
"use strict";
const fs = require("fs");
const config = require("config-yml");
const express = require("express");
const compression = require("compression");
const cron = require("node-cron");
const db = require("./db");
const themify = require("./utils/themify");
const axios = require("axios");
const PLACES = 7;
const urlToPing = "http://moe-counter-krfg.onrender.com/get/@Server";
const app = express();
const mysql = require("mysql2");
require("dotenv").config();
const distalk = process.env.DISTALK_DB_URL;
app.use(express.static("assets"));
app.use(compression());
app.set("view engine", "pug");
app.get("/", (req, res) => {
const site = config.app.site || `${req.protocol}://${req.get("host")}`;
res.render("index", { site });
});
// get the image
app.get("/get/@:name", async (req, res) => {
const { name } = req.params;
const { theme = "moebooru" } = req.query;
let length = PLACES;
// This helps with GitHub's image cache
res.set({
"content-type": "image/svg+xml",
"cache-control": "max-age=0, no-cache, no-store, must-revalidate",
});
const data = await getCountByName(name);
if (name === "demo") {
res.set({
"cache-control": "max-age=31536000",
});
length = 10;
}
// Send the generated SVG as the result
const renderSvg = themify.getCountImage({ count: data.num, theme, length });
res.send(renderSvg);
console.log(
data,
`theme: ${theme}`,
`ref: ${req.get("Referrer") || null}`,
`ua: ${req.get("User-Agent") || null}`
);
});
// JSON record
app.get("/record/@:name", async (req, res) => {
const { name } = req.params;
const data = await getCountByName(name);
res.json(data);
});
app.get("/heart-beat", (req, res) => {
res.set({
"cache-control": "max-age=0, no-cache, no-store, must-revalidate",
});
res.send("alive");
console.log("heart-beat");
});
const listener = app.listen(config.app.port || 3000, () => {
console.log("Your app is listening on port " + listener.address().port);
});
let __cache_counter = {},
shouldPush = false;
setInterval(() => {
shouldPush = true;
}, 1000 * 60);
async function pushDB() {
if (!shouldPush) return;
try {
shouldPush = false;
if (Object.keys(__cache_counter).length === 0) return;
console.log("pushDB", __cache_counter);
const counters = Object.keys(__cache_counter).map((key) => {
return {
name: key,
num: __cache_counter[key],
};
});
await db.setNumMulti(counters);
__cache_counter = {};
} catch (error) {
console.log("pushDB is error: ", error);
}
}
async function getCountByName(name) {
const defaultCount = { name, num: 0 };
if (name === "demo") return { name, num: "0123456789" };
try {
if (!(name in __cache_counter)) {
const counter = (await db.getNum(name)) || defaultCount;
__cache_counter[name] = counter.num + 1;
} else {
__cache_counter[name]++;
}
pushDB();
return { name, num: __cache_counter[name] };
} catch (error) {
console.log("get count by name is error: ", error);
return defaultCount;
}
}
cron.schedule("*/14 * * * *", async () => {
try {
const response = await axios.get(urlToPing);
console.log(`Pinged ${urlToPing} - Status Code: ${response.status}`);
} catch (error) {
console.error(`Error pinging ${urlToPing}: ${error.message}`);
}
});
async function executeMySQLQuery() {
try {
const connection = await mysql.createConnection(distalk);
const [rows] = await connection.promise().query("SHOW TABLES");
const tableNames = Object.values(rows[0]);
console.log("MySQL Query Result - Tables:", tableNames);
await connection.end();
} catch (error) {
console.error("Error executing MySQL query:", error.message);
}
}
cron.schedule("0 0 */10 * *", async () => {
try {
await executeMySQLQuery();
} catch (error) {
console.error("Error in cron job:", error.message);
}
});