kopia lustrzana https://github.com/badgen/badgen.net
chore: set api timeout 3200ms -> 4200ms
rodzic
549fd745da
commit
7e2e354c7e
|
@ -1,3 +0,0 @@
|
||||||
language: node_js
|
|
||||||
node_js:
|
|
||||||
- "10"
|
|
87
index.ts
87
index.ts
|
@ -1,87 +0,0 @@
|
||||||
import fs from 'fs'
|
|
||||||
import path from 'path'
|
|
||||||
import http from 'http'
|
|
||||||
import matchRoute from 'my-way'
|
|
||||||
import serveHandler from 'serve-handler'
|
|
||||||
|
|
||||||
import serve404 from './libs/serve-404'
|
|
||||||
|
|
||||||
const sendError = (res: http.ServerResponse, error: Error) => {
|
|
||||||
res.statusCode = 500
|
|
||||||
res.end(error.message)
|
|
||||||
}
|
|
||||||
|
|
||||||
const sendRedirection = (res: http.ServerResponse, code: number, dest: string) => {
|
|
||||||
res.statusCode = code
|
|
||||||
res.setHeader('Location', dest)
|
|
||||||
res.end()
|
|
||||||
}
|
|
||||||
|
|
||||||
const badgeNames = fs.readdirSync(path.join(__dirname, 'api'))
|
|
||||||
.filter(name => /\.[jt]s$/.test(name))
|
|
||||||
.map(name => name.replace(/\.[jt]s$/, ''))
|
|
||||||
|
|
||||||
const isStatic = (url) => {
|
|
||||||
if (url === '/') return true
|
|
||||||
if (url.startsWith('/_next/')) return true
|
|
||||||
if (url.startsWith('/static/')) return true
|
|
||||||
if (url.startsWith('/builder')) return true
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
const serveStaticHeaders = [
|
|
||||||
{
|
|
||||||
source: '**/*',
|
|
||||||
headers: [{
|
|
||||||
key: 'Cache-Control',
|
|
||||||
value: 'public, max-age=86400, s-maxage=604800, stale-while-revalidate=86400'
|
|
||||||
}]
|
|
||||||
}
|
|
||||||
]
|
|
||||||
|
|
||||||
const { PUB_DIR = '.' } = process.env
|
|
||||||
const server = http.createServer(async (req, res) => {
|
|
||||||
const url = req.url || '/'
|
|
||||||
|
|
||||||
// handle statics
|
|
||||||
if (isStatic(url)) {
|
|
||||||
return serveHandler(req, res, {
|
|
||||||
public: path.resolve(__dirname, PUB_DIR),
|
|
||||||
headers: serveStaticHeaders
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
// redirects `/docs/:name` to `/:name`
|
|
||||||
if (url.startsWith('/docs/')) {
|
|
||||||
return sendRedirection(res, 301, url.replace('/docs', ''))
|
|
||||||
}
|
|
||||||
|
|
||||||
// handle endpoints
|
|
||||||
const handlerName = badgeNames.find(h => matchRoute(`/${h}/:path*`, url))
|
|
||||||
|
|
||||||
try {
|
|
||||||
if (handlerName) {
|
|
||||||
const handlerPath = path.join(__dirname, 'api', handlerName)
|
|
||||||
const { default: handler } = await import(handlerPath)
|
|
||||||
return handler(req, res, handlerName)
|
|
||||||
}
|
|
||||||
} catch (error: any) {
|
|
||||||
console.error(error)
|
|
||||||
return sendError(res, error)
|
|
||||||
}
|
|
||||||
|
|
||||||
return serve404(req, res)
|
|
||||||
})
|
|
||||||
|
|
||||||
// Auto run
|
|
||||||
if (require.main === module) {
|
|
||||||
const port = process.env.PORT || 3000
|
|
||||||
server.listen(port)
|
|
||||||
console.log(`Badgen listening on port ${port}`)
|
|
||||||
}
|
|
||||||
|
|
||||||
process.on('unhandledRejection', e => {
|
|
||||||
console.error('REJECTION', e)
|
|
||||||
})
|
|
||||||
|
|
||||||
export default server
|
|
|
@ -62,7 +62,7 @@ export function createBadgenHandler (badgenServerConfig: BadgenServeConfig) {
|
||||||
function onBadgeHandlerError (meta: any, err: Error | HTTPError, req: NextApiRequest, res: NextApiResponse) {
|
function onBadgeHandlerError (meta: any, err: Error | HTTPError, req: NextApiRequest, res: NextApiResponse) {
|
||||||
sentry.captureException(err)
|
sentry.captureException(err)
|
||||||
|
|
||||||
console.error('BADGE_HANDLER_ERROR', err.message, meta)
|
console.error('BADGE_HANDLER_ERROR', err.message, req.url)
|
||||||
|
|
||||||
// Send user friendly response
|
// Send user friendly response
|
||||||
const errorBadgeParams = {
|
const errorBadgeParams = {
|
||||||
|
|
|
@ -2,7 +2,7 @@ import got from 'got'
|
||||||
|
|
||||||
export default got.extend({
|
export default got.extend({
|
||||||
timeout: {
|
timeout: {
|
||||||
request: 3200,
|
request: 4200,
|
||||||
},
|
},
|
||||||
retry: {
|
retry: {
|
||||||
limit: 0,
|
limit: 0,
|
||||||
|
|
|
@ -4,6 +4,8 @@ import { serveMarked } from 'serve-marked'
|
||||||
import serve404 from '../libs/serve-404'
|
import serve404 from '../libs/serve-404'
|
||||||
import { BadgenServeConfig } from '../libs/create-badgen-handler'
|
import { BadgenServeConfig } from '../libs/create-badgen-handler'
|
||||||
|
|
||||||
|
const { TRACKING_GA = 'G-PD7EFJDYFV' } = process.env
|
||||||
|
|
||||||
export default function serveDoc (conf: BadgenServeConfig): http.RequestListener {
|
export default function serveDoc (conf: BadgenServeConfig): http.RequestListener {
|
||||||
return (req, res) => {
|
return (req, res) => {
|
||||||
const helpMarkdown = generateHelpMarkdown(conf)
|
const helpMarkdown = generateHelpMarkdown(conf)
|
||||||
|
@ -14,9 +16,18 @@ export default function serveDoc (conf: BadgenServeConfig): http.RequestListener
|
||||||
return serveMarked(helpMarkdown, {
|
return serveMarked(helpMarkdown, {
|
||||||
title: `${conf.title} badge | Badgen`,
|
title: `${conf.title} badge | Badgen`,
|
||||||
inlineCSS,
|
inlineCSS,
|
||||||
beforeHeadEnd: '<link rel="icon" href="/favicon.png">',
|
beforeHeadEnd: `
|
||||||
|
<link rel="icon" href="/favicon.png" />
|
||||||
|
<!-- Google tag (gtag.js) -->
|
||||||
|
<script async src="https://www.googletagmanager.com/gtag/js?id=${TRACKING_GA}"></script>
|
||||||
|
<script>
|
||||||
|
window.dataLayer = window.dataLayer || [];
|
||||||
|
function gtag(){dataLayer.push(arguments);}
|
||||||
|
gtag('js', new Date());
|
||||||
|
gtag('config', '${TRACKING_GA}');
|
||||||
|
</script>
|
||||||
|
`,
|
||||||
beforeBodyEnd: helpFooter,
|
beforeBodyEnd: helpFooter,
|
||||||
trackingGA: process.env.TRACKING_GA
|
|
||||||
})(req, res)
|
})(req, res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -61,7 +72,7 @@ function hashify (str: string) {
|
||||||
|
|
||||||
const inlineCSS = `
|
const inlineCSS = `
|
||||||
html, body { scroll-behavior: smooth }
|
html, body { scroll-behavior: smooth }
|
||||||
.markdown-body { max-width: 850px; min-height: calc(100vh - 348px) }
|
.markdown-body { max-width: 960px; min-height: calc(100vh - 348px) }
|
||||||
.markdown-body h1 { margin-bottom: 42px }
|
.markdown-body h1 { margin-bottom: 42px }
|
||||||
li > img { vertical-align: middle; margin: 0.2em 0; font-size: 12px; float: right }
|
li > img { vertical-align: middle; margin: 0.2em 0; font-size: 12px; float: right }
|
||||||
li > img + a { font-family: monospace; font-size: 0.9em }
|
li > img + a { font-family: monospace; font-size: 0.9em }
|
||||||
|
|
|
@ -47,9 +47,9 @@ const nextConfig = {
|
||||||
'/xo',
|
'/xo',
|
||||||
]
|
]
|
||||||
|
|
||||||
badgeApis.forEach(b => {
|
badgeApis.forEach(badge => {
|
||||||
badgeRedirects.push({ source: `${b}/:path*`, destination: `/api${b}` }) // badges
|
badgeRedirects.push({ source: `${badge}/:path*`, destination: `/api${badge}` }) // badges
|
||||||
badgeRedirects.push({ source: b, destination: `/api${b}` }) // doc pages
|
badgeRedirects.push({ source: badge, destination: `/api${badge}` }) // doc pages
|
||||||
})
|
})
|
||||||
|
|
||||||
// const badgeRedirects = liveBadgeRedirects.concat(staticBadgeRedirects)
|
// const badgeRedirects = liveBadgeRedirects.concat(staticBadgeRedirects)
|
||||||
|
|
Ładowanie…
Reference in New Issue