2024-10-19 21:11:14 +00:00
|
|
|
"use strict";
|
2020-08-03 12:11:58 +00:00
|
|
|
|
2024-10-28 16:24:12 +00:00
|
|
|
require('dotenv').config();
|
2024-10-19 21:11:14 +00:00
|
|
|
const express = require("express");
|
|
|
|
const compression = require("compression");
|
2024-10-20 00:29:58 +00:00
|
|
|
const { z } = require("zod");
|
2020-08-03 12:11:58 +00:00
|
|
|
|
2024-10-19 21:11:14 +00:00
|
|
|
const db = require("./db");
|
2024-10-20 00:29:58 +00:00
|
|
|
const { themeList, getCountImage } = require("./utils/themify");
|
2024-10-25 05:36:32 +00:00
|
|
|
const { cors, ZodValid } = require("./utils/middleware");
|
2024-10-31 05:58:21 +00:00
|
|
|
const { randomArray, logger } = require("./utils");
|
2020-08-03 12:11:58 +00:00
|
|
|
|
2024-10-19 21:11:14 +00:00
|
|
|
const app = express();
|
2020-08-03 12:11:58 +00:00
|
|
|
|
2024-10-19 21:11:14 +00:00
|
|
|
app.use(express.static("assets"));
|
|
|
|
app.use(compression());
|
2024-10-25 05:36:32 +00:00
|
|
|
app.use(cors());
|
2024-10-19 21:11:14 +00:00
|
|
|
app.set("view engine", "pug");
|
2020-08-03 12:11:58 +00:00
|
|
|
|
|
|
|
app.get('/', (req, res) => {
|
2024-10-28 16:24:12 +00:00
|
|
|
const site = process.env.APP_SITE || `${req.protocol}://${req.get('host')}`
|
2024-10-31 05:58:21 +00:00
|
|
|
const ga_id = process.env.GA_ID || null
|
2024-10-20 00:29:58 +00:00
|
|
|
res.render('index', {
|
|
|
|
site,
|
2024-10-31 05:58:21 +00:00
|
|
|
ga_id,
|
2024-10-20 00:29:58 +00:00
|
|
|
themeList,
|
|
|
|
})
|
2020-08-03 12:11:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
// get the image
|
2024-10-20 00:29:58 +00:00
|
|
|
app.get(["/@:name", "/get/@:name"],
|
|
|
|
ZodValid({
|
|
|
|
params: z.object({
|
|
|
|
name: z.string().max(32),
|
|
|
|
}),
|
|
|
|
query: z.object({
|
|
|
|
theme: z.string().default("moebooru"),
|
2024-10-31 08:53:35 +00:00
|
|
|
padding: z.coerce.number().int().min(0).max(16).default(7),
|
2024-10-20 00:29:58 +00:00
|
|
|
offset: z.coerce.number().min(-500).max(500).default(0),
|
2024-10-30 07:46:17 +00:00
|
|
|
align: z.enum(["top", "center", "bottom"]).default("top"),
|
2024-10-20 00:29:58 +00:00
|
|
|
scale: z.coerce.number().min(0.1).max(2).default(1),
|
|
|
|
pixelated: z.enum(["0", "1"]).default("1"),
|
2024-11-02 13:44:48 +00:00
|
|
|
darkmode: z.enum(["0", "1", "auto"]).default("auto"),
|
|
|
|
|
|
|
|
// Unusual Options
|
|
|
|
num: z.coerce.number().int().min(0).max(1e15).default(0), // a carry-safe integer, less than `2^53-1`, and aesthetically pleasing in decimal.
|
|
|
|
prefix: z.coerce.number().int().min(-1).max(999999).default(-1)
|
2024-10-20 00:29:58 +00:00
|
|
|
})
|
|
|
|
}),
|
|
|
|
async (req, res) => {
|
|
|
|
const { name } = req.params;
|
2024-10-25 04:53:34 +00:00
|
|
|
let { theme = "moebooru", num = 0, ...rest } = req.query;
|
2024-10-20 00:29:58 +00:00
|
|
|
|
|
|
|
// 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",
|
|
|
|
});
|
|
|
|
|
2024-10-31 08:53:35 +00:00
|
|
|
const data = await getCountByName(String(name), Number(num));
|
2024-10-20 00:29:58 +00:00
|
|
|
|
|
|
|
if (name === "demo") {
|
|
|
|
res.set("cache-control", "max-age=31536000");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (theme === "random") {
|
|
|
|
theme = randomArray(Object.keys(themeList));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send the generated SVG as the result
|
|
|
|
const renderSvg = getCountImage({
|
|
|
|
count: data.num,
|
|
|
|
theme,
|
|
|
|
...rest
|
|
|
|
});
|
|
|
|
|
|
|
|
res.send(renderSvg);
|
|
|
|
|
2024-10-31 05:58:21 +00:00
|
|
|
logger.debug(
|
2024-10-20 00:29:58 +00:00
|
|
|
data,
|
2024-10-25 04:53:34 +00:00
|
|
|
{ theme, ...req.query },
|
2024-10-20 00:29:58 +00:00
|
|
|
`ip: ${req.headers['x-forwarded-for'] || req.connection.remoteAddress}`,
|
|
|
|
`ref: ${req.get("Referrer") || null}`,
|
|
|
|
`ua: ${req.get("User-Agent") || null}`
|
|
|
|
);
|
2024-10-19 21:11:14 +00:00
|
|
|
}
|
2024-10-20 00:29:58 +00:00
|
|
|
);
|
2020-08-22 13:20:20 +00:00
|
|
|
|
|
|
|
// JSON record
|
2024-10-19 21:11:14 +00:00
|
|
|
app.get("/record/@:name", async (req, res) => {
|
|
|
|
const { name } = req.params;
|
2020-08-22 13:20:20 +00:00
|
|
|
|
2024-10-19 21:11:14 +00:00
|
|
|
const data = await getCountByName(name);
|
2020-08-03 12:11:58 +00:00
|
|
|
|
2024-10-19 21:11:14 +00:00
|
|
|
res.json(data);
|
|
|
|
});
|
2020-08-03 12:11:58 +00:00
|
|
|
|
2024-10-19 21:11:14 +00:00
|
|
|
app.get("/heart-beat", (req, res) => {
|
|
|
|
res.set("cache-control", "max-age=0, no-cache, no-store, must-revalidate");
|
|
|
|
res.send("alive");
|
2024-10-31 05:58:21 +00:00
|
|
|
logger.debug("heart-beat");
|
2020-08-03 12:11:58 +00:00
|
|
|
});
|
|
|
|
|
2024-10-28 16:24:12 +00:00
|
|
|
const listener = app.listen(process.env.APP_PORT || 3000, () => {
|
2024-10-31 05:58:21 +00:00
|
|
|
logger.info("Your app is listening on port " + listener.address().port);
|
2024-10-19 21:11:14 +00:00
|
|
|
});
|
2020-08-22 13:20:20 +00:00
|
|
|
|
2024-10-19 21:11:14 +00:00
|
|
|
let __cache_counter = {};
|
2024-10-28 16:24:12 +00:00
|
|
|
let enablePushDelay = process.env.DB_INTERVAL > 0
|
2024-10-19 21:11:14 +00:00
|
|
|
let needPush = false;
|
2023-03-06 10:57:41 +00:00
|
|
|
|
2024-10-19 21:11:14 +00:00
|
|
|
if (enablePushDelay) {
|
|
|
|
setInterval(() => {
|
|
|
|
needPush = true;
|
2024-10-28 16:24:12 +00:00
|
|
|
}, 1000 * process.env.DB_INTERVAL);
|
2024-10-19 21:11:14 +00:00
|
|
|
}
|
2023-03-06 10:57:41 +00:00
|
|
|
|
|
|
|
async function pushDB() {
|
2024-10-19 21:11:14 +00:00
|
|
|
if (Object.keys(__cache_counter).length === 0) return;
|
|
|
|
if (enablePushDelay && !needPush) return;
|
2023-03-06 10:57:41 +00:00
|
|
|
|
|
|
|
try {
|
2024-10-19 21:11:14 +00:00
|
|
|
needPush = false;
|
2024-10-31 05:58:21 +00:00
|
|
|
logger.info("pushDB", __cache_counter);
|
2023-03-06 10:57:41 +00:00
|
|
|
|
2024-10-19 21:11:14 +00:00
|
|
|
const counters = Object.keys(__cache_counter).map((key) => {
|
2023-03-06 10:57:41 +00:00
|
|
|
return {
|
|
|
|
name: key,
|
2024-10-19 21:11:14 +00:00
|
|
|
num: __cache_counter[key],
|
|
|
|
};
|
|
|
|
});
|
2023-03-06 10:57:41 +00:00
|
|
|
|
2024-10-19 21:11:14 +00:00
|
|
|
await db.setNumMulti(counters);
|
|
|
|
__cache_counter = {};
|
2023-03-06 10:57:41 +00:00
|
|
|
} catch (error) {
|
2024-10-31 05:58:21 +00:00
|
|
|
logger.error("pushDB is error: ", error);
|
2023-03-06 10:57:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-10-25 04:53:34 +00:00
|
|
|
async function getCountByName(name, num) {
|
2024-10-19 21:11:14 +00:00
|
|
|
const defaultCount = { name, num: 0 };
|
2020-08-22 13:20:20 +00:00
|
|
|
|
2024-10-19 21:11:14 +00:00
|
|
|
if (name === "demo") return { name, num: "0123456789" };
|
2020-08-22 13:20:20 +00:00
|
|
|
|
2024-10-25 04:53:34 +00:00
|
|
|
if (num > 0) { return { name, num } };
|
|
|
|
|
2020-08-22 13:20:20 +00:00
|
|
|
try {
|
2023-03-06 10:57:41 +00:00
|
|
|
if (!(name in __cache_counter)) {
|
2024-10-19 21:11:14 +00:00
|
|
|
const counter = (await db.getNum(name)) || defaultCount;
|
|
|
|
__cache_counter[name] = counter.num + 1;
|
2023-03-06 10:57:41 +00:00
|
|
|
} else {
|
2024-10-19 21:11:14 +00:00
|
|
|
__cache_counter[name]++;
|
2023-03-06 10:57:41 +00:00
|
|
|
}
|
|
|
|
|
2024-10-19 21:11:14 +00:00
|
|
|
pushDB();
|
2020-08-22 13:20:20 +00:00
|
|
|
|
2024-10-19 21:11:14 +00:00
|
|
|
return { name, num: __cache_counter[name] };
|
2020-08-22 13:20:20 +00:00
|
|
|
} catch (error) {
|
2024-10-31 05:58:21 +00:00
|
|
|
logger.error("get count by name is error: ", error);
|
2024-10-19 21:11:14 +00:00
|
|
|
return defaultCount;
|
2020-08-21 16:14:01 +00:00
|
|
|
}
|
|
|
|
}
|