diff --git a/example/.simple-mock.yaml b/example/.simple-mock.yaml index b50302a..53168af 100644 --- a/example/.simple-mock.yaml +++ b/example/.simple-mock.yaml @@ -1,4 +1,3 @@ - root_dir: . api_dir: apis static_dir: static @@ -11,3 +10,10 @@ debug_log_file_path: ./debug.log watch: plugins: swagger: + path: /swagger + documentation: + title: "Test" + +rewrites: + - path: '' + test: ^(index)$ # rewrite index for '' diff --git a/example/apis/api/index.[id].json b/example/apis/api/index.[id].json new file mode 100644 index 0000000..b874021 --- /dev/null +++ b/example/apis/api/index.[id].json @@ -0,0 +1,3 @@ +{ + "asd": "{{params.id}}" +} diff --git a/example/plugins/peer-stream/index.js b/example/plugins/peer-stream/index.js index 7fe4d52..d3c9b82 100644 --- a/example/plugins/peer-stream/index.js +++ b/example/plugins/peer-stream/index.js @@ -1,10 +1,3 @@ -const config = require('./signal.json'); -const fs = require('fs'); -const child_process = require('child_process'); -const G_StartUe5Pool = []; -function initUEe5Pool() { - -} export default function({app, logger, option}) { app.ws('/ws', { message(ws, message) { diff --git a/src/constants/index.ts b/src/constants/index.ts index 2f37283..48bf69f 100644 --- a/src/constants/index.ts +++ b/src/constants/index.ts @@ -31,4 +31,8 @@ debug_log_file_path: ./debug.log watch: plugins: swagger: +rewrites: + - path: '' + test: ^(index)$ ` + diff --git a/src/core/config.ts b/src/core/config.ts index 95d8cc5..7283e99 100644 --- a/src/core/config.ts +++ b/src/core/config.ts @@ -39,7 +39,6 @@ export const cwd = (...p: string[]) => path.resolve(command.getOptionValue('cwd' // E 配置启动命令信息 const parsedConfig = yaml.parse(await safeRun(() => { const configFilePath = cwd(DEFAULT_CONFIG.root_dir, configFile); - if (!fs.existsSync(configFilePath)) { fs.writeFileSync(configFilePath, DEFAULT_MOCK_YAML_CONFIG); return ''; @@ -65,4 +64,5 @@ export const config = mergeDeep(DEFAULT_CONFIG, { static_dir: choice(command.getOptionValue("static-dir"), parsedConfig?.static_dir, DEFAULT_CONFIG.static_dir), static_route_prefix: choice(command.getOptionValue("static-route-prefix"), parsedConfig?.static_route_prefix, DEFAULT_CONFIG.static_route_prefix), plugins: parsedConfig?.plugins ?? {}, + rewrites: parsedConfig?.rewrites ?? {} } as Partial); diff --git a/src/core/startup.ts b/src/core/startup.ts index ba8d0cb..1fe0ea2 100644 --- a/src/core/startup.ts +++ b/src/core/startup.ts @@ -1,6 +1,5 @@ import path from "path"; import fs from "fs"; -import Logger from "@ptkdev/logger"; import {Glob} from "bun"; import Elysia from "elysia"; import {Table} from "console-table-printer"; @@ -16,7 +15,7 @@ import process from "process"; import readline from "readline"; //@ts-ignore import {logger as midLogger} from '@grotto/logysia'; -import {getPlugins, initPlugins} from "./plugin-handler.ts"; +import {initPlugins} from "./plugin-handler.ts"; import {checkAndInit} from "./config.ts"; import {getLogger} from "../utils/logger.ts"; @@ -27,15 +26,15 @@ export async function startup(config, cwd) { // E 相关配置 checkAndInit({resolve, config}); - const plugins = await initPlugins({cwd, config, resolve, logger}); + const plugins = await initPlugins({config, resolve, logger}); // S 打印日志配置 // 遍历路径信息 const glob = new Glob("./**/*.*"); // 启动服务 const app = new Elysia(); - const rewrites = [ - [/^index$/, ''], - ] as [[RegExp, string]]; + const rewrites = config.rewrites.map(({path, test}) => { + return [new RegExp(test), path]; + }) as [[RegExp, string]]; const urlRewrite = (url: string, method: string) => { for (let [reg, final] of rewrites) { @@ -65,17 +64,25 @@ export async function startup(config, cwd) { for await (const file of glob.scan(resolve(config.api_dir))) { let [_, url, method] = [undefined, '', 'get']; const group = path.normalize(path.parse(path.normalize(file)).dir).replaceAll(path.sep, '/'); + const regBackets = /\[([^}]*)\]/g; + + const transformFile = (value: string) => + regBackets.test(value) ? value.replace(regBackets, (_, s) => `:${s}`) : value; + const transformedFile = transformFile(path.basename(file)); + const withMethodParams = /(.*)\.(get|post|patch|head|delete|option|put)\.(.*)$/.exec(transformedFile); + + if (withMethodParams) { + [_, url, method] = withMethodParams; + } else { + const route = transformedFile.split('.'); + url = route.slice(0, route.length - 1).join('/'); + } + app.group(group.replaceAll(".", ""), (app) => { - const withMethodParams = /(.*)\.(get|post|patch|head|delete|option|put)\.(.*)$/.exec(path.basename(file)); - if (withMethodParams) { - [_, url, method] = withMethodParams; - } else { - url = path.basename(file).split('.')[0]; - } const finalUrl = urlRewrite(url, method,); app[method]?.(finalUrl, async (req) => { let res = Bun.file(resolve(path.join(config.api_dir, file))); - logger.debug(JSON.stringify(pick(req, ['cookie', 'user-agent', 'headers', 'body', 'route', 'query', 'content-type']))); + logger.debug(JSON.stringify(pick(req, ['cookie', 'user-agent', 'headers', "params", 'body', 'route', 'query', 'content-type']))); switch (path.extname(file)) { case ".json": return safeRun(async () => Mock.mock(JSON.parse(Mustache.render(JSON.stringify(await res.json()) as string, req))), Promise.resolve(res)); diff --git a/src/presets/plugins/swagger/index.ts b/src/presets/plugins/swagger/index.ts index 4a1dbff..9cad2e5 100644 --- a/src/presets/plugins/swagger/index.ts +++ b/src/presets/plugins/swagger/index.ts @@ -1,5 +1,8 @@ import {swagger} from '@elysiajs/swagger'; -export default function ({app}) { - app.use(swagger); +export default function ({app, option}) { + console.log(option); + app.use(swagger({ + ...(option ?? {}), + })); }