-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathservices-configuration.js
82 lines (77 loc) · 2.88 KB
/
services-configuration.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// @themost-framework 2.0 Codename Blueshift Copyright (c) 2017-2025, THEMOST LP All rights reserved
var ModuleLoaderStrategy = require('@themost/common').ModuleLoaderStrategy;
var _ = require('lodash');
/**
* @interface
* @property {string} serviceType
* @property {string} strategyType
*/
function ServiceConfigurationElement() {
//
}
/**
* @class
* @constructor
*/
function ServicesConfiguration() {
//
}
/**
* Adds application services as they are defined in application configuration services section
* @example
* # config/app.json
* {
* "services": [
* { "serviceType":"./services/my-service#MyService" },
* { "strategyType":"./services/my-service#MyStrategy", "serviceType":"./services/my-service#MyService" }
* ]
* }
*
* @param {HttpApplication} app
*/
ServicesConfiguration.config = function(app) {
/**
* @type {Array<ServiceConfigurationElement>}
*/
var services = app.getConfiguration().getSourceAt('services');
if (_.isArray(services)) {
_.forEach(services,
/**
* @param {ServiceConfigurationElement} x
*/
function(x) {
if (typeof x.serviceType === 'undefined' || x.serviceType === null) {
throw new Error('Invalid configuration. Service type cannot be empty at this context.');
}
var strategyType = x.strategyType || x.serviceType;
var StrategyCtor;
var ServiceCtor;
var typeModule;
var typeCtor;
var hashIndex = strategyType.indexOf('#');
if (hashIndex>-1) {
typeModule = app.getConfiguration().getStrategy(ModuleLoaderStrategy).require(strategyType.substr(0,hashIndex));
typeCtor = strategyType.substr(hashIndex+1,strategyType.length-hashIndex);
StrategyCtor = typeModule[typeCtor];
}
else {
StrategyCtor = app.getConfiguration().getStrategy(ModuleLoaderStrategy).require(strategyType);
}
hashIndex = x.serviceType.indexOf('#');
if (hashIndex>-1) {
typeModule = app.getConfiguration().getStrategy(ModuleLoaderStrategy).require(x.serviceType.substr(0,hashIndex));
typeCtor = x.serviceType.substr(hashIndex+1,x.serviceType.length-hashIndex);
ServiceCtor = typeModule[typeCtor];
}
else {
ServiceCtor = app.getConfiguration().getStrategy(ModuleLoaderStrategy).require(x.serviceType);
}
app.useStrategy(ServiceCtor, StrategyCtor);
});
}
};
if (typeof exports !== 'undefined')
{
module.exports.ServiceConfigurationElement = ServiceConfigurationElement;
module.exports.ServicesConfiguration = ServicesConfiguration;
}