11import Fastify from 'fastify' ;
22import * as drpy from './libs/drpyS.js' ;
33import path from 'path' ;
4+ import os from "os" ;
45import { fileURLToPath } from 'url' ;
6+ import { readdirSync , readFileSync } from 'fs' ;
57import { base64Decode } from "./libs_drpy/crypto-util.js" ;
8+ import { marked } from './utils/marked.esm.min.js' ;
69
710const fastify = Fastify ( { logger : true } ) ;
811
912const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
1013console . log ( '__dirname:' , __dirname ) ;
1114
15+ // 添加 / 接口
16+ fastify . get ( '/' , async ( request , reply ) => {
17+ const rootDir = path . resolve ( '.' ) ; // 当前根目录
18+ let readmePath = null ;
19+
20+ // 查找根目录下的 README.md 文件(不区分大小写)
21+ const files = readdirSync ( rootDir ) ;
22+ for ( const file of files ) {
23+ if ( / ^ r e a d m e \. m d $ / i. test ( file ) ) {
24+ readmePath = path . join ( rootDir , file ) ;
25+ break ;
26+ }
27+ }
28+
29+ // 如果未找到 README.md 文件
30+ if ( ! readmePath ) {
31+ reply . code ( 404 ) . send ( '<h1>README.md not found</h1>' ) ;
32+ return ;
33+ }
34+
35+ // 读取 README.md 文件内容
36+ const markdownContent = readFileSync ( readmePath , 'utf-8' ) ;
37+
38+ // 将 Markdown 转换为 HTML
39+ const htmlContent = marked ( markdownContent ) ;
40+
41+ // 返回 HTML 内容
42+ reply . type ( 'text/html' ) . send ( `
43+ <!DOCTYPE html>
44+ <html lang="en">
45+ <head>
46+ <meta charset="UTF-8">
47+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
48+ <title>README</title>
49+ </head>
50+ <body>
51+ ${ htmlContent }
52+ </body>
53+ </html>
54+ ` ) ;
55+ } ) ;
56+
1257// 动态加载模块并根据 query 执行不同逻辑
1358fastify . get ( '/api/:module' , async ( request , reply ) => {
1459 const moduleName = request . params . module ;
@@ -34,7 +79,7 @@ fastify.get('/api/:module', async (request, reply) => {
3479 }
3580 }
3681 // 分类逻辑
37- const result = await drpy . cate ( modulePath , query . t , pg , extend ) ;
82+ const result = await drpy . cate ( modulePath , query . t , pg , 1 , extend ) ;
3883 return reply . send ( result ) ;
3984 }
4085
@@ -79,8 +124,29 @@ fastify.get('/api/:module', async (request, reply) => {
79124// 启动服务
80125const start = async ( ) => {
81126 try {
82- await fastify . listen ( 5757 ) ;
83- console . log ( 'Server listening at http://localhost:5757' ) ;
127+ // 监听 0.0.0.0
128+ await fastify . listen ( { port : 5757 , host : '0.0.0.0' } ) ;
129+
130+ // 获取本地地址
131+ const localAddress = `http://localhost:5757` ;
132+
133+ // 获取局域网地址
134+ const interfaces = os . networkInterfaces ( ) ;
135+ let lanAddress = 'Not available' ;
136+ for ( const iface of Object . values ( interfaces ) ) {
137+ if ( ! iface ) continue ;
138+ for ( const config of iface ) {
139+ if ( config . family === 'IPv4' && ! config . internal ) {
140+ lanAddress = `http://${ config . address } :5757` ;
141+ break ;
142+ }
143+ }
144+ }
145+
146+ // 打印服务地址
147+ console . log ( `Server listening at:` ) ;
148+ console . log ( `- Local: ${ localAddress } ` ) ;
149+ console . log ( `- LAN: ${ lanAddress } ` ) ;
84150 } catch ( err ) {
85151 fastify . log . error ( err ) ;
86152 process . exit ( 1 ) ;
0 commit comments