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.
112 lines
2.8 KiB
112 lines
2.8 KiB
"use strict";
|
|
import plugins from "@/plugins";
|
|
import path from "path";
|
|
import { baseDir,templateDir } from "@/util";
|
|
import { validateJwt, validateSession } from "./validate";
|
|
const Hapi = require("@hapi/hapi");
|
|
// const HapiSwagger = require("hapi-swagger");
|
|
// const HapiSwagger = require("hapi-swaggered-ui"); // swagger v2版本
|
|
|
|
const run = async () => {
|
|
const server = Hapi.server({
|
|
port: 3000,
|
|
host: "localhost",
|
|
});
|
|
|
|
/**
|
|
* jwt
|
|
*/
|
|
// 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"] },
|
|
// });
|
|
/**
|
|
* cookie
|
|
*/
|
|
await server.register(require("@hapi/cookie"));
|
|
server.auth.strategy("session", "cookie", {
|
|
cookie: {
|
|
name: "sid", //cookie的名字
|
|
password: process.env.KEY,
|
|
isSecure: false,
|
|
},
|
|
redirectTo: "/login",
|
|
validateFunc: validateSession,
|
|
});
|
|
server.auth.default("session");
|
|
|
|
await server.register(plugins, {
|
|
routes: {
|
|
// prefix: "/api",
|
|
},
|
|
});
|
|
|
|
/**
|
|
* 模板引擎
|
|
*/
|
|
// 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,
|
|
},
|
|
});
|
|
|
|
// 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();
|
|
console.log("Server running on %s", server.info.uri);
|
|
};
|
|
|
|
process.on("unhandledRejection", (err) => {
|
|
console.log("hh:", err);
|
|
process.exit(1);
|
|
});
|
|
|
|
export { run };
|
|
|