diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 4555503..0000000 --- a/.travis.yml +++ /dev/null @@ -1,3 +0,0 @@ -language: node_js -node_js: - - "10" diff --git a/index.ts b/index.ts deleted file mode 100644 index fbb78b8..0000000 --- a/index.ts +++ /dev/null @@ -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 diff --git a/libs/create-badgen-handler-next.ts b/libs/create-badgen-handler-next.ts index 3cdcbe5..27272e2 100644 --- a/libs/create-badgen-handler-next.ts +++ b/libs/create-badgen-handler-next.ts @@ -62,7 +62,7 @@ export function createBadgenHandler (badgenServerConfig: BadgenServeConfig) { function onBadgeHandlerError (meta: any, err: Error | HTTPError, req: NextApiRequest, res: NextApiResponse) { sentry.captureException(err) - console.error('BADGE_HANDLER_ERROR', err.message, meta) + console.error('BADGE_HANDLER_ERROR', err.message, req.url) // Send user friendly response const errorBadgeParams = { diff --git a/libs/got.ts b/libs/got.ts index 785788e..5ac2053 100644 --- a/libs/got.ts +++ b/libs/got.ts @@ -2,7 +2,7 @@ import got from 'got' export default got.extend({ timeout: { - request: 3200, + request: 4200, }, retry: { limit: 0, diff --git a/libs/serve-doc.ts b/libs/serve-doc.ts index a71d378..9df07a9 100644 --- a/libs/serve-doc.ts +++ b/libs/serve-doc.ts @@ -4,6 +4,8 @@ import { serveMarked } from 'serve-marked' import serve404 from '../libs/serve-404' import { BadgenServeConfig } from '../libs/create-badgen-handler' +const { TRACKING_GA = 'G-PD7EFJDYFV' } = process.env + export default function serveDoc (conf: BadgenServeConfig): http.RequestListener { return (req, res) => { const helpMarkdown = generateHelpMarkdown(conf) @@ -14,9 +16,18 @@ export default function serveDoc (conf: BadgenServeConfig): http.RequestListener return serveMarked(helpMarkdown, { title: `${conf.title} badge | Badgen`, inlineCSS, - beforeHeadEnd: '', + beforeHeadEnd: ` + + + + + `, beforeBodyEnd: helpFooter, - trackingGA: process.env.TRACKING_GA })(req, res) } @@ -61,7 +72,7 @@ function hashify (str: string) { const inlineCSS = ` 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 } li > img { vertical-align: middle; margin: 0.2em 0; font-size: 12px; float: right } li > img + a { font-family: monospace; font-size: 0.9em } diff --git a/next.config.js b/next.config.js index 3aa98bc..1cd7628 100644 --- a/next.config.js +++ b/next.config.js @@ -47,9 +47,9 @@ const nextConfig = { '/xo', ] - badgeApis.forEach(b => { - badgeRedirects.push({ source: `${b}/:path*`, destination: `/api${b}` }) // badges - badgeRedirects.push({ source: b, destination: `/api${b}` }) // doc pages + badgeApis.forEach(badge => { + badgeRedirects.push({ source: `${badge}/:path*`, destination: `/api${badge}` }) // badges + badgeRedirects.push({ source: badge, destination: `/api${badge}` }) // doc pages }) // const badgeRedirects = liveBadgeRedirects.concat(staticBadgeRedirects)