Skip to content

Commit

Permalink
chore: refact
Browse files Browse the repository at this point in the history
  • Loading branch information
npmstudy committed Nov 1, 2023
1 parent 9ad6873 commit 739c4f0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
7 changes: 6 additions & 1 deletion packages/core2/src/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,26 @@ export interface Strategy {
init: any[];
load: any[];
prefix;
name;
app;
compose;
proxy;
server;
serverConfig;
}

export default class Plugable implements Strategy {
public init: any[] = [];
public load: any[] = [];
public name;
public app;
public server;
public serverConfig;
public prefix;
public compose;

constructor() {
this.name = 'fn';
this.name = 'base';
this.app = new Koa();
this.init = [];
this.load = [];
Expand Down
24 changes: 8 additions & 16 deletions packages/core2/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,6 @@ export const LifeCycleConfig = {
app.use(mw);
});
},
beforeMount: async (server) => {
const app = server.app;
const loadMiddlewares = server.config.hooks.beforeMount;
loadMiddlewares.forEach((mw) => {
app.use(mw);
});
},
afterMount: async (server) => {
const app = server.app;
const loadMiddlewares = server.config.hooks.afterMount;
loadMiddlewares.forEach((mw) => {
app.use(mw);
});
},
beforeAll: async (ctx, next) => {
log('beforeAll');
await next();
Expand Down Expand Up @@ -127,6 +113,12 @@ export default class RpcServer {
console.dir('mount');
// hooks
for (const plugin of this.plugins) {
plugin.server = this;
plugin.serverConfig = this.config;

// set config namespace
if (plugin.name !== 'base') this.config.plugin[plugin.name] = {};

if (plugin.init.length > 0) this.config.hooks.init.push(...plugin.init);

// console.dir(plugin);
Expand Down Expand Up @@ -170,13 +162,13 @@ export default class RpcServer {
const _port = port || 3000;
console.dir(_port);

// this.config.load(this);
this.config.before(this);

// this.config.beforeMount(this);
this.mount();

// this.config.afterMount(this);
// this.config.after(this);
this.config.after(this);
// this.app.use(this.config.afterAll);

this.app.listen(_port, (err) => {
Expand Down

0 comments on commit 739c4f0

Please sign in to comment.