-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathindex.js
308 lines (272 loc) · 10.3 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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import Fastify from 'fastify';
import fastifyStatic from '@fastify/static';
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);
const docsDir = path.join(__dirname, 'docs');
console.log('docsDir:', docsDir);
// 配置静态文件目录
fastify.register(fastifyStatic, {
root: path.join(__dirname, 'public'), // public 目录的绝对路径
prefix: '/public/', // 可选的 URL 前缀
});
// 添加 / 接口
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>
`);
});
fastify.get('/docs/*', async (request, reply) => {
const fullPath = request.params['*']; // 捕获整个路径
console.log(`Request received for path: ${fullPath}`);
try {
const resolvedPath = path.resolve(docsDir, fullPath); // 将路径解析为绝对路径
// 确保 resolvedPath 在 docsDir 下
if (!resolvedPath.startsWith(docsDir)) {
reply.status(403).send(`<h1>403 Forbidden</h1><p>Access to the requested file is forbidden.</p>`);
return;
}
fastify.log.info(`Resolved path: ${resolvedPath}`);
// 检查文件是否存在
if (!existsSync(resolvedPath)) {
reply.status(404).send(`<h1>404 Not Found</h1><p>File "${fullPath}" not found in /docs.</p>`);
return;
}
// 获取文件扩展名
const ext = path.extname(resolvedPath).toLowerCase();
if (ext === '.md') {
// 处理 Markdown 文件
const markdownContent = readFileSync(resolvedPath, 'utf8');
const htmlContent = marked.parse(markdownContent);
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>${fullPath}</title>
<style>
body { font-family: Arial, sans-serif; line-height: 1.6; margin: 20px; padding: 0; }
h1, h2, h3 { color: #333; }
pre { background: #f4f4f4; padding: 10px; border-radius: 5px; }
code { font-family: monospace; }
</style>
</head>
<body>
${htmlContent}
</body>
</html>
`);
} else {
try {
const mimeType = getMimeType(ext);
if (mimeType.startsWith('text') || mimeType.includes('json') || mimeType.includes('javascript')) {
const fileContent = readFileSync(resolvedPath, 'utf8'); // 确保读取文本内容为 UTF-8
reply.type(mimeType).send(fileContent);
} else {
const fileContent = readFileSync(resolvedPath);
reply.type(mimeType).send(fileContent);
}
} catch (e) {
console.log(e)
}
}
} catch (error) {
reply.status(500).send(`<h1>500 Internal Server Error</h1><p>Error reading or rendering file: ${error.message}</p>`);
}
});
/**
* 根据扩展名返回 MIME 类型
* @param {string} ext 文件扩展名
* @returns {string} MIME 类型
*/
function getMimeType(ext) {
const mimeTypes = {
'.txt': 'text/plain; charset=utf-8',
'.html': 'text/html; charset=utf-8',
'.css': 'text/css; charset=utf-8',
'.js': 'application/javascript; charset=utf-8',
'.json': 'application/json; charset=utf-8',
'.jpg': 'image/jpeg',
'.jpeg': 'image/jpeg',
'.png': 'image/png',
'.gif': 'image/gif',
'.svg': 'image/svg+xml',
'.pdf': 'application/pdf',
};
return mimeTypes[ext] || 'application/octet-stream';
}
// 工具函数:生成 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};
}
// 接口:返回配置 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();