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.
95 lines
2.3 KiB
95 lines
2.3 KiB
import {
|
|
auth,
|
|
config,
|
|
method,
|
|
route,
|
|
swagger,
|
|
validate,
|
|
} from "@noderun/hapi-router";
|
|
import { Req, Res, ReturnValue } from "#/global";
|
|
import Joi from "joi";
|
|
import * as bcrypt from "bcrypt";
|
|
import glob from "fast-glob";
|
|
import path from "path";
|
|
import fs from "fs-extra";
|
|
import { baseDir } from "@/util";
|
|
|
|
export default class Index {
|
|
async css(request: Req, h: Res): ReturnValue {
|
|
return h.view("views/css.pug");
|
|
}
|
|
|
|
@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(false)
|
|
async about(request: Req, h) {
|
|
console.log(request.auth);
|
|
console.log(1);
|
|
|
|
try {
|
|
const User = request.getModel("User");
|
|
|
|
console.log(await User.findOne({ where: { username: "xieyaxin" } }));
|
|
} catch (error) {
|
|
console.log(error);
|
|
}
|
|
console.log(2);
|
|
return h.view("views/about.ejs");
|
|
}
|
|
|
|
// @auth()
|
|
@route("/docs/{path*}")
|
|
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")
|
|
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(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");
|
|
}
|
|
}
|
|
|