og/server.js

112 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-08-02 18:09:44 -04:00
import express from 'express'
2023-08-02 18:37:47 -04:00
import { Eta } from 'eta'
2023-08-02 19:54:54 -04:00
import render, { renderHtml } from './lib/render.js'
2023-08-02 23:00:44 -04:00
import languageColors from './lib/languagecolors.js'
2023-08-03 03:13:34 -04:00
import fetch from 'node-fetch'
// satori is using the native fetch api, causing a warning, so make node-fetch the default
globalThis.fetch = fetch
2023-08-02 18:51:06 -04:00
const debug = process.env.DEBUG ? true : false
2023-08-02 18:37:47 -04:00
const eta = new Eta({
views: 'views',
2023-08-02 18:51:06 -04:00
cache: !debug
2023-08-02 18:37:47 -04:00
})
2023-08-02 18:09:44 -04:00
2023-08-02 18:17:21 -04:00
const app = express()
2023-08-02 18:09:44 -04:00
2023-08-02 18:17:21 -04:00
app.get('/', function (req, res) {
res.redirect('https://git.gay/gitgay/og.git')
})
2023-08-02 18:09:44 -04:00
2023-08-02 22:00:54 -04:00
function getLanguagePercentages(languages) {
let sum = 0
const values = Object.values(languages)
for (const value of values) {
sum += value
}
return Object.fromEntries(
Object.keys(languages).map(language => {
return [language, (languages[language] / sum) * 100]
})
)
}
2023-08-02 18:17:21 -04:00
app.get('/:owner/:repo', async function (req, res) {
2023-08-02 18:19:26 -04:00
const repoResp = await fetch(
`https://git.gay/api/v1/repos/${encodeURIComponent(
req.params.owner
)}/${encodeURIComponent(req.params.repo)}`
2023-08-02 18:19:26 -04:00
)
if (!repoResp.ok) {
res.status(repoResp.status)
res.end()
return
}
2023-08-02 18:19:26 -04:00
const repo = await repoResp.json()
2023-08-02 22:00:54 -04:00
let languages = {}
if (!repo.empty && repo.languages_url) {
const languagesResp = await fetch(
`https://git.gay/api/v1/repos/${encodeURIComponent(
req.params.owner
)}/${encodeURIComponent(req.params.repo)}/languages`
)
if (languagesResp.ok) {
languages = getLanguagePercentages(await languagesResp.json())
}
2023-08-02 22:00:54 -04:00
}
2023-08-02 18:51:06 -04:00
const html = await eta.renderAsync('repo', {
repo,
2023-08-02 22:00:54 -04:00
languages,
languageColors,
2023-08-02 18:51:06 -04:00
debug
})
if (debug) {
2023-08-02 20:00:50 -04:00
res.send(await renderHtml(html))
2023-08-02 18:51:06 -04:00
return
}
2023-08-02 18:37:47 -04:00
res.type('png')
res.set('Content-Disposition', 'inline')
2023-08-02 18:51:06 -04:00
res.send(await render(html))
2023-08-02 18:09:44 -04:00
})
app.get('/:owner/:repo/issue/:num', async function (req, res) {
2023-08-02 19:10:20 -04:00
const issueResp = await fetch(
`https://git.gay/api/v1/repos/${encodeURIComponent(
req.params.owner
)}/${encodeURIComponent(req.params.repo)}/issues/${encodeURIComponent(
req.params.num
)}`
)
if (!issueResp.ok) {
res.status(issueResp.status)
res.end()
return
}
const issue = await issueResp.json()
2023-08-02 22:00:54 -04:00
let languages = {}
const languagesResp = await fetch(
`https://git.gay/api/v1/repos/${encodeURIComponent(
req.params.owner
)}/${encodeURIComponent(req.params.repo)}/languages`
)
2023-08-02 23:50:41 -04:00
if (languagesResp.ok) {
languages = getLanguagePercentages(await languagesResp.json())
}
const html = await eta.renderAsync('issue', {
issue,
2023-08-02 22:00:54 -04:00
languages,
languageColors,
debug
})
if (debug) {
res.send(await renderHtml(html))
return
}
res.type('png')
res.set('Content-Disposition', 'inline')
res.send(await render(html))
})
2023-08-02 18:17:21 -04:00
app.listen(8085)