@@ -3,7 +3,7 @@ import * as drpy from './libs/drpyS.js';
3
3
import path from 'path' ;
4
4
import os from "os" ;
5
5
import { fileURLToPath } from 'url' ;
6
- import { readdirSync , readFileSync } from 'fs' ;
6
+ import { readdirSync , readFileSync , writeFileSync , existsSync } from 'fs' ;
7
7
import { base64Decode } from "./libs_drpy/crypto-util.js" ;
8
8
import './utils/marked.min.js' ;
9
9
@@ -15,7 +15,8 @@ console.log('__dirname:', __dirname);
15
15
// 配置目标目录
16
16
const jsDir = path . join ( __dirname , 'js' ) ;
17
17
console . log ( 'jsDir:' , jsDir ) ;
18
-
18
+ const indexFilePath = path . join ( __dirname , 'index.json' ) ; // index.json 文件路径
19
+ console . log ( 'indexFilePath:' , indexFilePath ) ;
19
20
20
21
// 添加 / 接口
21
22
fastify . get ( '/' , async ( request , reply ) => {
@@ -80,16 +81,40 @@ function generateSiteJSON() {
80
81
return { sites } ;
81
82
}
82
83
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
84
95
fastify . get ( '/config' , async ( request , reply ) => {
85
96
try {
86
97
const siteJSON = generateSiteJSON ( ) ;
98
+ writeFileSync ( indexFilePath , JSON . stringify ( siteJSON , null , 2 ) , 'utf8' ) ; // 写入 index.json
87
99
reply . send ( siteJSON ) ;
88
100
} catch ( error ) {
89
101
reply . status ( 500 ) . send ( { error : 'Failed to generate site JSON' , details : error . message } ) ;
90
102
}
91
103
} ) ;
92
104
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
+
93
118
// 动态加载模块并根据 query 执行不同逻辑
94
119
fastify . get ( '/api/:module' , async ( request , reply ) => {
95
120
const moduleName = request . params . module ;
0 commit comments