Skip to content

Commit

Permalink
feat(app): allow direct service functions to replace require directories
Browse files Browse the repository at this point in the history
  • Loading branch information
daywiss committed Apr 7, 2020
1 parent 6a9ca0c commit 29baff1
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
5 changes: 4 additions & 1 deletion libs/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ module.exports = async config => {
transports = await Promise.props(transports)

return Promise.mapSeries(compile(config),compiledConfig=>{
console.log('loading',config.name,compiledConfig.require)
console.log('loading',compiledConfig.name)
if(lodash.isFunction(compiledConfig.require)){
return Service(compiledConfig.require,compiledConfig,transports,osConfig)
}
return Service(require.main.require(compiledConfig.require),compiledConfig,transports,osConfig)
})
}
Expand Down
78 changes: 78 additions & 0 deletions test/client-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const test = require('tape')
const App = require('../libs/app')

const child = () => {
return {
error(msg){
throw new Error(msg)
},
echo(msg){
return msg
}
}
}

const index = (config,services) => {
return services
}

const root = (config,services) => {
return {
echo(msg){
return services.index.child.echo(msg)
},
error(msg){
return services.index.child.error(msg)
}
}
}
const config = {
start:['test'],
transports:{
local:{
require:'../transports/local'
}
},
transport:'local',
test:{
start:['index.child','index','root'],
index:{
require:index,
clients:['child'],
child:{
require:child,
},
},
root:{
require:root,
clients:['index'],
}
}
}

test('service proxy passthrough',t=>{
let services
t.test('init',async t=>{
services = await App(config).catch(t.end)
t.ok(services.length)
t.end()
})
t.test('echo',async t=>{
const [child,index,root] = services
// console.log(await index.child.echo('hello'))
const result = await root.echo('hello').catch(t.end)
t.equal(result,'hello')
t.end()
})
t.test('error',async t=>{
const [child,index,root] = services
// console.log(await index.child.echo('hello'))
const result = await root.error('hello').catch(e=>e)
t.equal(result.message,'hello')
t.end()
})
t.test('close',async t=>{
services.forEach(x=>x.utils.shutdown(0))
t.end()
})
})

0 comments on commit 29baff1

Please sign in to comment.