Skip to content

Commit c51384e

Browse files
committed
update:接口生成
1 parent 81f0b83 commit c51384e

File tree

4 files changed

+56
-9
lines changed

4 files changed

+56
-9
lines changed

Diff for: README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
nodejs作为服务端的drpy实现。全面升级异步写法
44

5-
[本地配置接口](/config)
5+
* [本地配置接口-动态](/config)
6+
* [本地配置接口-静态](/index)
67

78
## 更新记录
89

@@ -18,6 +19,7 @@ nodejs作为服务端的drpy实现。全面升级异步写法
1819
8. 打印所有req发出的请求
1920
9. 增加主页的html
2021
10. 番茄小说示例源增加导入模块的用法
22+
11. 更新自动生成配置的接口,自动读取js目录下非_开头的文件视为源
2123

2224
## 基础框架
2325

Diff for: custom.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"sites": [
33
{
4-
"key": "drpyS_番茄小说",
5-
"name": "番茄小说(drpyS)",
4+
"key": "drpyS_番茄小说[书]",
5+
"name": "番茄小说[书](drpyS)",
66
"type": 4,
77
"api": "http://127.0.0.1:5757/api/番茄小说[书]",
88
"searchable": 1,
99
"ext": ""
1010
},
1111
{
12-
"key": "drpyS_金牌影视",
13-
"name": "金牌影视(drpyS)",
12+
"key": "drpyS_金牌影院",
13+
"name": "金牌影院(drpyS)",
1414
"type": 4,
15-
"api": "http://127.0.0.1:5757/api/金牌影视",
15+
"api": "http://127.0.0.1:5757/api/金牌影院",
1616
"searchable": 1,
1717
"ext": ""
1818
}

Diff for: index.js

+28-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as drpy from './libs/drpyS.js';
33
import path from 'path';
44
import os from "os";
55
import {fileURLToPath} from 'url';
6-
import {readdirSync, readFileSync} from 'fs';
6+
import {readdirSync, readFileSync,writeFileSync,existsSync} from 'fs';
77
import {base64Decode} from "./libs_drpy/crypto-util.js";
88
import './utils/marked.min.js';
99

@@ -15,7 +15,8 @@ console.log('__dirname:', __dirname);
1515
// 配置目标目录
1616
const jsDir = path.join(__dirname, 'js');
1717
console.log('jsDir:', jsDir);
18-
18+
const indexFilePath = path.join(__dirname, 'index.json'); // index.json 文件路径
19+
console.log('indexFilePath:', indexFilePath);
1920

2021
// 添加 / 接口
2122
fastify.get('/', async (request, reply) => {
@@ -80,16 +81,40 @@ function generateSiteJSON() {
8081
return { sites };
8182
}
8283

83-
// 定义接口
84+
// // 定义接口
85+
// fastify.get('/config', async (request, reply) => {
86+
// try {
87+
// const siteJSON = generateSiteJSON();
88+
// reply.send(siteJSON);
89+
// } catch (error) {
90+
// reply.status(500).send({ error: 'Failed to generate site JSON', details: error.message });
91+
// }
92+
// });
93+
94+
// 接口:返回配置 JSON,同时写入 index.json
8495
fastify.get('/config', async (request, reply) => {
8596
try {
8697
const siteJSON = generateSiteJSON();
98+
writeFileSync(indexFilePath, JSON.stringify(siteJSON, null, 2), 'utf8'); // 写入 index.json
8799
reply.send(siteJSON);
88100
} catch (error) {
89101
reply.status(500).send({ error: 'Failed to generate site JSON', details: error.message });
90102
}
91103
});
92104

105+
// 接口:返回 index.json 内容
106+
fastify.get('/index', async (request, reply) => {
107+
try {
108+
if (!existsSync(indexFilePath)) {
109+
return reply.status(404).send({ error: 'index.json file not found' });
110+
}
111+
const indexContent = readFileSync(indexFilePath, 'utf8');
112+
reply.type('application/json').send(JSON.parse(indexContent));
113+
} catch (error) {
114+
reply.status(500).send({ error: 'Failed to read index.json', details: error.message });
115+
}
116+
});
117+
93118
// 动态加载模块并根据 query 执行不同逻辑
94119
fastify.get('/api/:module', async (request, reply) => {
95120
const moduleName = request.params.module;

Diff for: index.json

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"sites": [
3+
{
4+
"key": "drpyS_番茄小说[书]",
5+
"name": "番茄小说[书](drpyS)",
6+
"type": 4,
7+
"api": "http://127.0.0.1:5757/api/番茄小说[书]",
8+
"searchable": 1,
9+
"ext": ""
10+
},
11+
{
12+
"key": "drpyS_金牌影院",
13+
"name": "金牌影院(drpyS)",
14+
"type": 4,
15+
"api": "http://127.0.0.1:5757/api/金牌影院",
16+
"searchable": 1,
17+
"ext": ""
18+
}
19+
]
20+
}

0 commit comments

Comments
 (0)