From 78873a8eef2ad7e9aabf2d4c18ec37e73f0417b3 Mon Sep 17 00:00:00 2001 From: npmstudy Date: Wed, 15 Nov 2023 12:00:19 +0800 Subject: [PATCH] chore: stash --- .vscode/launch.json | 11 ++++++++++- packages/core/app.ts | 9 ++++----- packages/core/src/fn.ts | 7 +++---- packages/core/src/plugin.ts | 3 ++- packages/core/src/server.ts | 23 +++++++++++++++-------- 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 64188b7..de75022 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -4,6 +4,15 @@ // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ + { + "name": "Attach by Process ID", + "processId": "${command:PickProcess}", + "request": "attach", + "skipFiles": [ + "/**" + ], + "type": "node" + }, { "type": "node", "request": "launch", @@ -21,4 +30,4 @@ ] } ] -} +} \ No newline at end of file diff --git a/packages/core/app.ts b/packages/core/app.ts index 58ccbfd..08ede24 100644 --- a/packages/core/app.ts +++ b/packages/core/app.ts @@ -3,12 +3,11 @@ import { Fn, createServer, Plugable } from './src/index'; // import { RpcServer } from './src/server'; // const rpc = new RpcServer({}); -const rpc = createServer({}); - -const fn = new Fn({ - prefix: ['/api', '/apk'], +const rpc = createServer({ + fn: { + prefix: ['/apk', '/aaa'], + }, }); -const demo = new Demo({}); rpc.fn('/a', function (a) { return { a: a }; diff --git a/packages/core/src/fn.ts b/packages/core/src/fn.ts index ec7614c..4666e43 100644 --- a/packages/core/src/fn.ts +++ b/packages/core/src/fn.ts @@ -8,14 +8,13 @@ export class Fn extends Plugable { constructor(cfg?: any) { super(cfg); - console.dir(this.config); + // console.dir(this.config); this.name = 'Fn'; - if (this.config.prefix === '') { + if (this.prefix === '') { this.prefix = '/api'; } - - console.dir(this.config); + // console.dir(this.config); } fn(key, fn) { // console.dir('=this.config='); diff --git a/packages/core/src/plugin.ts b/packages/core/src/plugin.ts index abc26cc..9437dd5 100644 --- a/packages/core/src/plugin.ts +++ b/packages/core/src/plugin.ts @@ -43,7 +43,8 @@ export class Plugable implements Strategy { this.app = new Koa(); this.init = []; this.load = []; - this.prefix = ''; + this.prefix = this.config.prefix ? this.config.prefix : ''; + this.compose = compose; // TODO: 此处最好改成mount diff --git a/packages/core/src/server.ts b/packages/core/src/server.ts index e8e1e1f..f9e38f0 100644 --- a/packages/core/src/server.ts +++ b/packages/core/src/server.ts @@ -1,5 +1,6 @@ import { bodyParser } from '@koa/bodyparser'; import Koa from 'koa'; +import compose from 'koa-compose'; import mount from 'koa-mount'; import { Strategy, log } from './index'; @@ -145,15 +146,21 @@ export class RpcServer { console.dir('mount plugin ' + plugin.prefix); // console.dir(plugin); // this.app.use(compose([plugin.proxy(), mount(plugin.prefix, plugin.app)])); - - if (Array.isArray(plugin.prefix) === true) { - console.dir('prefix is array' + plugin.prefix); - plugin.prefix.forEach((path) => { - console.dir('mount path' + path); - const i = mount(path, plugin.app); - this.app.use(path, i); - }); + const that = this.app; + if (Array.isArray(plugin.config.prefix) === true) { + console.dir('prefix is array ' + plugin.config.prefix); + + for (const i in plugin.config.prefix) { + const path = plugin.config.prefix[i]; + console.dir('mount path = ' + path); + const mw = mount(path, plugin.app); + console.dir(mw); + // console.dir(this); + this.app.use(mw); + } } else { + console.dir('prefix is string ' + plugin.prefix); + const mw = plugin.prefix === '' ? mount(plugin.app) : mount(plugin.prefix, plugin.app); this.app.use(mw); }