1- import express from 'express' ;
2- import path from 'path' ;
3- import { fileURLToPath } from 'url' ;
4- import logger from './utils/logger.js' ;
5- import { cors , timeoutSetting , actionTransfer } from './command/middleware.js' ;
6- import restful from './command/restful.js' ;
7- import action from './command/action.js' ;
8- import { ConfirmPort } from './utils/prompt.js' ;
9- import { getIdlePort } from './utils/index.js' ;
10-
11- // 获取当前文件路径(解决 ESM 模块路径问题)
12- const __dirname = path . dirname ( fileURLToPath ( import . meta. url ) ) ;
13-
14- // 静态资源路径
15- const staticPath = path . join ( __dirname , 'public' ) ;
16-
17- // 表单提交处理函数
18- const handleFormSubmission = ( req , res ) => {
19- const { name, email } = req . body ;
20- console . log ( `Received form submission: Name=${ name } , Email=${ email } ` ) ;
21- res . send ( `<h2>Thank you, ${ name } ! Your email is ${ email } </h2>` ) ;
22- process . exit ( 0 ) ; // 停止 CLI 工具
23- } ;
24-
25- // 服务器启动日志
26- const logServerStart = ( port , type ) => {
27- const examples = {
28- action : `curl --location --request POST 'http://localhost:${ port } ' --header 'Content-Type: application/json' --data-raw '{ "Action": "Query" }'` ,
29- restful : `curl --location --request GET 'http://localhost:${ port } /v1/user' --header 'Content-Type: application/json'` ,
30- } ;
31- logger . success ( `Mock API listening on port ${ port } !` ) ;
32- logger . success ( `example: ${ examples [ type ] } ` ) ;
33- } ;
34-
35- // 启动服务器
36- const startServer = ( app , port , type ) => {
37- app . listen ( port , ( ) => logServerStart ( port , type ) ) ;
38- } ;
39-
40- // 模拟服务器
41- const mock = async ( { port, type } ) => {
42- // open port
43- const newPort = await getIdlePort ( port ) ;
44- let confirmPortResult = true ;
45- if ( port !== newPort ) {
46- confirmPortResult = await ConfirmPort ( port , newPort ) ;
47- if ( confirmPortResult ) {
48- port = newPort ;
49- } else {
50- logger . output . error (
51- `The current port is occupied and the service cannot be started. Please close the current port and try again.` ,
52- ) ;
53- process . exit ( 0 ) ;
54- }
55- }
56-
57- // 启动express
58- const app = express ( ) ;
59- const filePath = path . join ( process . cwd ( ) , './mock' ) ;
60-
61- app . use ( express . urlencoded ( { extended : true } ) ) ;
62- app . use ( express . json ( { limit : '50mb' } ) ) ;
63- app . use ( express . static ( staticPath ) ) ;
64-
65- // 渲染 HTML 表单页面
66- app . get ( '/monto/docs' , ( req , res ) => res . sendFile ( path . join ( staticPath , 'index.html' ) ) ) ;
67-
68- // 处理表单提交
69- app . post ( '/submit' , handleFormSubmission ) ;
70-
71- // 通用中间件
72- app . all ( '*' , cors ) ;
73- app . all ( '*' , timeoutSetting ) ;
74- if ( type === 'action' ) {
75- app . all ( "*" , actionTransfer )
76- }
77-
78- // 根据类型加载相应模块
79- const loadModule = type === 'action' ? action : restful ;
80- loadModule ( { app, filePath } ) ;
81-
82- startServer ( app , port , type ) ;
83- } ;
1+ import MockServer from './server.js' ;
2+ import ConfigManager from './utils/config.js' ;
3+
4+ const mock = async ( commandArgs ) => {
5+ const GlobalConfig = new ConfigManager ( ) ;
6+ const config = await GlobalConfig . getConfig ( commandArgs ) ;
7+ const mockServer = new MockServer ( config ) ;
8+ mockServer . start ( ) ;
9+ }
8410
8511export default mock ;
0 commit comments