|
|
@ -5,9 +5,10 @@ import { baseDir, isDev, publicDir, sourceDir, templateDir } from "@/util" |
|
|
|
import { validateJwt, validateSession } from "./auth" |
|
|
|
import Hapi, { Server } from "@hapi/hapi" |
|
|
|
import { Sequelize } from "sequelize" |
|
|
|
import { Req } from "#/global" |
|
|
|
import { Req, Res } from "#/global" |
|
|
|
import { sequelize } from "./plugins/sequelize" |
|
|
|
import fs from "fs-extra" |
|
|
|
import { Boom } from "@hapi/boom" |
|
|
|
// const Hapi = require("@hapi/hapi");
|
|
|
|
// const HapiSwagger = require("hapi-swagger");
|
|
|
|
// const HapiSwagger = require("hapi-swaggered-ui"); // swagger v2版本
|
|
|
@ -25,16 +26,16 @@ const run = async (): Promise<Server> => { |
|
|
|
server.events.on('request', (request, event, tags) => { |
|
|
|
if (tags.error) { |
|
|
|
loggerSite.error(request.path, "\n", event); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
loggerSite.info(request.path, "\n", event); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
server.events.on('log', (event, tags) => { |
|
|
|
if (tags.error) { |
|
|
|
loggerSite.error(event); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
loggerSite.info(event); |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
await server.register([ |
|
|
|
{ |
|
|
@ -42,7 +43,7 @@ const run = async (): Promise<Server> => { |
|
|
|
options: [ |
|
|
|
{ |
|
|
|
name: "data", // identifier
|
|
|
|
models: isDev?["source/models/**/*.ts"]:["dist/models/**/*.js"], // paths/globs to model files
|
|
|
|
models: isDev ? ["source/models/**/*.ts"] : ["dist/models/**/*.js"], // paths/globs to model files
|
|
|
|
// ignoredModels: [__dirname + "/server/models/**/*.js"], // OPTIONAL: paths/globs to ignore files
|
|
|
|
sequelize: sequelize, // sequelize instance
|
|
|
|
sync: true, // sync models - default false
|
|
|
@ -96,7 +97,7 @@ const run = async (): Promise<Server> => { |
|
|
|
const Pug = require("pug") |
|
|
|
Pug.filters.public = function (text, options) { |
|
|
|
const p = path.resolve(publicDir, text) |
|
|
|
if(fs.pathExistsSync(p)) { |
|
|
|
if (fs.pathExistsSync(p)) { |
|
|
|
return fs.readFileSync(p, "utf-8") |
|
|
|
} |
|
|
|
return "" |
|
|
@ -160,6 +161,17 @@ const run = async (): Promise<Server> => { |
|
|
|
}, |
|
|
|
}, |
|
|
|
]) |
|
|
|
|
|
|
|
server.ext('onPreResponse', (request, h) => { |
|
|
|
const { response } = request |
|
|
|
|
|
|
|
// 如果状态码为403,则调用自定义错误处理程序
|
|
|
|
if (response instanceof Boom && (response.output.statusCode < 200 || response.output.statusCode > 299)) { |
|
|
|
return h.view('403.pug', { error: response}) |
|
|
|
} |
|
|
|
return h.continue; |
|
|
|
}); |
|
|
|
|
|
|
|
await server.start() |
|
|
|
logger.trace("Server running on %s", server.info.uri.replace("0.0.0.0", "localhost")) |
|
|
|
return server |
|
|
|