You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.5 KiB
55 lines
1.5 KiB
import ResponseTime from "./ResponseTime"
|
|
import Send from "./Send"
|
|
import { resolve } from "path"
|
|
import { fileURLToPath } from "url"
|
|
import path from "path"
|
|
import ErrorHandler from "./ErrorHandler"
|
|
import { auth } from "./Auth"
|
|
import bodyParser from "koa-bodyparser"
|
|
import Views from "./Views"
|
|
import Session from "./Session"
|
|
import Toast from "./Toast"
|
|
import { autoRegisterControllers } from "@/utils/ForRegister.js"
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
const publicPath = resolve(__dirname, "../../public")
|
|
|
|
export default app => {
|
|
app.use(Toast())
|
|
app.use(ErrorHandler())
|
|
app.use(ResponseTime)
|
|
app.use(Session(app));
|
|
app.use(
|
|
auth({
|
|
whiteList: [
|
|
// 所有请求放行
|
|
{ pattern: "/", auth: false },
|
|
{ pattern: "/**/*", auth: false },
|
|
],
|
|
blackList: [
|
|
// 禁用api请求
|
|
"/api",
|
|
"/api/",
|
|
"/api/**/*",
|
|
],
|
|
})
|
|
)
|
|
app.use(bodyParser())
|
|
app.use(
|
|
Views(resolve(__dirname, "../views"), {
|
|
extension: "pug",
|
|
options: {
|
|
basedir: resolve(__dirname, "../views"),
|
|
}
|
|
})
|
|
)
|
|
autoRegisterControllers(app)
|
|
app.use(async (ctx, next) => {
|
|
try {
|
|
await Send(ctx, ctx.path, { root: publicPath })
|
|
} catch (err) {
|
|
if (err.status !== 404) throw err
|
|
}
|
|
await next()
|
|
})
|
|
}
|
|
|