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.
103 lines
3.4 KiB
103 lines
3.4 KiB
import { auth, config, method, route, swagger, validate } from "@noderun/hapi-router"
|
|
import { Req, Res, ReturnValue } from "#/global"
|
|
import path from "path"
|
|
import fs from "fs-extra"
|
|
import { baseDir, gFail } from "@/util"
|
|
import MarkdownIt from "markdown-it"
|
|
|
|
export default class Index {
|
|
@auth("try")
|
|
async index(request: Req, h: Res): ReturnValue {
|
|
if (request.auth.isAuthenticated) {
|
|
// 登录了
|
|
} else {
|
|
// 未登录
|
|
}
|
|
return h.view("views/index.pug", { isLogin: request.auth.isAuthenticated })
|
|
}
|
|
|
|
@route("/about")
|
|
@auth("try")
|
|
async about(request: Req, h: Res) {
|
|
const isRenderHtmx = Reflect.has(request.query, "htmx")
|
|
if (!request.auth.isAuthenticated) {
|
|
if (isRenderHtmx) {
|
|
// https://github.com/hapijs/hapi/issues/4443
|
|
// https://github.com/hapijs/hapi/issues/4212
|
|
return h.response().header("HX-Redirect", `/login?next=${encodeURIComponent(request.path)}`)
|
|
}
|
|
return h.redirect(`/login?next=${encodeURIComponent(request.path)}`).takeover()
|
|
}
|
|
const { id } = request.auth.credentials
|
|
|
|
const User = request.getModel("User")
|
|
let res = await User.findOne({ where: { id: id } })
|
|
if (res == null) {
|
|
request.yar.flash("warning", "不存在此用户")
|
|
return h.redirect(`/`).takeover()
|
|
}
|
|
const userinfo = res.toJSON()
|
|
delete userinfo.password
|
|
|
|
const md = new MarkdownIt()
|
|
var result = md.render("# markdown-it rulezz!")
|
|
const data = {
|
|
md: result,
|
|
user: userinfo,
|
|
}
|
|
if (isRenderHtmx) {
|
|
return h.view("htmx/path/about.pug", data)
|
|
}
|
|
return h.view("views/about.pug", data)
|
|
}
|
|
|
|
@route("/docs/{path*}")
|
|
@auth()
|
|
async docs(req: Req, h: Res): ReturnValue {
|
|
// const {id} = req.auth.credentials
|
|
// try {
|
|
// req.cookieAuth.ttl(720 * 24 * 60 * 60 * 1000)
|
|
// req.cookieAuth.set({ id: id });
|
|
// } catch (error) {
|
|
// console.log(error);
|
|
|
|
// }
|
|
if (req.params && req.params.path.endsWith(".md")) {
|
|
// console.log(path.resolve(baseDir, "docs/"+"*.md"));
|
|
// console.log(await glob("docs/"+"*.md"));
|
|
const mdPath = path.resolve(baseDir, "docs/" + req.params.path)
|
|
if (fs.existsSync(mdPath)) {
|
|
const str = fs.readFileSync(mdPath, "utf8")
|
|
console.log("---->", mdPath)
|
|
|
|
return h.view("views/css.pug", {
|
|
content: str.toString(),
|
|
})
|
|
}
|
|
// 解析文档
|
|
return h.view("views/css.pug")
|
|
}
|
|
// 404页面
|
|
return h.redirect("/404")
|
|
}
|
|
@route("/{path*}")
|
|
async any(req: Req, h: Res): ReturnValue {
|
|
console.log("404: ", req.raw.req.url)
|
|
return h.redirect("/404?r=" + encodeURIComponent(req.raw.req.url))
|
|
}
|
|
@route("/404")
|
|
@auth("try")
|
|
async 404(request: Req, h: Res): ReturnValue {
|
|
if (request.auth.isAuthenticated) {
|
|
// 登录了
|
|
} else {
|
|
// 未登录
|
|
}
|
|
if (request.query?.r) {
|
|
// 可重定向返回
|
|
}
|
|
return h.view("404.pug", {
|
|
rollback: request.query?.r,
|
|
})
|
|
}
|
|
}
|
|
|