import filePlugin from "./file-plugin"; import path from "path"; import { baseDir, sourceDir } from "@/util"; import { plugin as routePlugin } from "@noderun/hapi-router"; import {ServerRegisterPluginObject, Plugin, Server, Request, ResponseObject} from "@hapi/hapi" import Hoek from "@hapi/hoek" import HapiYar from "@hapi/yar"; import HapiCrumb from "@hapi/crumb" import { Stream } from "stream"; import HapiPino from "hapi-pino"; const pino = require('pino') const transport = pino.transport({ targets: [ // process.env.NODE_ENV !== 'production' ? { // target: 'pino-pretty', // options: { // colorize: true, // translateTime: 'HH:MM:ss.l mmmm dS yyyy "UTC"' // }, // level: 'info' } : { target: 'pino/file', level: 'info' }, { target: 'pino-pretty', level: 'trace', options: { destination: path.resolve(baseDir, "./log/pino.log"), colorize: true, translateTime: 'HH:MM:ss.l mmmm dS yyyy "UTC"', mkdir: true, // append: false }} // { target: 'pino/file', level: 'trace', options: { // destination: path.resolve(baseDir, "./log/pino.log"), // mkdir: true, // // append: false // }} ] }) export default [ { plugin: HapiPino, options: { logPayload: true, logQueryParams: true, logPathParams: true, logRouteTags: true, // logRequestStart: true, instance: pino(transport), ignoreFunc: (options, request) => { return request.path.startsWith('/public') || request.path.startsWith('/404') }, // stream: fs.createWriteStream(path.resolve(baseDir, "./log/pino.log"), { encoding: "utf-8" }) // prettyPrint: process.env.NODE_ENV !== 'production', // Redact Authorization headers, see https://getpino.io/#/docs/redaction // redact: ['req.headers.cookie'] } }, { plugin: filePlugin as unknown as Plugin, }, { plugin: routePlugin as Plugin, options: { auth: ['/api'], sourceDir: [ // { // dir: path.resolve(sourceDir, "route/api"), // prefix: "api" // }, { dir: path.resolve(sourceDir, "route/htmx"), prefix: "htmx" }, { dir: path.resolve(sourceDir, "route/views"), prefix: "" }, ], type: "session" }, }, { plugin: { name: "flash", version: "0.0.1", // https://github.com/hks-epod/paydash/blob/master/lib/flash.js register: function (server: Server, options) { server.ext('onPreResponse', async function(request: Request, h) { // @ts-ignore if(request.response.variety === "file") return h.continue; // 返回文件时不处理 if(request.path.startsWith("/api") || request.path.startsWith("/htmx")) return h.continue; // 需要设置auth是try或者true才行 const isLogin = request.auth.isAuthenticated; loggerSite.debug(`是否登录:${isLogin}, 请求路径:${request.path}, 请求方法:${request.method}`) // @ts-ignore // console.log(isLogin, request.path, request.response.variety); // let user; // if(isLogin){ // const { id } = request.auth.credentials; // const User = request.getModel("User") // user = await User.findOne({ where: { id: id } }); // user = user.toJSON(); // delete user.password; // } // @ts-ignore if (request.yar && request.yar.flash && request.response.variety === 'view') { var flash = request.yar.flash(); request.yar.set('_flash', {}); // @ts-ignore request.response.source.context = Hoek.applyToDefaults( { flash: !!Object.keys(flash).length?flash:false, isLogin: isLogin, user: isLogin?request.auth.credentials:false, }, // @ts-ignore request.response.source.context ); // @ts-ignore } return h.continue; }); } } as Plugin }, { plugin: HapiYar, options: { name: "yar", storeBlank: false, cookieOptions: { password: "dsRhw1Y5UZqB8SjfClbkrX9PF7yuDMV3JItcW0G4vgpaxONo6mzenHLQET2AiKyPUjjdgjo10amjfghy", path: '/', isSecure: false } } }, { plugin: HapiCrumb, options: { autoGenerate: true, logUnauthorized: true, skip: function(request, reply) { // 流的话直接通过,不需要验证,主要用于传递form-data数据时这里通不过 if(request.payload instanceof Stream){ return true } return false; }, cookieOptions: { path: '/', isSecure: false } } }, ] as unknown as ServerRegisterPluginObject;