clean up options code
This commit is contained in:
parent
6956faad46
commit
a395fc6e9a
1 changed files with 24 additions and 30 deletions
54
server.js
54
server.js
|
@ -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']
|
||||
|
|
Loading…
Reference in a new issue