waibu
Version:
Web Framework for Bajo
83 lines (77 loc) • 2.51 kB
JavaScript
export function writeHtml (req, reply, tpl, payload) {
const { getPluginFile } = this.app.bajo
const { fs } = this.app.lib
const { template } = this.app.lib._
reply.header('Content-Type', 'text/html')
reply.header('Content-Language', req.lang)
const file = getPluginFile(tpl)
const content = fs.readFileSync(file, 'utf8')
const compiled = template(content)
return compiled(payload)
}
export async function interceptor (name, err, req, reply) {
const { get, trim } = this.app.lib._
let webApp = get(req, 'routeOptions.config.webApp')
if (!webApp) {
const url = req.url ?? req.raw.url
const [prefix] = trim(url, '/').split('/')
webApp = this.getPluginByPrefix(prefix, true)
}
if (!webApp) {
const wa = this.webApps.find(item => item.prefix === '')
if (wa) webApp = wa.ns
}
if (webApp) {
const plugin = this.app[webApp]
const handler = get(plugin, `webAppFactory.${name}`)
if (handler) return await handler.call(plugin, err, req, reply)
}
}
function redirSvc (req) {
const { trim, find, get } = this.app.lib._
const { outmatch } = this.app.lib
let match = false
let [prefix, ...args] = trim(req.url, '/').split('/')
args = '/' + args.join('/')
let plugin = find(this.app.getAllNs(), p => {
return get(this, `app.${p}.config.waibu.prefix`) === prefix
})
if (!plugin) {
plugin = 'main'
args = `/${prefix}`
}
const items = get(this, `app.${plugin}.config.waibuMpa.redirect`, {})
for (const k in items) {
const isMatch = outmatch(k)
if (isMatch(args)) {
match = items[k]
break
}
}
return match
}
export async function notFound (err, req, reply) {
const { getMethod } = this.app.bajo
let redirectTo = await redirSvc.call(this, req, reply)
if (redirectTo !== false) {
const fn = getMethod(redirectTo, false)
if (fn) redirectTo = await fn(req)
if (redirectTo) return reply.redirectTo(redirectTo)
}
reply.code(404)
const resp = await interceptor.call(this, 'notFoundHandler', err, req, reply)
if (resp) return resp
if (err && err.noContent) return ''
const payload = {
text: req.t('notFound%s%s', req.t('route'), req.url),
title: req.t('pageNotFound')
}
return writeHtml.call(this, req, reply, `${this.ns}:/lib/template/404.html`, payload)
}
async function handleNotFound () {
const me = this
me.instance.setNotFoundHandler(async function (req, reply) {
return await notFound.call(me, null, req, reply)
})
}
export default handleNotFound