Moe-Counter/index.js

141 lines
3.2 KiB
JavaScript
Raw Normal View History

"use strict";
2020-08-03 12:11:58 +00:00
const config = require("config-yml");
const express = require("express");
const compression = require("compression");
2020-08-03 12:11:58 +00:00
const db = require("./db");
const themify = require("./utils/themify");
2020-08-03 12:11:58 +00:00
const app = express();
2020-08-03 12:11:58 +00:00
app.use(express.static("assets"));
app.use(compression());
app.set("view engine", "pug");
2020-08-03 12:11:58 +00:00
app.get('/', (req, res) => {
const site = config.app.site || `${req.protocol}://${req.get('host')}`
res.render('index', { site })
2020-08-03 12:11:58 +00:00
});
// get the image
app.get(["/@:name", "/get/@:name"], async (req, res) => {
const { name } = req.params;
const { theme = "moebooru", padding = 7, pixelated = '1', darkmode = 'auto' } = req.query;
const isPixelated = pixelated === '1';
if (name.length > 32) {
res.status(400).send("name too long");
return;
}
if (padding > 32) {
res.status(400).send("padding too long");
return;
}
2020-08-03 12:11:58 +00:00
// This helps with GitHub's image cache
2020-08-03 12:11:58 +00:00
res.set({
"content-type": "image/svg+xml",
"cache-control": "max-age=0, no-cache, no-store, must-revalidate",
});
2020-08-03 12:11:58 +00:00
const data = await getCountByName(name);
if (name === "demo") {
res.set("cache-control", "max-age=31536000");
2020-08-05 05:37:12 +00:00
}
2020-08-03 12:11:58 +00:00
// Send the generated SVG as the result
const renderSvg = themify.getCountImage({
count: data.num,
theme,
padding,
darkmode,
pixelated: isPixelated
});
res.send(renderSvg);
console.log(
data,
`theme: ${theme}`,
`ip: ${req.headers['x-forwarded-for'] || req.connection.remoteAddress}`,
`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);
2020-08-03 12:11:58 +00:00
res.json(data);
});
2020-08-03 12:11:58 +00:00
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");
2020-08-03 12:11:58 +00:00
});
const listener = app.listen(config.app.port || 3000, () => {
console.log("Your app is listening on port " + listener.address().port);
});
let __cache_counter = {};
let enablePushDelay = config.db.interval > 0
let needPush = false;
if (enablePushDelay) {
setInterval(() => {
needPush = true;
}, 1000 * config.db.interval);
}
async function pushDB() {
if (Object.keys(__cache_counter).length === 0) return;
if (enablePushDelay && !needPush) return;
try {
needPush = false;
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;
}
}