|
1 | | -import { Request, Response } from 'express'; |
2 | | -import { GitHubClient } from '../utils/github-client.js'; |
3 | | -import { LanguageCardRenderer } from '../components/language-card.js'; |
4 | | -import { LanguagePieChartRenderer } from '../components/language-pie-chart.js'; |
5 | | -export class LanguageController { |
6 | | - private static githubClient: GitHubClient; |
7 | | - private static cache: Map<string, { data: string; timestamp: number }>; |
8 | | - private static CACHE_DURATION: number; |
9 | | - static routeDocs = { |
10 | | - requiredParams: ['username'], |
11 | | - optionalParams: [ |
12 | | - 'type', |
13 | | - 'theme', |
14 | | - 'show_info', |
15 | | - 'info_outline' |
16 | | - ], |
17 | | - payload: null as null, |
18 | | - example: '/languages?username=pphatdev&type=card&theme=default' |
19 | | - }; |
20 | | - |
21 | | - static initialize(githubClient: GitHubClient, cache: Map<string, { data: string; timestamp: number }>, cacheDuration: number) { |
22 | | - this.githubClient = githubClient; |
23 | | - this.cache = cache; |
24 | | - this.CACHE_DURATION = cacheDuration; |
25 | | - } |
26 | | - |
27 | | - static async getSvg(req: Request, res: Response) { |
28 | | - try { |
29 | | - const { username, type = 'card', theme = 'default', show_info, info_outline = 'solid' } = req.query; |
30 | | - |
31 | | - if (!username || typeof username !== 'string') { |
32 | | - return res.status(400).send('Username is required'); |
33 | | - } |
34 | | - |
35 | | - const cacheKey = `languages-${username}-${type}-${theme}-${show_info}-${info_outline}`; |
36 | | - const cached = LanguageController.cache.get(cacheKey); |
37 | | - if (cached && Date.now() - cached.timestamp < LanguageController.CACHE_DURATION) { |
38 | | - res.setHeader('Content-Type', 'image/svg+xml'); |
39 | | - res.setHeader('Cache-Control', 'public, max-age=600'); |
40 | | - return res.send(cached.data); |
41 | | - } |
42 | | - |
43 | | - const languages = await LanguageController.githubClient.fetchUserLanguages(username); |
44 | | - |
45 | | - let svg: string; |
46 | | - if (type === 'pie') { |
47 | | - svg = LanguagePieChartRenderer.generatePieChart(languages, { |
48 | | - theme: theme as string, |
49 | | - }); |
50 | | - } else { |
51 | | - svg = LanguageCardRenderer.generateLanguagesCard(languages, { |
52 | | - theme: theme as string, |
53 | | - showInfo: show_info !== 'false', |
54 | | - dataBorderStyle: info_outline === 'frame' ? 'frame' : 'solid', |
55 | | - }); |
56 | | - } |
57 | | - |
58 | | - LanguageController.cache.set(cacheKey, { data: svg, timestamp: Date.now() }); |
59 | | - |
60 | | - res.setHeader('Content-Type', 'image/svg+xml'); |
61 | | - res.setHeader('Cache-Control', 'public, max-age=600'); |
62 | | - res.send(svg); |
63 | | - } catch (error) { |
64 | | - console.error('Error generating languages background:', error); |
65 | | - res.status(500).send(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`); |
66 | | - } |
67 | | - } |
68 | | -} |
| 1 | +import { Request, Response } from 'express'; |
| 2 | +import { GitHubClient } from '../utils/github-client.js'; |
| 3 | +import { LanguageCardRenderer } from '../components/language-card.js'; |
| 4 | +import { LanguagePieChartRenderer } from '../components/language-pie-chart.js'; |
| 5 | +export class LanguageController { |
| 6 | + private static githubClient: GitHubClient; |
| 7 | + private static cache: Map<string, { data: string; timestamp: number }>; |
| 8 | + private static CACHE_DURATION: number; |
| 9 | + static routeDocs = { |
| 10 | + requiredParams: ['username'], |
| 11 | + optionalParams: [ |
| 12 | + 'type', |
| 13 | + 'theme', |
| 14 | + 'show_info', |
| 15 | + 'info_outline' |
| 16 | + ], |
| 17 | + payload: null as null, |
| 18 | + example: '/languages?username=pphatdev&type=card&theme=default' |
| 19 | + }; |
| 20 | + |
| 21 | + static initialize(githubClient: GitHubClient, cache: Map<string, { data: string; timestamp: number }>, cacheDuration: number) { |
| 22 | + this.githubClient = githubClient; |
| 23 | + this.cache = cache; |
| 24 | + this.CACHE_DURATION = cacheDuration; |
| 25 | + } |
| 26 | + |
| 27 | + static async getSvg(req: Request, res: Response) { |
| 28 | + try { |
| 29 | + const { username, type = 'card', theme = 'default', show_info, info_outline = 'solid' } = req.query; |
| 30 | + |
| 31 | + if (!username || typeof username !== 'string') { |
| 32 | + return res.status(400).send('Username is required'); |
| 33 | + } |
| 34 | + |
| 35 | + const cacheKey = `languages-${username}-${type}-${theme}-${show_info}-${info_outline}`; |
| 36 | + const cached = LanguageController.cache.get(cacheKey); |
| 37 | + if (cached && Date.now() - cached.timestamp < LanguageController.CACHE_DURATION) { |
| 38 | + res.setHeader('Content-Type', 'image/svg+xml'); |
| 39 | + res.setHeader('Cache-Control', 'public, max-age=600'); |
| 40 | + return res.send(cached.data); |
| 41 | + } |
| 42 | + |
| 43 | + const languages = await LanguageController.githubClient.fetchUserLanguages(username); |
| 44 | + |
| 45 | + let svg: string; |
| 46 | + if (type === 'pie') { |
| 47 | + svg = LanguagePieChartRenderer.generatePieChart(languages, { |
| 48 | + theme: theme as string, |
| 49 | + }); |
| 50 | + } else { |
| 51 | + svg = LanguageCardRenderer.generateLanguagesCard(languages, { |
| 52 | + theme: theme as string, |
| 53 | + showInfo: show_info !== 'false', |
| 54 | + dataBorderStyle: info_outline === 'frame' ? 'frame' : 'solid', |
| 55 | + }); |
| 56 | + } |
| 57 | + |
| 58 | + LanguageController.cache.set(cacheKey, { data: svg, timestamp: Date.now() }); |
| 59 | + |
| 60 | + res.setHeader('Content-Type', 'image/svg+xml'); |
| 61 | + res.setHeader('Cache-Control', 'public, max-age=600'); |
| 62 | + res.send(svg); |
| 63 | + } catch (error) { |
| 64 | + console.error('Error generating languages background:', error); |
| 65 | + res.status(500).send(`Error: ${error instanceof Error ? error.message : 'Unknown error'}`); |
| 66 | + } |
| 67 | + } |
| 68 | +} |
0 commit comments