-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathindex.js
217 lines (193 loc) · 6.99 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import Fastify from 'fastify';
import * as drpy from './libs/drpyS.js';
import path from 'path';
import os from "os";
import {fileURLToPath} from 'url';
import {readdirSync, readFileSync,writeFileSync,existsSync} from 'fs';
import {base64Decode} from "./libs_drpy/crypto-util.js";
import './utils/marked.min.js';
const fastify = Fastify({logger: true});
const PORT = 5757;
const __dirname = path.dirname(fileURLToPath(import.meta.url));
console.log('__dirname:', __dirname);
// 配置目标目录
const jsDir = path.join(__dirname, 'js');
console.log('jsDir:', jsDir);
const indexFilePath = path.join(__dirname, 'index.json'); // index.json 文件路径
console.log('indexFilePath:', indexFilePath);
// 添加 / 接口
fastify.get('/', async (request, reply) => {
let readmePath = null;
// 查找根目录下的 README.md 文件(不区分大小写)
const files = readdirSync(__dirname);
for (const file of files) {
if (/^readme\.md$/i.test(file)) {
readmePath = path.join(__dirname, file);
break;
}
}
// 如果未找到 README.md 文件
if (!readmePath) {
reply.code(404).send('<h1>README.md not found</h1>');
return;
}
// 读取 README.md 文件内容
const markdownContent = readFileSync(readmePath, 'utf-8');
// 将 Markdown 转换为 HTML
const htmlContent = marked.parse(markdownContent);
// 返回 HTML 内容
reply.type('text/html').send(`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>README</title>
</head>
<body>
${htmlContent}
</body>
</html>
`);
});
// 工具函数:生成 JSON 数据
function generateSiteJSON() {
const files = readdirSync(jsDir);
const sites = files
.filter((file) => file.endsWith('.js') && !file.startsWith('_')) // 筛选出不是 "_" 开头的 .js 文件
.map((file) => {
const baseName = path.basename(file, '.js'); // 去掉文件扩展名
const key = `drpyS_${baseName}`;
const name = `${baseName}(drpyS)`;
const api = `http://127.0.0.1:${PORT}/api/${baseName}`;
return {
key,
name,
type: 4, // 固定值
api,
searchable: 1, // 固定值
ext: "", // 固定为空字符串
};
});
return { sites };
}
// // 定义接口
// fastify.get('/config', async (request, reply) => {
// try {
// const siteJSON = generateSiteJSON();
// reply.send(siteJSON);
// } catch (error) {
// reply.status(500).send({ error: 'Failed to generate site JSON', details: error.message });
// }
// });
// 接口:返回配置 JSON,同时写入 index.json
fastify.get('/config', async (request, reply) => {
try {
const siteJSON = generateSiteJSON();
writeFileSync(indexFilePath, JSON.stringify(siteJSON, null, 2), 'utf8'); // 写入 index.json
reply.send(siteJSON);
} catch (error) {
reply.status(500).send({ error: 'Failed to generate site JSON', details: error.message });
}
});
// 接口:返回 index.json 内容
fastify.get('/index', async (request, reply) => {
try {
if (!existsSync(indexFilePath)) {
return reply.status(404).send({ error: 'index.json file not found' });
}
const indexContent = readFileSync(indexFilePath, 'utf8');
reply.type('application/json').send(JSON.parse(indexContent));
} catch (error) {
reply.status(500).send({ error: 'Failed to read index.json', details: error.message });
}
});
// 动态加载模块并根据 query 执行不同逻辑
fastify.get('/api/:module', async (request, reply) => {
const moduleName = request.params.module;
const query = request.query; // 获取 query 参数
const modulePath = path.join(__dirname, 'js', `${moduleName}.js`);
const pg = Number(query.pg) || 1;
try {
// 根据 query 参数决定执行逻辑
if ('play' in query) {
// 处理播放逻辑
const result = await drpy.play(modulePath, query.flag, query.play);
return reply.send(result);
}
if ('ac' in query && 't' in query) {
let ext = query.ext;
let extend = {};
if (ext) {
try {
extend = JSON.parse(base64Decode(ext))
} catch (e) {
}
}
// 分类逻辑
const result = await drpy.cate(modulePath, query.t, pg, 1, extend);
return reply.send(result);
}
if ('ac' in query && 'ids' in query) {
// 详情逻辑
const result = await drpy.detail(modulePath, query.ids.split(','));
return reply.send(result);
}
if ('wd' in query) {
// 搜索逻辑
if (!('quick' in query)) {
query.quick = 0
}
const result = await drpy.search(modulePath, query.wd, query.quick, pg);
return reply.send(result);
}
if ('refresh' in query) {
// 强制刷新初始化逻辑
const refreshedObject = await drpy.init(modulePath, true);
return reply.send(refreshedObject);
}
if (!('filter' in query)) {
query.filter = 1
}
// 默认逻辑,返回 home + homeVod 接口
const result_home = await drpy.home(modulePath, query.filter);
const result_homeVod = await drpy.homeVod(modulePath);
const result = {
...result_home,
list: result_homeVod
}
reply.send(result);
} catch (error) {
console.error('Error processing request:', error);
reply.status(500).send({error: `Failed to process request for module ${moduleName}: ${error.message}`});
}
});
// 启动服务
const start = async () => {
try {
// 监听 0.0.0.0
await fastify.listen({port: PORT, host: '0.0.0.0'});
// 获取本地地址
const localAddress = `http://localhost:${PORT}`;
// 获取局域网地址
const interfaces = os.networkInterfaces();
let lanAddress = 'Not available';
for (const iface of Object.values(interfaces)) {
if (!iface) continue;
for (const config of iface) {
if (config.family === 'IPv4' && !config.internal) {
lanAddress = `http://${config.address}:${PORT}`;
break;
}
}
}
// 打印服务地址
console.log(`Server listening at:`);
console.log(`- Local: ${localAddress}`);
console.log(`- LAN: ${lanAddress}`);
} catch (err) {
fastify.log.error(err);
process.exit(1);
}
};
start();