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.
 
 
 
 
 

148 lines
5.1 KiB

"use strict"
import plugins from "@/plugins"
import path from "path"
import { baseDir, templateDir } from "@/util"
import { validateJwt, validateSession } from "./auth"
import Hapi, { Server } from "@hapi/hapi"
import { Sequelize } from "sequelize"
import { Req } from "#/global"
// const Hapi = require("@hapi/hapi");
// const HapiSwagger = require("hapi-swagger");
// const HapiSwagger = require("hapi-swaggered-ui"); // swagger v2版本
const pugPluginAlias = require("pug-alias")
const run = async (): Promise<Server> => {
const server = Hapi.server({
port: 3388,
host: "localhost",
})
await server.register([
{
plugin: require("hapi-sequelizejs"),
options: [
{
name: "data", // identifier
models: [__dirname + "/models/**/*.ts"], // paths/globs to model files
// ignoredModels: [__dirname + "/server/models/**/*.js"], // OPTIONAL: paths/globs to ignore files
sequelize: new Sequelize({
dialect: "sqlite",
storage: path.resolve(__dirname, "./db/data.db"),
logging: false,
// logging: loggerSQL.debug.bind(loggerSQL) // Alternative way to use custom logger, displays all messages
}), // sequelize instance
sync: true, // sync models - default false
forceSync: false, // force sync (drops tables) - default false
},
],
},
])
//===== JWT ===== Start
// await server.register(require("hapi-auth-jwt2"));
// server.auth.strategy("jwt", "jwt", {
// key: process.env.KEY, // Never Share your secret key
// validate: validateJwt, // validate function defined above
// verifyOptions: { algorithms: ["HS256"] },
// });
//===== JWT ===== End
//===== session ===== Start
// https://hapi.dev/module/cookie/api?v=11.0.2
await server.register(require("@hapi/cookie"))
server.auth.strategy("session", "cookie", {
cookie: {
ttl: 1000 * 60 * 60 * 24,
path: "/", // 测试退出时set-cookie失效,加上这个好了
name: "sid", //cookie的名字
password: process.env.KEY,
isSecure: false, // false: 允许 Cookie 通过不安全的连接传输,这会使其受到攻击
},
redirectTo(request: Req) {
if (request.path.startsWith("/api")) {
return false
}
return "/login"
},
appendNext: true,
validateFunc: validateSession,
})
server.auth.default("session")
//===== session ===== End
await server.register(plugins as any)
/**
* 模板引擎
*/
// https://hapi.dev/module/vision/api/?v=6.1.0
await server.register(require("@hapi/vision"))
server.views({
engines: {
ejs: require("ejs"),
pug: require("pug"),
},
isCached: process.env.NODE_ENV === "development" ? false : true,
compileMode: "sync", // ejs
relativeTo: baseDir,
layout: false, // ejs
layoutPath: path.resolve(templateDir, "layout"), // ejs
path: "template",
// pug
compileOptions: {
// By default Pug uses relative paths (e.g. ../root.pug), when using absolute paths (e.g. include /root.pug), basedir is prepended.
// https://pugjs.org/language/includes.html
basedir: templateDir,
plugins: [
pugPluginAlias({
// as Function
"@": fn => fn.replace(/^@/, "template"),
"@hxpath": fn => fn.replace(/^@hxpath/, "template/htmx/path"),
"@views": fn => fn.replace(/^@views/, "template/views"),
}),
],
},
})
// http://localhost:3000/documentation
await server.register([
{
plugin: require("hapi-swagger"),
options: {
documentationPath: "/doc",
info: {
title: "Dream 文档",
version: "1.0.0",
},
grouping: "tags",
tags: [
{
name: "sum",
description: "working with maths",
externalDocs: {
description: "Find out more",
url: "http://example.org",
},
},
{
name: "store",
description: "storing data",
externalDocs: {
description: "Find out more",
url: "http://example.org",
},
},
],
},
},
])
await server.start()
logger.trace("Server running on %s", server.info.uri)
return server
}
process.on("unhandledRejection", err => {
console.log("unhandledRejection:", err)
process.exit(1)
})
export { run }