Browse Source

add

theme^2
npmrun 2 years ago
parent
commit
5f462422a3
  1. BIN
      data/data.db
  2. 1
      docs/a.md
  3. 1
      package.json
  4. 3
      pnpm-lock.yaml
  5. 4
      route.txt
  6. 5
      source/log4js_config.ts
  7. 12
      source/plugins/index.ts
  8. 26
      source/run.ts
  9. 2
      source/util/index.ts
  10. 10
      template/403.pug
  11. 2
      template/404.pug
  12. 1
      template/layout/layout.pug
  13. 1
      template/views/register.pug

BIN
data/data.db

Binary file not shown.

1
docs/a.md

@ -0,0 +1 @@
ffff

1
package.json

@ -20,6 +20,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"@hapi/boom": "^10.0.1",
"@hapi/cookie": "^12.0.1",
"@hapi/crumb": "^9.0.1",
"@hapi/hapi": "^21.3.2",

3
pnpm-lock.yaml

@ -6,6 +6,9 @@ patchedDependencies:
path: patches/hapi-sequelizejs@4.6.1.patch
dependencies:
'@hapi/boom':
specifier: ^10.0.1
version: 10.0.1
'@hapi/cookie':
specifier: ^12.0.1
version: 12.0.1

4
route.txt

@ -1,6 +1,4 @@
D:\1XYX\pro\hapi-demo\source\route\htmx对应路径:
不需权限(提供无需验证try): GET /htmx/path/{path*}
D:\1XYX\pro\hapi-demo\source\route\views对应路径:
/home/topuser/Code/@project/hapi-demo/source/route/views对应路径:
不需权限(提供无需验证try): GET /404
不需权限(提供即需验证optional): GET /
不需权限(提供无需验证try): GET /about

5
source/log4js_config.ts

@ -1,15 +1,16 @@
import path from "path"
import { logsDir } from "./util"
export default function () {
return {
appenders: {
file: {
type: "file",
filename: path.resolve(__dirname, "../", "./logs", "./Site.log"),
filename: path.resolve(logsDir, "./Site.log"),
},
SQL: {
type: "file",
filename: path.resolve(__dirname, "../", "./logs", "./SQL.log"),
filename: path.resolve(logsDir, "./SQL.log"),
},
console: {
type: "console",

12
source/plugins/index.ts

@ -1,6 +1,6 @@
import filePlugin from "./file-plugin"
import path from "path"
import { baseDir, sourceDir } from "@/util"
import { logsDir, sourceDir } from "@/util"
import { plugin as routePlugin } from "@noderun/hapi-router"
import { ServerRegisterPluginObject, Plugin, Server, Request, ResponseObject } from "@hapi/hapi"
import Hoek from "@hapi/hoek"
@ -23,7 +23,7 @@ const transport = pino.transport({
target: "pino-pretty",
level: "trace",
options: {
destination: path.resolve(baseDir, "./log/pino.log"),
destination: path.resolve(logsDir, "./pino.log"),
colorize: true,
translateTime: 'HH:MM:ss.l mmmm dS yyyy "UTC"',
mkdir: true,
@ -69,10 +69,10 @@ export default [
// dir: path.resolve(sourceDir, "route/api"),
// prefix: "api"
// },
{
dir: path.resolve(sourceDir, "route/htmx"),
prefix: "htmx",
},
// {
// dir: path.resolve(sourceDir, "route/htmx"),
// prefix: "htmx",
// },
{
dir: path.resolve(sourceDir, "route/views"),
prefix: "",

26
source/run.ts

@ -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

2
source/util/index.ts

@ -6,6 +6,8 @@ export const isProd = process.env.NODE_ENV === "production"
export const baseDir = path.resolve(__dirname, "../../")
export const logsDir = path.resolve(__dirname, "../../logs")
export const sourceDir = isProd ? path.resolve(__dirname, "../../dist") : path.resolve(__dirname, "../../source")
export const publicDir = path.resolve(__dirname, "../../public")
export const uploadDir = path.resolve(publicDir, "upload")

10
template/403.pug

@ -0,0 +1,10 @@
extends layout/layout
block head
link(rel="stylesheet", href="/public/css/views/404.css")
block content
include @/helper/flush.pug
div(style="text-align: center")
span.text404 403
div #{error}

2
template/404.pug

@ -1,10 +1,10 @@
extends layout/layout
include @/helper/flush.pug
block head
link(rel="stylesheet", href="/public/css/views/404.css")
block content
include @/helper/flush.pug
div(style="text-align: center")
span.text404 404
div 重定向回:#{rollback}

1
template/layout/layout.pug

@ -1,5 +1,6 @@
doctype html
include @/helper/helper.pug
include @/helper/flush.pug
block var
html(lang="zh-cn" class=hideHeader?"":"has-navbar-fixed-top")

1
template/views/register.pug

@ -1,5 +1,4 @@
extends @/layout/layout
include @/helper/flush.pug
block var
-title="注册" // 网页标题

Loading…
Cancel
Save