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.
71 lines
1.6 KiB
71 lines
1.6 KiB
"use strict";
|
|
import plugins from "@/plugins";
|
|
import { baseDir } from "@/util";
|
|
import validate 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, // validate function defined above
|
|
verifyOptions: { algorithms: ["HS256"] },
|
|
});
|
|
server.auth.default("jwt");
|
|
|
|
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"),
|
|
},
|
|
isCached: process.env.NODE_ENV === "development" ? false : true,
|
|
compileMode: "sync",
|
|
relativeTo: baseDir,
|
|
layout: true,
|
|
path: "template",
|
|
});
|
|
|
|
// http://localhost:3000/documentation
|
|
await server.register([
|
|
{
|
|
plugin: require("hapi-swagger"),
|
|
options: {
|
|
documentationPath: "/doc",
|
|
info: {
|
|
title: "Dream 文档",
|
|
version: "1.0.0",
|
|
},
|
|
},
|
|
},
|
|
]);
|
|
|
|
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 };
|
|
|