const express = require('express');
const axios = require('axios');
const app = express();
const PORT = process.env.PORT || 3000;
app.get('/badge', async (req, res) => {
const {
icon = 'portainer',
label = 'Portainer',
bg = '#f0f0f0',
color = '#000000',
size = 24,
labelpos = 'right', // lehet: 'right', 'left', 'above', 'below'
fontweight = 'normal', // új paraméter, alapértelmezett: normal
} = req.query;
try {
// Ikon letöltése
const iconUrl = `https://cdn.simpleicons.org/${icon}?viewbox=auto&size=${size}`;
const iconResponse = await axios.get(iconUrl, { responseType: 'text' });
if (iconResponse.status !== 200) {
return res.status(404).send('Icon not found');
}
const iconSVG = iconResponse.data;
// Ha a color paraméter nincs megadva, próbáljuk kinyerni az ikon színét
let effectiveColor = color;
if (!req.query.color || req.query.color === '#000000') {
// Próbáljuk kinyerni az ikon fő színét a SimpleIcons API-ból
try {
const iconMetaUrl = `https://cdn.simpleicons.org/${icon}`;
const iconMetaResp = await axios.get(iconMetaUrl, { responseType: 'text' });
// Kikeressük az első "fill=" attribútumot
const match = iconMetaResp.data.match(/fill="([^"]+)"/);
if (match && match[1]) {
effectiveColor = match[1];
} else {
effectiveColor = "#222"; // fallback
}
} catch {
effectiveColor = "#222";
}
}
// Badge méretek
const padding = 10;
const iconSize = parseInt(size);
const fontSize = Math.round(iconSize * 0.6); // Font size az ikon méret 60%-a
const textPadding = Math.round(iconSize * 0.4); // Dinamikus: size 40%-a
const radius = 8;
const approxCharWidth = fontSize * 0.6;
const textWidth = label.length * approxCharWidth;
let width, height, iconGroup, textElem;
// Szebb, élsimított font shields.io stílusban
const fontFamily = "Verdana,Geneva,DejaVu Sans,sans-serif";
const fontWeight = fontweight; // paraméterből
if (labelpos === 'above' || labelpos === 'below') {
width = Math.max(iconSize, textWidth) + padding * 2;
height = padding * 2 + iconSize + textPadding + fontSize;
if (labelpos === 'above') {
// Szöveg felül, ikon alul
textElem = `${label}`;
iconGroup = `${iconSVG}`;
} else {
// Szöveg alul, ikon felül
iconGroup = `${iconSVG}`;
textElem = `${label}`;
}
} else if (labelpos === 'left') {
width = padding * 2 + iconSize + textPadding + textWidth;
height = Math.max(iconSize + padding * 2, fontSize + padding * 2);
// Szöveg balra, ikon jobbra (függőleges közép)
textElem = `${label}`;
iconGroup = `${iconSVG}`;
} else {
width = padding * 2 + iconSize + textPadding + textWidth;
height = Math.max(iconSize + padding * 2, fontSize + padding * 2);
// Alapértelmezett: ikon balra, szöveg jobbra (függőleges közép)
iconGroup = `${iconSVG}`;
textElem = `${label}`;
}
// SVG badge string összefűzéssel, szöveg árnyékkal
const svg = `