2020-07-12 01:50:42 +08:00
|
|
|
require("dotenv").config();
|
2020-07-21 00:13:51 +08:00
|
|
|
const {
|
|
|
|
renderError,
|
|
|
|
parseBoolean,
|
|
|
|
clampValue,
|
|
|
|
CONSTANTS,
|
2020-07-23 15:35:50 +08:00
|
|
|
logger,
|
2020-07-21 00:13:51 +08:00
|
|
|
} = require("../src/utils");
|
2020-07-12 00:40:13 +08:00
|
|
|
const fetchRepo = require("../src/fetchRepo");
|
|
|
|
const renderRepoCard = require("../src/renderRepoCard");
|
2020-07-10 00:53:14 +08:00
|
|
|
|
|
|
|
module.exports = async (req, res) => {
|
2020-07-12 15:16:08 +08:00
|
|
|
const {
|
|
|
|
username,
|
|
|
|
repo,
|
|
|
|
title_color,
|
|
|
|
icon_color,
|
|
|
|
text_color,
|
|
|
|
bg_color,
|
2020-07-19 23:04:41 +08:00
|
|
|
theme,
|
2020-07-16 20:33:33 +08:00
|
|
|
show_owner,
|
2020-07-21 00:13:51 +08:00
|
|
|
cache_seconds,
|
2020-07-12 15:16:08 +08:00
|
|
|
} = req.query;
|
2020-07-10 00:53:14 +08:00
|
|
|
|
2020-07-10 20:42:31 +08:00
|
|
|
let repoData;
|
2020-07-16 20:33:33 +08:00
|
|
|
|
2020-07-10 00:53:14 +08:00
|
|
|
res.setHeader("Content-Type", "image/svg+xml");
|
2020-07-10 20:42:31 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
repoData = await fetchRepo(username, repo);
|
|
|
|
} catch (err) {
|
2020-07-23 15:35:50 +08:00
|
|
|
logger.error(err);
|
2020-07-10 20:42:31 +08:00
|
|
|
return res.send(renderError(err.message));
|
|
|
|
}
|
|
|
|
|
2020-07-21 00:13:51 +08:00
|
|
|
let cacheSeconds = clampValue(
|
|
|
|
parseInt(cache_seconds || CONSTANTS.THIRTY_MINUTES, 10),
|
|
|
|
CONSTANTS.THIRTY_MINUTES,
|
|
|
|
CONSTANTS.ONE_DAY
|
|
|
|
);
|
|
|
|
|
|
|
|
/*
|
|
|
|
if star count & fork count is over 1k then we are kFormating the text
|
|
|
|
and if both are zero we are not showing the stats
|
|
|
|
so we can just make the cache longer, since there is no need to frequent updates
|
|
|
|
*/
|
|
|
|
const stars = repoData.stargazers.totalCount;
|
|
|
|
const forks = repoData.forkCount;
|
|
|
|
const isBothOver1K = stars > 1000 && forks > 1000;
|
|
|
|
const isBothUnder1 = stars < 1 && forks < 1;
|
|
|
|
if (!cache_seconds && (isBothOver1K || isBothUnder1)) {
|
|
|
|
cacheSeconds = CONSTANTS.TWO_HOURS;
|
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
|
2020-07-23 15:35:50 +08:00
|
|
|
|
2020-07-12 15:16:08 +08:00
|
|
|
res.send(
|
|
|
|
renderRepoCard(repoData, {
|
|
|
|
title_color,
|
|
|
|
icon_color,
|
|
|
|
text_color,
|
|
|
|
bg_color,
|
2020-07-19 23:04:41 +08:00
|
|
|
theme,
|
2020-07-16 20:33:33 +08:00
|
|
|
show_owner: parseBoolean(show_owner),
|
2020-07-12 15:16:08 +08:00
|
|
|
})
|
|
|
|
);
|
2020-07-10 00:53:14 +08:00
|
|
|
};
|