import{a as p}from"./chunk-42XPMRYT.mjs";import{c as r}from"./chunk-WY5IDFPH.mjs";import{bodyParser as a}from"@koa/bodyparser";import l from"koa";import d from"koa-compose";import f from"koa-mount";import c from"koa-compose";var x=c;function y(e){let i=new t(Object.assign({fn:{}},e)),o=new p(Object.assign({},e.fn));return console.dir("createServer"),i.plugin(o),Object.assign(i,{base:".",init:i.config.hooks.init,before:i.config.hooks.before,load:i.config.hooks.load,after:i.config.hooks.after,dump:function(){console.dir("dump")},fn:function(n,s){o.fn(n,s)},add:function(n){o.add(n)}})}var g={hooks:{init:[],before:[],load:[],after:[],default:async(e,i)=>{r("default"),e.body="[core server] no middleware repsonse, please check your route",r("default end")}},before:async e=>{let i=e.app;e.config.hooks.before.forEach(n=>{i.use(n)})},init:async e=>{let i=e.app;e.config.hooks.init.forEach(n=>{i.use(n)})},load:async e=>{let i=e.app;e.config.hooks.load.forEach(n=>{i.use(n)})},after:async e=>{let i=e.app;e.config.hooks.after.forEach(n=>{i.use(n)})}},t=class{constructor(i){this.plugins=[];this.proxy={init:[],load:[],before:{}};this.config=Object.assign(g,i),this.app=new l,this.app.use(a()),this.use=this.app.use}plugin(i){this.plugins.push(i)}mount(){let i={};for(let o of this.plugins){o.server=this,o.serverConfig=this.config,o.name==="base"&&console.error("plugin name \u6CA1\u6709\u4FEE\u6539\uFF0C\u53EF\u80FD\u4F1A\u51FA\u73B0serverConfig\u83B7\u53D6\u95EE\u9898\uFF0C\u8BF7\u5173\u6CE8");for(let n in o.config.bind);this.config[o.name]=o.config,i[o.name]=o}this.app.use(async(o,n)=>{for(let s in i)o[s]=i[s];await n()});for(let o of this.plugins)if(console.dir("init proxy stage"),o.config.proxy&&(o.config.proxy.inject==="init"&&(console.dir("plugin.config.proxy.inject init"),this.proxy.init.push(o.proxy())),o.config.proxy.inject==="load"&&(console.dir("plugin.config.proxy.inject load"),this.proxy.load.push(o.proxy())),o.config.proxy.inject==="before")){console.dir("plugin.config.proxy.inject before");for(let n in o.config.proxy.before){let s=o.config.proxy.before[n];this.proxy.before[s.toLowerCase()]||(this.proxy.before[s.toLowerCase()]=[]),this.proxy.before[s.toLowerCase()].push(o.proxy())}}this.config.hooks.init=this.proxy.init;for(let o of this.plugins)console.dir("init stage"),o.init.length>0&&this.config.hooks.init.push(...o.init);this.config.init(this),this.config.hooks.load=this.proxy.load;for(let o of this.plugins)console.dir("load stage"),o.load.length>0&&this.config.hooks.load.push(...o.load);this.config.load(this);for(let o of this.plugins){console.dir("mount plugin "+o.prefix);let n=this.proxy.before[o.name.toLowerCase()]||[],s=o.prefix===""?f(o.app):f(o.prefix,o.app);console.dir(o.prefix),this.app.use(d([...n,s]))}console.dir("\u515C\u5E95\u7684else"),this.app.use(this.config.hooks.default)}start(i){let o=i||3e3;this.config.before(this),this.mount(),this.config.after(this),this.app.use(function(n,s){n.body="default"}),this.app.listen(o,n=>{n?console.error(n):console.log("@tomrpc/core listening on "+o)})}};export{g as a,t as b,x as c,y as d};
//# sourceMappingURL=chunk-FYAUVABA.mjs.map