|
| 1 | +/* Copyright (c) 2014-2017 Richard Rodger and other contributors, MIT License */ |
| 2 | + |
| 3 | +var MOCK_SEARCH = JSON.parse(process.env.MOCK_SEARCH || 'false') |
| 4 | +var MOCK_INFO = JSON.parse(process.env.MOCK_INFO || 'false') |
| 5 | +var MOCK_SUGGEST = JSON.parse(process.env.MOCK_SUGGEST || 'false') |
| 6 | +var MOCK = MOCK_SEARCH || MOCK_INFO || MOCK_SUGGEST |
| 7 | + |
| 8 | + |
| 9 | +var Seneca = require('seneca') |
| 10 | + |
| 11 | + |
| 12 | +// Build the frontend server using the hapi framework. |
| 13 | +var app = require('../web.js') |
| 14 | + |
| 15 | + |
| 16 | +Seneca({tag: 'web'}) |
| 17 | + .test('print') |
| 18 | + |
| 19 | + .use('seneca-repl', {port:10010}) |
| 20 | + |
| 21 | + .listen(9010) |
| 22 | + |
| 23 | + // Use port numbers for local development. |
| 24 | + .client({pin:'role:search', port:9020}) |
| 25 | + .client({pin:'role:info', port:9030}) |
| 26 | + .client({pin:'role:suggest', port:9060}) |
| 27 | + |
| 28 | + .ready(function(){ |
| 29 | + var server = app({seneca: this}) |
| 30 | + |
| 31 | + this.log.info(server.info) |
| 32 | + }) |
| 33 | + |
| 34 | + |
| 35 | +// Run mock services that this service depends on. |
| 36 | +if (MOCK) { |
| 37 | + var mock = Seneca({tag:'mock'}) |
| 38 | + .test('print') |
| 39 | + |
| 40 | + if (MOCK_SEARCH) { |
| 41 | + mock |
| 42 | + .listen(9020) |
| 43 | + .add('role:search', function (msg, reply) { |
| 44 | + reply({ |
| 45 | + |
| 46 | + // Create fake results using each term of the query. |
| 47 | + items: msg.query.split(/\s+/).map(function (term) { |
| 48 | + return {name:term, version:'1.0.0', desc:term+'!'} |
| 49 | + }) |
| 50 | + }) |
| 51 | + }) |
| 52 | + } |
| 53 | + |
| 54 | + if (MOCK_INFO) { |
| 55 | + mock |
| 56 | + .listen(9030) |
| 57 | + .add('role:info', function (msg, reply) { |
| 58 | + reply({npm:{name:msg.name, version:'1.0.0'}}) |
| 59 | + }) |
| 60 | + } |
| 61 | + |
| 62 | + if (MOCK_SUGGEST) { |
| 63 | + mock |
| 64 | + .listen(9060) |
| 65 | + .add('role:suggest', function (msg, reply) { |
| 66 | + reply(msg.query.split('')) |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
0 commit comments