const { createRouter } = require("gg-unicloud-router");
// const {createRouter} = require('../index');
const path = require("path");
const router = createRouter({
root: path.resolve(__dirname, "./routers"),
permissions: {
//**代表模糊匹配多级目录 *代表模糊匹配当前目录
"test/test": "auth && auth.uid",
"test/test/**": true,
"*": (action, event, context, auth) => {
return false;
},
},
response: (action, data, error) => {
if (error) {
return {
code: error.code || 500,
message: error.message || "服务器内部错误",
};
}
if (action.indexOf("wechat") > -1) {
return data;
}
return {
code: 200,
message: "操作成功",
result: data,
};
},
timers: [
{
name: "test/test",
desc: "name指定子路由名称,desc表示子路由定时器触发的概率100为100%",
weight: 0,
config: "33 33 * * * * *",
},
],
before: [
function (action, event, context, next) {
console.log("前置中间件");
next();
},
],
after: [
function (action, event, context, data, next) {
console.log("后置中间件");
next(data);
},
],
});
setTimeout(async () => {
//模拟http
// console.log(await router.serve({
// httpMethod:"POST",
// queryStringParameters:{
// "$action":"test/test2",
// },
// },{SOURCE:'http'}))
//模拟unicloud客户端
// console.log(await router.serve({
// "$action":"test/test",
// // "timingTriggerConfig": "cron:33 33 * * * *",
// // "timestamp": 1654749213106
// },{}))
//模拟定时器
// console.log(await router.serve({
// "timingTriggerConfig": "cron:33 33 * * * *",
// "timestamp": 1654749213106
// },{}))
//子路由调用子路由
context.callFunction({
name: "xxx/xxx",
data: {},
});
});