clean up options code

This commit is contained in:
hazycora 2023-12-21 06:47:43 -06:00
parent 6956faad46
commit a395fc6e9a
No known key found for this signature in database
GPG key ID: 215AF1F81F86940E

View file

@ -51,6 +51,20 @@ async function getLanguages(owner, repo) {
}
const comicSansNames = new Set(['trixie'])
const allowedFormats = new Set(['png', 'svg', 'json'])
app.use('/', function (req, res, next) {
if (req.query.format && !allowedFormats.has(req.query.format)) {
res.status(400)
res.end()
return
}
app.locals.options = {
fonts: comicSansNames.has(req.params.owner) ? ComicSans : DMSans,
format: req.query.format ?? 'png'
}
next()
})
app.get('/:user', async function (req, res) {
const [userResp, reposResp] = await Promise.all([
@ -81,13 +95,9 @@ app.get('/:user', async function (req, res) {
res.send(await renderHtml(html))
return
}
const options = {
fonts: DMSans,
format: req.query.format == 'svg' ? 'svg' : 'png'
}
res.type(options.format)
res.type(app.locals.options.format)
res.set('Content-Disposition', 'inline')
res.send(await render(html, options))
res.send(await render(html, app.locals.options))
})
app.get('/:owner/:repo', async function (req, res) {
@ -122,13 +132,9 @@ app.get('/:owner/:repo', async function (req, res) {
res.send(await renderHtml(html))
return
}
const options = {
fonts: comicSansNames.has(req.params.owner) ? ComicSans : DMSans,
format: req.query.format == 'svg' ? 'svg' : 'png'
}
res.type(options.format)
res.type(app.locals.options.format)
res.set('Content-Disposition', 'inline')
res.send(await render(html, options))
res.send(await render(html, app.locals.options))
})
app.get('/:owner/:repo/commit/:hash', async function (req, res) {
@ -172,13 +178,9 @@ app.get('/:owner/:repo/commit/:hash', async function (req, res) {
res.send(await renderHtml(html))
return
}
const options = {
fonts: comicSansNames.has(req.params.owner) ? ComicSans : DMSans,
format: req.query.format == 'svg' ? 'svg' : 'png'
}
res.type(options.format)
res.type(app.locals.options.format)
res.set('Content-Disposition', 'inline')
res.send(await render(html, options))
res.send(await render(html, app.locals.options))
})
app.get('/:owner/:repo/:type/:num', (req, res, next) => {
@ -221,13 +223,9 @@ app.get('/:owner/:repo/issues/:num', async function (req, res) {
res.send(await renderHtml(html))
return
}
const options = {
fonts: comicSansNames.has(req.params.owner) ? ComicSans : DMSans,
format: req.query.format == 'svg' ? 'svg' : 'png'
}
res.type(options.format)
res.type(app.locals.options.format)
res.set('Content-Disposition', 'inline')
res.send(await render(html, options))
res.send(await render(html, app.locals.options))
})
app.get('/:owner/:repo/pulls/:num', async function (req, res) {
@ -276,13 +274,9 @@ app.get('/:owner/:repo/pulls/:num', async function (req, res) {
res.send(await renderHtml(html))
return
}
const options = {
fonts: comicSansNames.has(req.params.owner) ? ComicSans : DMSans,
format: req.query.format == 'svg' ? 'svg' : 'png'
}
res.type(options.format)
res.type(app.locals.options.format)
res.set('Content-Disposition', 'inline')
res.send(await render(html, options))
res.send(await render(html, app.locals.options))
})
const [port, host] = [process.env.PORT ?? 9054, process.env.HOST ?? '127.0.0.1']