|
@ -1,82 +1,72 @@ |
|
|
// @ts-nocheck
|
|
|
// @ts-nocheck
|
|
|
|
|
|
|
|
|
import { walkDir, removeIndex, isIndexEnd } from "./util"; |
|
|
import { walkDir, removeIndex, isIndexEnd } from "./util" |
|
|
import * as Joi from "joi"; |
|
|
import * as Joi from "joi" |
|
|
const path = require("path"); |
|
|
const path = require("path") |
|
|
const fs = require("fs"); |
|
|
const fs = require("fs") |
|
|
|
|
|
|
|
|
class routePlugin { |
|
|
class routePlugin { |
|
|
public name: string = "routePlugin"; |
|
|
public name: string = "routePlugin" |
|
|
public version: string = "0.0.1"; |
|
|
public version: string = "0.0.1" |
|
|
public register(server: any, opts: any) { |
|
|
public register(server: any, opts: any) { |
|
|
const sourceDir = opts.sourceDir; |
|
|
const sourceDir = opts.sourceDir |
|
|
const type = opts.type || "jwt"; |
|
|
const type = opts.type || "jwt" |
|
|
const auth = opts.auth || []; |
|
|
const auth = opts.auth || [] |
|
|
let array = []; |
|
|
let array = [] |
|
|
for (let i = 0; i < sourceDir.length; i++) { |
|
|
for (let i = 0; i < sourceDir.length; i++) { |
|
|
const dir = sourceDir[i]; |
|
|
const dir = sourceDir[i] |
|
|
console.log(dir); |
|
|
console.log(dir) |
|
|
array.push(dir.dir + "对应路径:"); |
|
|
array.push(dir.dir + "对应路径:") |
|
|
array = array.concat( |
|
|
array = array.concat(this.registerRoute(server, dir.dir, dir.prefix || "", auth, type)) |
|
|
this.registerRoute(server, dir.dir, dir.prefix || "", auth, type) |
|
|
|
|
|
); |
|
|
|
|
|
} |
|
|
|
|
|
fs.writeFileSync( |
|
|
|
|
|
path.resolve(process.cwd(), "route.txt"), |
|
|
|
|
|
array.join("\n"), |
|
|
|
|
|
{ |
|
|
|
|
|
encoding: "utf-8", |
|
|
|
|
|
} |
|
|
} |
|
|
); |
|
|
fs.writeFileSync(path.resolve(process.cwd(), "route.txt"), array.join("\n"), { |
|
|
|
|
|
encoding: "utf-8", |
|
|
|
|
|
}) |
|
|
} |
|
|
} |
|
|
registerRoute(server, sourceDir, prefix, auth, type) { |
|
|
registerRoute(server, sourceDir, prefix, auth, type) { |
|
|
const files = walkDir(sourceDir); |
|
|
const files = walkDir(sourceDir) |
|
|
const routes = []; |
|
|
const routes = [] |
|
|
files.forEach((file) => { |
|
|
files.forEach(file => { |
|
|
let filename = file.relativeFileNoExt; |
|
|
let filename = file.relativeFileNoExt |
|
|
let array = filename.split(path.sep).slice(1); |
|
|
let array = filename.split(path.sep).slice(1) |
|
|
let fileNoExt = removeIndex("/" + array.join("/")); |
|
|
let fileNoExt = removeIndex("/" + array.join("/")) |
|
|
const moduleName = path.resolve(sourceDir, filename); |
|
|
const moduleName = path.resolve(sourceDir, filename) |
|
|
const obj = require(moduleName); |
|
|
const obj = require(moduleName) |
|
|
if (obj.default) { |
|
|
if (obj.default) { |
|
|
const func = new (obj.default || obj)(); |
|
|
const func = new (obj.default || obj)() |
|
|
const prototype = Object.getPrototypeOf(func); |
|
|
const prototype = Object.getPrototypeOf(func) |
|
|
const keys = Reflect.ownKeys(prototype); |
|
|
const keys = Reflect.ownKeys(prototype) |
|
|
for (const key of keys) { |
|
|
for (const key of keys) { |
|
|
if (key !== "constructor") { |
|
|
if (key !== "constructor") { |
|
|
let ff = func[key]; |
|
|
let ff = func[key] |
|
|
let handler: () => void = undefined |
|
|
let handler: () => void = undefined |
|
|
// 默认方法
|
|
|
// 默认方法
|
|
|
const method = ff.$method || "GET"; |
|
|
const method = ff.$method || "GET" |
|
|
// 路由收集规则
|
|
|
// 路由收集规则
|
|
|
let route = ""; |
|
|
let route = "" |
|
|
if (ff.$route) { |
|
|
if (ff.$route) { |
|
|
if (isIndexEnd(fileNoExt)) { |
|
|
if (isIndexEnd(fileNoExt)) { |
|
|
route = ff.$route; |
|
|
route = ff.$route |
|
|
} else { |
|
|
} else { |
|
|
route = fileNoExt + ff.$route; |
|
|
route = fileNoExt + ff.$route |
|
|
} |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
if (isIndexEnd(fileNoExt)) { |
|
|
if (isIndexEnd(fileNoExt)) { |
|
|
route = fileNoExt + key.toString(); |
|
|
route = fileNoExt + key.toString() |
|
|
} else { |
|
|
} else { |
|
|
route = fileNoExt + "/" + key.toString(); |
|
|
route = fileNoExt + "/" + key.toString() |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
route = removeIndex(route); |
|
|
route = removeIndex(route) |
|
|
route = prefix ? route[0] + prefix + "/" + route.slice(1) : route; |
|
|
route = prefix ? route[0] + prefix + "/" + route.slice(1) : route |
|
|
// 配置规则
|
|
|
// 配置规则
|
|
|
const options = ff.$options ? ff.$options : {}; |
|
|
const options = ff.$options ? ff.$options : {} |
|
|
if (!options.auth) { |
|
|
if (!options.auth) { |
|
|
if (ff.$auth == undefined) { |
|
|
if (ff.$auth == undefined) { |
|
|
if ( |
|
|
if (auth && auth.length && auth.filter(v => route.startsWith(v)).length) { |
|
|
auth && |
|
|
options.auth = type |
|
|
auth.length && |
|
|
|
|
|
auth.filter((v) => route.startsWith(v)).length |
|
|
|
|
|
) { |
|
|
|
|
|
options.auth = type; |
|
|
|
|
|
} else { |
|
|
} else { |
|
|
options.auth = false; |
|
|
options.auth = false |
|
|
} |
|
|
} |
|
|
} else if (ff.$auth) { |
|
|
} else if (ff.$auth) { |
|
|
options.auth = |
|
|
options.auth = |
|
@ -85,24 +75,24 @@ class routePlugin { |
|
|
: { |
|
|
: { |
|
|
strategy: type, |
|
|
strategy: type, |
|
|
mode: ff.$auth, |
|
|
mode: ff.$auth, |
|
|
}; |
|
|
} |
|
|
} else { |
|
|
} else { |
|
|
options.auth = false; |
|
|
options.auth = false |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
if (!options.validate) { |
|
|
if (!options.validate) { |
|
|
let validateObj = ff.$validate || {}; |
|
|
let validateObj = ff.$validate || {} |
|
|
if (options.auth && type === "jwt") { |
|
|
if (options.auth && type === "jwt") { |
|
|
if (validateObj.headers) { |
|
|
if (validateObj.headers) { |
|
|
validateObj.headers = validateObj.headers.keys({ |
|
|
validateObj.headers = validateObj.headers.keys({ |
|
|
Authorization: Joi.string(), |
|
|
Authorization: Joi.string(), |
|
|
}); |
|
|
}) |
|
|
} else { |
|
|
} else { |
|
|
validateObj.headers = Joi.object({ |
|
|
validateObj.headers = Joi.object({ |
|
|
headers: Joi.object({ |
|
|
headers: Joi.object({ |
|
|
Authorization: Joi.string(), |
|
|
Authorization: Joi.string(), |
|
|
}).unknown(), // 注意加上这个
|
|
|
}).unknown(), // 注意加上这个
|
|
|
}); |
|
|
}) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
if (validateObj && !!Object.keys(validateObj).length) { |
|
|
if (validateObj && !!Object.keys(validateObj).length) { |
|
@ -117,8 +107,8 @@ class routePlugin { |
|
|
const h = argus[1] |
|
|
const h = argus[1] |
|
|
if (request.logs && !!request.logs.length && errto) { |
|
|
if (request.logs && !!request.logs.length && errto) { |
|
|
// request.yar.flash('error', request.logs.map((v: any)=>v.error.message));
|
|
|
// request.yar.flash('error', request.logs.map((v: any)=>v.error.message));
|
|
|
request.yar.flash('error', failReason); |
|
|
request.yar.flash("error", failReason) |
|
|
return h.redirect(errto); |
|
|
return h.redirect(errto) |
|
|
} |
|
|
} |
|
|
return await ff.call(this, ...argus) |
|
|
return await ff.call(this, ...argus) |
|
|
} |
|
|
} |
|
@ -129,54 +119,43 @@ class routePlugin { |
|
|
if (err.details) { |
|
|
if (err.details) { |
|
|
request.$joi_error = err.details.map(v => v.message) |
|
|
request.$joi_error = err.details.map(v => v.message) |
|
|
} |
|
|
} |
|
|
return h.continue; |
|
|
return h.continue |
|
|
} |
|
|
} |
|
|
handler = async function (...argus) { |
|
|
handler = async function (...argus) { |
|
|
const request = argus[0] |
|
|
const request = argus[0] |
|
|
const h = argus[1] |
|
|
const h = argus[1] |
|
|
if (request.$joi_error) { |
|
|
if (request.$joi_error) { |
|
|
loggerSite.debug('传输参数错误: ', request.$joi_error) |
|
|
loggerSite.debug("传输参数错误: ", request.$joi_error) |
|
|
request.yar.flash('error', failReason); |
|
|
request.yar.flash("error", failReason) |
|
|
delete request.$joi_error |
|
|
delete request.$joi_error |
|
|
return h.redirect(errto); |
|
|
return h.redirect(errto) |
|
|
} |
|
|
} |
|
|
return await ff.call(this, ...argus) |
|
|
return await ff.call(this, ...argus) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
options.validate = validateObj; |
|
|
options.validate = validateObj |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
// && route.startsWith("/api")
|
|
|
// && route.startsWith("/api")
|
|
|
if (ff.$swagger) { |
|
|
if (ff.$swagger) { |
|
|
options.description = ff.$swagger[0]; |
|
|
options.description = ff.$swagger[0] |
|
|
options.notes = ff.$swagger[1]; |
|
|
options.notes = ff.$swagger[1] |
|
|
options.tags = ff.$swagger[2]; |
|
|
options.tags = ff.$swagger[2] |
|
|
} |
|
|
} |
|
|
let str = route; |
|
|
let str = route |
|
|
if ( |
|
|
if ( |
|
|
(typeof options.auth === "string" && options.auth) || |
|
|
(typeof options.auth === "string" && options.auth) || |
|
|
(typeof options.auth === "object" && |
|
|
(typeof options.auth === "object" && options.auth.mode === "required") |
|
|
options.auth.mode === "required") |
|
|
|
|
|
) { |
|
|
|
|
|
str = |
|
|
|
|
|
" 需要权限 : " + " " + full(method) + " " + str; |
|
|
|
|
|
} else if ( |
|
|
|
|
|
typeof options.auth === "object" && |
|
|
|
|
|
options.auth.mode === "optional" |
|
|
|
|
|
) { |
|
|
|
|
|
str = |
|
|
|
|
|
" 不需权限(提供即需验证): " + " " + full(method) + " " + str; |
|
|
|
|
|
} else if ( |
|
|
|
|
|
typeof options.auth === "object" && |
|
|
|
|
|
options.auth.mode === "try" |
|
|
|
|
|
) { |
|
|
) { |
|
|
str = |
|
|
str = " 需要权限 : " + " " + full(method) + " " + str |
|
|
" 不需权限(提供无需验证): " + " " + full(method) + " " + str; |
|
|
} else if (typeof options.auth === "object" && options.auth.mode === "optional") { |
|
|
|
|
|
str = " 不需权限(提供即需验证): " + " " + full(method) + " " + str |
|
|
|
|
|
} else if (typeof options.auth === "object" && options.auth.mode === "try") { |
|
|
|
|
|
str = " 不需权限(提供无需验证): " + " " + full(method) + " " + str |
|
|
} else { |
|
|
} else { |
|
|
str = |
|
|
str = " 不需权限 : " + " " + full(method) + " " + str |
|
|
" 不需权限 : " + " " + full(method) + " " + str; |
|
|
|
|
|
} |
|
|
} |
|
|
routes.push(str); |
|
|
routes.push(str) |
|
|
|
|
|
|
|
|
if (options.validate && options.validate.$errto) { |
|
|
if (options.validate && options.validate.$errto) { |
|
|
delete options.validate.$errto |
|
|
delete options.validate.$errto |
|
@ -189,23 +168,23 @@ class routePlugin { |
|
|
path: route, |
|
|
path: route, |
|
|
handler: handler, |
|
|
handler: handler, |
|
|
options: options, |
|
|
options: options, |
|
|
}); |
|
|
}) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}); |
|
|
}) |
|
|
return routes; |
|
|
return routes |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
function full(str: string, length = 10) { |
|
|
function full(str: string, length = 10) { |
|
|
let len = str.length; |
|
|
let len = str.length |
|
|
let need = length - len; |
|
|
let need = length - len |
|
|
if (need <= 0) return str; |
|
|
if (need <= 0) return str |
|
|
return str + [...Array(need)].map((v, i) => " ").join(""); |
|
|
return str + [...Array(need)].map((v, i) => " ").join("") |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
const plugin = new routePlugin(); |
|
|
const plugin = new routePlugin() |
|
|
|
|
|
|
|
|
export { plugin }; |
|
|
export { plugin } |
|
|
export * from "./util/decorators"; |
|
|
export * from "./util/decorators" |
|
|