Skip to content

Commit de6e7a7

Browse files
committed
update:增加首页接口,引入markdown相关库
1 parent 8ed4546 commit de6e7a7

File tree

7 files changed

+2803
-15
lines changed

7 files changed

+2803
-15
lines changed

Diff for: README.md

+5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ nodejs作为服务端的drpy实现。全面升级异步写法
1010
3. 更新atob、btoa函数逻辑
1111
4. 导出pq函数
1212
5. 增加模块系统,$.require和$.exports
13+
6. 修复drpyS源筛选不生效问题
14+
7. 增加局域网可访问接口
15+
8. 打印所有req发出的请求
16+
9. 增加主页的html
17+
10. 番茄小说示例源增加导入模块的用法
1318

1419
## 基础框架
1520

Diff for: index.js

+69-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,59 @@
11
import Fastify from 'fastify';
22
import * as drpy from './libs/drpyS.js';
33
import path from 'path';
4+
import os from "os";
45
import {fileURLToPath} from 'url';
6+
import {readdirSync,readFileSync} from 'fs';
57
import {base64Decode} from "./libs_drpy/crypto-util.js";
8+
import {marked} from './utils/marked.esm.min.js';
69

710
const fastify = Fastify({logger: true});
811

912
const __dirname = path.dirname(fileURLToPath(import.meta.url));
1013
console.log('__dirname:', __dirname);
1114

15+
// 添加 / 接口
16+
fastify.get('/', async (request, reply) => {
17+
const rootDir = path.resolve('.'); // 当前根目录
18+
let readmePath = null;
19+
20+
// 查找根目录下的 README.md 文件(不区分大小写)
21+
const files = readdirSync(rootDir);
22+
for (const file of files) {
23+
if (/^readme\.md$/i.test(file)) {
24+
readmePath = path.join(rootDir, file);
25+
break;
26+
}
27+
}
28+
29+
// 如果未找到 README.md 文件
30+
if (!readmePath) {
31+
reply.code(404).send('<h1>README.md not found</h1>');
32+
return;
33+
}
34+
35+
// 读取 README.md 文件内容
36+
const markdownContent = readFileSync(readmePath, 'utf-8');
37+
38+
// 将 Markdown 转换为 HTML
39+
const htmlContent = marked(markdownContent);
40+
41+
// 返回 HTML 内容
42+
reply.type('text/html').send(`
43+
<!DOCTYPE html>
44+
<html lang="en">
45+
<head>
46+
<meta charset="UTF-8">
47+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
48+
<title>README</title>
49+
</head>
50+
<body>
51+
${htmlContent}
52+
</body>
53+
</html>
54+
`);
55+
});
56+
1257
// 动态加载模块并根据 query 执行不同逻辑
1358
fastify.get('/api/:module', async (request, reply) => {
1459
const moduleName = request.params.module;
@@ -34,7 +79,7 @@ fastify.get('/api/:module', async (request, reply) => {
3479
}
3580
}
3681
// 分类逻辑
37-
const result = await drpy.cate(modulePath, query.t, pg, extend);
82+
const result = await drpy.cate(modulePath, query.t, pg, 1,extend);
3883
return reply.send(result);
3984
}
4085

@@ -79,8 +124,29 @@ fastify.get('/api/:module', async (request, reply) => {
79124
// 启动服务
80125
const start = async () => {
81126
try {
82-
await fastify.listen(5757);
83-
console.log('Server listening at http://localhost:5757');
127+
// 监听 0.0.0.0
128+
await fastify.listen({ port: 5757, host: '0.0.0.0' });
129+
130+
// 获取本地地址
131+
const localAddress = `http://localhost:5757`;
132+
133+
// 获取局域网地址
134+
const interfaces = os.networkInterfaces();
135+
let lanAddress = 'Not available';
136+
for (const iface of Object.values(interfaces)) {
137+
if (!iface) continue;
138+
for (const config of iface) {
139+
if (config.family === 'IPv4' && !config.internal) {
140+
lanAddress = `http://${config.address}:5757`;
141+
break;
142+
}
143+
}
144+
}
145+
146+
// 打印服务地址
147+
console.log(`Server listening at:`);
148+
console.log(`- Local: ${localAddress}`);
149+
console.log(`- LAN: ${lanAddress}`);
84150
} catch (err) {
85151
fastify.log.error(err);
86152
process.exit(1);

Diff for: js/番茄小说[书].js

+3-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
// http://localhost:5757/api/番茄小说[书]?ac=detail&ids=https://fanqienovel.com/page/7431786294105099289
33
// http://localhost:5757/api/番茄小说[书]?wd=斩神&pg=2
44
// http://localhost:5757/api/番茄小说[书]?play=7432172914662720025&flag=番茄小说
5+
6+
const {getRandomFromList} = $.require('./_lib.random.js');
7+
58
var rule = {
69
类型: '小说',
710
title: '番茄小说[书]',
@@ -220,15 +223,6 @@ function decodeText(text, _type) {
220223
return _decodeText2(text);
221224
}
222225

223-
function getRandomFromList(list) {
224-
// 将列表转换为数组
225-
const array = Array.isArray(list) ? list : Array.from(list);
226-
// 获取随机索引
227-
const randomIndex = Math.floor(Math.random() * array.length);
228-
// 返回随机选取的元素
229-
return array[randomIndex];
230-
}
231-
232226
function getFqCookie() {
233227
let cookies = [
234228
'novel_web_id=78444872394737941004',

Diff for: libs/drpyS.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ export async function init(filePath, refresh) {
5151
if (moduleCache.has(filePath) && !refresh) {
5252
const cached = moduleCache.get(filePath);
5353
if (cached.hash === fileHash) {
54-
log(`Module ${filePath} already initialized and unchanged, returning cached instance.`);
54+
// log(`Module ${filePath} already initialized and unchanged, returning cached instance.`);
5555
return cached.moduleObject;
5656
}
5757
}
@@ -485,7 +485,7 @@ async function searchParseAfter(d, pg) {
485485

486486
async function playParse(rule, flag, id, flags) {
487487
let url = id;
488-
log('playParse:', url)
488+
// log('playParse:', url)
489489
if (!/http/.test(url)) {
490490
try {
491491
url = base64Decode(url);
@@ -494,7 +494,7 @@ async function playParse(rule, flag, id, flags) {
494494
}
495495
}
496496
url = decodeURIComponent(url);
497-
log('playParse:', url)
497+
// log('playParse:', url)
498498
return {
499499
TYPE: 'play',
500500
MY_FLAG: flag,

Diff for: libs_drpy/drpyInject.js

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ async function request(url, opt = {}) {
7575
} else {
7676
agent = https.Agent({rejectUnauthorized: false,})
7777
}
78+
console.log(`request:${url} headers:${JSON.stringify(headers)}`);
7879
var resp = await axios(url, {
7980
responseType: respType,
8081
method: opt ? opt.method || 'get' : 'get',

0 commit comments

Comments
 (0)