2020-07-21 19:33:05 +08:00
|
|
|
require("dotenv").config();
|
2020-07-21 20:34:58 +08:00
|
|
|
const {
|
|
|
|
renderError,
|
|
|
|
clampValue,
|
|
|
|
parseBoolean,
|
2020-07-23 23:31:23 +08:00
|
|
|
parseArray,
|
2020-07-21 20:34:58 +08:00
|
|
|
CONSTANTS,
|
2020-08-02 15:37:26 +08:00
|
|
|
} = require("../src/common/utils");
|
|
|
|
const fetchTopLanguages = require("../src/fetchers/top-languages-fetcher");
|
|
|
|
const renderTopLanguages = require("../src/cards/top-languages-card");
|
2020-08-17 16:48:17 +08:00
|
|
|
const blacklist = require("../src/common/blacklist");
|
2020-07-21 19:33:05 +08:00
|
|
|
|
|
|
|
module.exports = async (req, res) => {
|
|
|
|
const {
|
|
|
|
username,
|
2020-07-23 22:17:04 +08:00
|
|
|
hide,
|
2020-07-21 20:34:58 +08:00
|
|
|
hide_title,
|
2020-07-30 21:49:03 +08:00
|
|
|
hide_border,
|
2020-07-21 19:33:05 +08:00
|
|
|
card_width,
|
|
|
|
title_color,
|
|
|
|
text_color,
|
|
|
|
bg_color,
|
|
|
|
theme,
|
|
|
|
cache_seconds,
|
2020-07-30 21:49:03 +08:00
|
|
|
layout,
|
2020-09-12 21:16:18 +08:00
|
|
|
langs_count,
|
2020-07-21 19:33:05 +08:00
|
|
|
} = req.query;
|
|
|
|
let topLangs;
|
|
|
|
|
|
|
|
res.setHeader("Content-Type", "image/svg+xml");
|
|
|
|
|
2020-08-17 16:48:17 +08:00
|
|
|
if (blacklist.includes(username)) {
|
|
|
|
return res.send(renderError("Something went wrong"));
|
|
|
|
}
|
|
|
|
|
2020-07-21 19:33:05 +08:00
|
|
|
try {
|
2020-09-12 21:16:18 +08:00
|
|
|
topLangs = await fetchTopLanguages(username, langs_count);
|
2020-07-21 19:33:05 +08:00
|
|
|
|
2020-08-13 23:48:26 +08:00
|
|
|
const cacheSeconds = clampValue(
|
|
|
|
parseInt(cache_seconds || CONSTANTS.TWO_HOURS, 10),
|
|
|
|
CONSTANTS.TWO_HOURS,
|
2020-09-25 00:08:14 +08:00
|
|
|
CONSTANTS.ONE_DAY,
|
2020-08-13 23:48:26 +08:00
|
|
|
);
|
2020-07-21 19:33:05 +08:00
|
|
|
|
2020-08-13 23:48:26 +08:00
|
|
|
res.setHeader("Cache-Control", `public, max-age=${cacheSeconds}`);
|
2020-07-21 19:33:05 +08:00
|
|
|
|
2020-08-13 23:48:26 +08:00
|
|
|
return res.send(
|
|
|
|
renderTopLanguages(topLangs, {
|
|
|
|
hide_title: parseBoolean(hide_title),
|
|
|
|
hide_border: parseBoolean(hide_border),
|
|
|
|
card_width: parseInt(card_width, 10),
|
|
|
|
hide: parseArray(hide),
|
|
|
|
title_color,
|
|
|
|
text_color,
|
|
|
|
bg_color,
|
|
|
|
theme,
|
|
|
|
layout,
|
2020-09-25 00:08:14 +08:00
|
|
|
}),
|
2020-08-13 23:48:26 +08:00
|
|
|
);
|
|
|
|
} catch (err) {
|
|
|
|
return res.send(renderError(err.message, err.secondaryMessage));
|
|
|
|
}
|
2020-07-21 19:33:05 +08:00
|
|
|
};
|