-
Notifications
You must be signed in to change notification settings - Fork 159
/
Copy pathconfig.js
64 lines (58 loc) · 2.59 KB
/
config.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
import {readdirSync, readFileSync, writeFileSync, existsSync} from 'fs';
import path from 'path';
// 工具函数:生成 JSON 数据
function generateSiteJSON(jsDir, requestHost) {
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 = `${requestHost}/api/${baseName}`; // 使用请求的 host 地址,避免硬编码端口
return {
key,
name,
type: 4, // 固定值
api,
searchable: 1, // 固定值
filterable: 1, // 固定值
quickSearch: 0, // 固定值
ext: "", // 固定为空字符串
};
});
return {sites};
}
export default (fastify, options, done) => {
fastify.get('/index', async (request, reply) => {
if (!existsSync(options.indexFilePath)) {
reply.code(404).send({error: 'index.json not found'});
return;
}
const content = readFileSync(options.indexFilePath, 'utf-8');
reply.send(JSON.parse(content));
});
// 接口:返回配置 JSON,同时写入 index.json
fastify.get('/config*', async (request, reply) => {
const cfg_path = request.params['*']; // 捕获整个路径
console.log(cfg_path);
try {
// 获取主机名,协议及端口
const protocol = request.protocol; // http 或 https
const hostname = request.hostname; // 主机名,不包含端口
const port = request.socket.localPort; // 获取当前服务的端口
console.log('port:', port);
let requestHost = cfg_path === '/1' ? `${protocol}://${hostname}` : `http://127.0.0.1:${options.PORT}`; // 动态生成根地址
const siteJSON = generateSiteJSON(options.jsDir, requestHost);
const siteStr = JSON.stringify(siteJSON, null, 2);
writeFileSync(options.indexFilePath, siteStr, 'utf8'); // 写入 index.json
if (cfg_path === '/1') {
writeFileSync(options.customFilePath, siteStr, 'utf8'); // 写入 index.json
}
reply.send(siteJSON);
} catch (error) {
reply.status(500).send({error: 'Failed to generate site JSON', details: error.message});
}
});
done();
};