@@ -3,7 +3,7 @@ import * as drpy from './libs/drpyS.js';
33import path from 'path' ;
44import os from "os" ;
55import { fileURLToPath } from 'url' ;
6- import { readdirSync , readFileSync } from 'fs' ;
6+ import { readdirSync , readFileSync , writeFileSync , existsSync } from 'fs' ;
77import { base64Decode } from "./libs_drpy/crypto-util.js" ;
88import './utils/marked.min.js' ;
99
@@ -15,7 +15,8 @@ console.log('__dirname:', __dirname);
1515// 配置目标目录
1616const jsDir = path . join ( __dirname , 'js' ) ;
1717console . log ( 'jsDir:' , jsDir ) ;
18-
18+ const indexFilePath = path . join ( __dirname , 'index.json' ) ; // index.json 文件路径
19+ console . log ( 'indexFilePath:' , indexFilePath ) ;
1920
2021// 添加 / 接口
2122fastify . 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
8495fastify . 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 执行不同逻辑
94119fastify . get ( '/api/:module' , async ( request , reply ) => {
95120 const moduleName = request . params . module ;
0 commit comments