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.
69 lines
2.0 KiB
69 lines
2.0 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 etag from "@koa/etag"
|
|
import conditional from "koa-conditional-get"
|
|
import { autoRegisterControllers } from "@/utils/ForRegister.js"
|
|
|
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
const publicPath = resolve(__dirname, "../../public")
|
|
|
|
export default app => {
|
|
// 错误处理
|
|
app.use(ErrorHandler())
|
|
// 响应时间
|
|
app.use(ResponseTime)
|
|
// session设置
|
|
app.use(Session(app))
|
|
// 权限设置
|
|
app.use(
|
|
Auth({
|
|
whiteList: [
|
|
// 所有请求放行
|
|
{ pattern: "/", auth: false },
|
|
{ pattern: "/**/*", auth: false },
|
|
],
|
|
blackList: [
|
|
// 禁用api请求
|
|
// "/api",
|
|
// "/api/",
|
|
// "/api/**/*",
|
|
],
|
|
})
|
|
)
|
|
// 视图设置
|
|
app.use(
|
|
Views(resolve(__dirname, "../views"), {
|
|
extension: "pug",
|
|
options: {
|
|
basedir: resolve(__dirname, "../views"),
|
|
},
|
|
})
|
|
)
|
|
// 请求体解析
|
|
app.use(bodyParser())
|
|
// 自动注册控制器
|
|
autoRegisterControllers(app, path.resolve(__dirname, "../controllers"))
|
|
// 注册完成之后静态资源设置
|
|
app.use(async (ctx, next) => {
|
|
if (ctx.body) return await next()
|
|
if (ctx.status === 200) return await next()
|
|
if (ctx.method.toLowerCase() === "get") {
|
|
try {
|
|
await Send(ctx, ctx.path, { root: publicPath, maxAge: 0, immutable: false })
|
|
} catch (err) {
|
|
if (err.status !== 404) throw err
|
|
}
|
|
}
|
|
await next()
|
|
})
|
|
app.use(conditional())
|
|
app.use(etag())
|
|
}
|
|
|