|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +module.exports = function extendServer(api, opts) { |
| 4 | + |
| 5 | + api.assertVersion('>=0.2.0'); |
| 6 | + |
| 7 | + const registerMethods = require('./methods'); |
| 8 | + registerMethods(api); |
| 9 | + |
| 10 | + const requireMicro = require('@micro-app/core'); |
| 11 | + const _ = require('lodash'); |
| 12 | + |
| 13 | + const logger = api.logger; |
| 14 | + |
| 15 | + api.extendConfig('selfServerConfig', { |
| 16 | + cache: true, |
| 17 | + description: '当前工程下的服务配置', |
| 18 | + }, function() { |
| 19 | + return api.self.toServerConfig(true); |
| 20 | + }); |
| 21 | + |
| 22 | + api.extendConfig('microsServerConfig', { |
| 23 | + cache: true, |
| 24 | + description: '当前工程下所有依赖的服务配置合集', |
| 25 | + }, function() { |
| 26 | + const _self = this.self; |
| 27 | + const config = {}; |
| 28 | + const micros = _.cloneDeep([ ...this.micros ]); |
| 29 | + micros.forEach(key => { |
| 30 | + const microConfig = requireMicro(key); |
| 31 | + if (microConfig) { |
| 32 | + config[key] = microConfig.toServerConfig(true); |
| 33 | + } else { |
| 34 | + this.micros.delete(key); |
| 35 | + logger.error(`Not Found micros: "${key}"`); |
| 36 | + } |
| 37 | + }); |
| 38 | + config[_self.key] = api.selfServerConfig || _self.toServerConfig(true); |
| 39 | + return config; |
| 40 | + }); |
| 41 | + |
| 42 | + api.extendConfig('serverConfig', { |
| 43 | + description: '当前工程下服务端配置集合', |
| 44 | + }, function() { |
| 45 | + return api.applyPluginHooks('modifyDefaultServerConfig', {}); |
| 46 | + }); |
| 47 | + |
| 48 | + // merge server config |
| 49 | + api.onInitWillDone(() => { |
| 50 | + const serverMerge = require('../../../src/utils/merge-server'); |
| 51 | + const serverHooksMerge = require('../../../src/utils/merge-server-hooks'); |
| 52 | + api.modifyDefaultServerConfig(_serverConfig => { |
| 53 | + const selfServerConfig = api.selfServerConfig; |
| 54 | + const microsServerConfig = api.microsServerConfig; |
| 55 | + const micros = api.micros; |
| 56 | + const serverEntrys = serverMerge(...micros.map(key => microsServerConfig[key]), selfServerConfig); |
| 57 | + const serverHooks = serverHooksMerge(...micros.map(key => microsServerConfig[key]), selfServerConfig); |
| 58 | + return Object.assign(_serverConfig, { |
| 59 | + ..._.pick(selfServerConfig, [ |
| 60 | + 'host', |
| 61 | + 'port', |
| 62 | + ]), |
| 63 | + entrys: serverEntrys, |
| 64 | + hooks: serverHooks, |
| 65 | + }); |
| 66 | + }); |
| 67 | + }); |
| 68 | +}; |
0 commit comments