kopia lustrzana https://github.com/badgen/badgen.net
Serve docs on live badge root path
rodzic
9deadbc250
commit
768d427c26
25
index.ts
25
index.ts
|
@ -4,13 +4,18 @@ import http from 'http'
|
|||
import serveHandler from 'serve-handler'
|
||||
|
||||
import serve404 from './libs/serve-404'
|
||||
import serveDocs from './endpoints/docs'
|
||||
|
||||
const sendError = (req, res, error) => {
|
||||
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 badgeHandlers = fs.readdirSync(path.join(__dirname, 'endpoints'))
|
||||
.filter(name => /\.[jt]s$/.test(name))
|
||||
.map(name => name.replace(/\.[jt]s$/, ''))
|
||||
|
@ -35,8 +40,10 @@ const serveStaticHeaders = [
|
|||
|
||||
const { PUB_DIR = '.' } = process.env
|
||||
const server = http.createServer(async (req, res) => {
|
||||
const url = req.url || '/'
|
||||
|
||||
// handle statics
|
||||
if (isStatic(req.url)) {
|
||||
if (isStatic(url)) {
|
||||
return serveHandler(req, res, {
|
||||
public: path.resolve(__dirname, PUB_DIR),
|
||||
headers: serveStaticHeaders
|
||||
|
@ -44,22 +51,22 @@ const server = http.createServer(async (req, res) => {
|
|||
}
|
||||
|
||||
// handle `/docs/:name`
|
||||
if (req.url!.startsWith('/docs/')) {
|
||||
return serveDocs(req, res)
|
||||
if (url.startsWith('/docs/')) {
|
||||
return sendRedirection(res, 301, url.replace('/docs', ''))
|
||||
}
|
||||
|
||||
// handle endpoints
|
||||
const handlerName = badgeHandlers.find(h => req.url!.startsWith(`/${h}/`))
|
||||
const handlerName = badgeHandlers.find(h => url.startsWith(`/${h}`))
|
||||
|
||||
try {
|
||||
if (handlerName) {
|
||||
const handlerPath = path.join(__dirname, 'endpoints', handlerName)
|
||||
const { default: handler } = await import(handlerPath)
|
||||
return handler(req, res)
|
||||
return handler(req, res, handlerName)
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
return sendError(req, res, error)
|
||||
return sendError(res, error)
|
||||
}
|
||||
|
||||
return serve404(req, res)
|
||||
|
@ -73,7 +80,7 @@ if (require.main === module) {
|
|||
}
|
||||
|
||||
process.on('unhandledRejection', e => {
|
||||
console.error(500, e)
|
||||
console.error('REJECTION', e)
|
||||
})
|
||||
|
||||
export default server
|
||||
|
|
|
@ -3,6 +3,7 @@ import matchRoute from 'my-way'
|
|||
|
||||
import fetchIcon from './fetch-icon'
|
||||
import serveBadge from './serve-badge'
|
||||
import serveDocs from './serve-docs'
|
||||
import serve404 from './serve-404'
|
||||
import sentry from './sentry'
|
||||
|
||||
|
@ -20,15 +21,20 @@ export type BadgenServeHandler = (args: BadgenServeHandlerArgs) => BadgenServeHa
|
|||
export type BadgenServeHandlers = { [key: string]: BadgenServeHandler }
|
||||
|
||||
export function badgenServe (handlers: BadgenServeHandlers): Function {
|
||||
return async function httpHandler (req, res) {
|
||||
return async function Handler (req, res, name) {
|
||||
const { pathname = '/', query } = url.parse(req.url, true)
|
||||
|
||||
// serve favicon
|
||||
// Serve favicon
|
||||
if (pathname === '/favicon.ico') {
|
||||
return res.end()
|
||||
}
|
||||
|
||||
// Lookup handler
|
||||
// Serve docs
|
||||
if (matchRoute(`/${name}`, pathname)) {
|
||||
return serveDocs(req, res)
|
||||
}
|
||||
|
||||
// Find handler
|
||||
let matchedArgs
|
||||
const matchedScheme = Object.keys(handlers).find(scheme => {
|
||||
matchedArgs = matchRoute(scheme, decodeURI(pathname))
|
||||
|
|
|
@ -2,14 +2,11 @@ import serveMarked from 'serve-marked'
|
|||
import serve404 from '../libs/serve-404'
|
||||
import genHelp from '../libs/gen-help'
|
||||
|
||||
// Handles `/docs/:name`
|
||||
export default async function (req, res) {
|
||||
const [ , , name ] = req.url.split('/')
|
||||
const [ , name ] = req.url.split('/')
|
||||
const helpMarkdown = genHelp(name)
|
||||
|
||||
if (helpMarkdown) {
|
||||
console.info(`DOC ${name}: ${req.url}`)
|
||||
|
||||
res.setHeader('Cache-Control', 'public, max-age=86400, s-maxage=604800, stale-while-revalidate=86400')
|
||||
|
||||
return serveMarked(helpMarkdown, {
|
Ładowanie…
Reference in New Issue