Skip to content

Commit 6169944

Browse files
committed
init:开发了一个基础架构
1 parent 4939ff4 commit 6169944

File tree

8 files changed

+594
-0
lines changed

8 files changed

+594
-0
lines changed

Diff for: README.md

+8
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
# drpy-node
2+
23
nodejs作为服务端的drpy实现。全面升级异步写法
4+
5+
## 基础框架
6+
7+
todo:
8+
9+
1. js里的源能否去除export开头,保持跟qjs一致
10+
2. js里的源,像一级这种异步js,里面调用未定义的函数,能否不通过函数参数传入直接注入调用

Diff for: index.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import Fastify from 'fastify'; // 使用命名导入 Fastify
2+
import path from 'path'; // 使用 Node.js 的 path 模块来处理文件路径
3+
4+
// 使用 pino-pretty 来格式化日志输出
5+
import pino from 'pino';
6+
import pinoPretty from 'pino-pretty';
7+
8+
// 创建 pino-pretty 配置
9+
const prettyPrintOptions = {
10+
colorize: true, // 启用颜色输出
11+
translateTime: 'SYS:standard', // 显示标准时间
12+
ignore: 'pid,hostname' // 忽略 pid 和 hostname
13+
};
14+
15+
// 创建 Fastify 实例
16+
const fastify = Fastify({
17+
logger: pino({
18+
level: 'info', // 设置日志级别
19+
prettyPrint: false // 禁用内置 prettyPrint
20+
}).child({}, {stream: pinoPretty(prettyPrintOptions)}) // 使用 pino-pretty 进行日志格式化
21+
});
22+
23+
// 引入封装好的库
24+
import * as drpy from './libs/drpy.js';
25+
26+
// API 接口: 根据模块名加载相应的 js 文件,并调用 drpy 的方法
27+
fastify.get('/api/:module', async (request, reply) => {
28+
const moduleName = request.params.module; // 获取模块名,例如 '360'
29+
const modulePath = new URL(`./js/${moduleName}.js`, import.meta.url).pathname; // 获取模块路径
30+
31+
try {
32+
// 动态加载 js 文件
33+
const module = await import(modulePath); // 使用动态导入加载模块
34+
console.log(module)
35+
36+
// 由于 `360.js` 使用的是全局的 `rule` 变量,我们通过 `module.default` 获取
37+
const result = await drpy.init(module.rule); // 使用 `module.rule` 直接调用
38+
// 返回结果
39+
return reply.send(result);
40+
} catch (error) {
41+
reply.status(500).send({error: `Failed to load module ${moduleName}: ${error.message}`});
42+
}
43+
});
44+
45+
// 启动服务
46+
const start = async () => {
47+
try {
48+
await fastify.listen(5757);
49+
console.log('Server listening at http://localhost:5757');
50+
} catch (err) {
51+
fastify.log.error(err);
52+
process.exit(1);
53+
}
54+
};
55+
56+
start();

Diff for: js/360.js

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// js/360.js
2+
export var rule = {
3+
title: '标题1',
4+
description: '这是描述',
5+
category: '视频',
6+
一级:async (req)=>{
7+
let html = await req('123');
8+
// console.log(html);
9+
return html
10+
},
11+
};

Diff for: libs/drpy.js

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// drpy.js: 封装模块,包含处理逻辑
2+
3+
import * as utils from '../utils/utils.js'; // 使用 import 引入工具类
4+
const { req } = await import('../utils/req.js');
5+
6+
export async function init(rule) {
7+
// 假设我们传入的 moduleObject 是 js/360.js 中的 rule 对象
8+
const moduleObject = utils.deepCopy(rule)
9+
try {
10+
// 读取并修改传入的对象
11+
if (moduleObject && moduleObject.title) {
12+
moduleObject.title += ' (drpy)'; // 修改 title 属性
13+
}
14+
15+
// 你可以根据需要修改其他属性
16+
if (moduleObject && moduleObject.description) {
17+
moduleObject.description += ' [Modified]';
18+
}
19+
const title = moduleObject.title
20+
console.log(moduleObject)
21+
const titleLength = utils.getTitleLength(title); // 使用 utils.js 中的方法
22+
23+
24+
Object.assign(moduleObject, {
25+
message: `Module initialized with title: ${title}`,
26+
titleLength: titleLength
27+
})
28+
29+
console.log(typeof moduleObject.一级)
30+
if( typeof moduleObject.一级 === 'function'){
31+
let html = await moduleObject.一级(req)
32+
console.log(html)
33+
moduleObject.html = html
34+
}
35+
36+
// 返回修改后的对象
37+
return moduleObject;
38+
} catch (error) {
39+
console.error('Error in drpy.init:', error);
40+
throw new Error('Failed to initialize module');
41+
}
42+
}
43+
44+
// 其他方法可以依照需求继续添加
45+
export async function home() {
46+
return {message: 'Home method'};
47+
}
48+
49+
export async function cate() {
50+
return {message: 'Cate method'};
51+
}
52+
53+
export async function detail() {
54+
return {message: 'Detail method'};
55+
}
56+
57+
export async function play() {
58+
return {message: 'Play method'};
59+
}
60+
61+
export async function search() {
62+
return {message: 'Search method'};
63+
}

Diff for: package.json

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "drpy-node",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"type": "module",
6+
"scripts": {
7+
"dev": "node index.js"
8+
},
9+
"repository": "https://github.com/hjdhnx/drpy-node.git",
10+
"author": "晚风拂柳颜 <[email protected]>",
11+
"license": "MIT",
12+
"dependencies": {
13+
"fastify": "^4.15.0",
14+
"lodash": "^4.17.21",
15+
"pino-pretty": "^13.0.0"
16+
}
17+
}

Diff for: utils/req.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export async function req(param) {
2+
// 模拟异步请求
3+
return new Promise((resolve) => {
4+
setTimeout(() => {
5+
resolve(`Response for ${param}`);
6+
}, 1000);
7+
});
8+
}

Diff for: utils/utils.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// utils.js: 存放工具类方法
2+
import pkg from 'lodash';
3+
4+
const {cloneDeep} = pkg;
5+
6+
export function getTitleLength(title) {
7+
return title.length; // 返回标题长度
8+
}
9+
10+
export const deepCopy = cloneDeep

0 commit comments

Comments
 (0)