-
Notifications
You must be signed in to change notification settings - Fork 0
/
angular-oniyi-configurator.js
153 lines (140 loc) · 5.66 KB
/
angular-oniyi-configurator.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
(function() {
'use strict';
function merge(dst) {
angular.forEach(arguments, function(obj) {
if (obj !== dst) {
angular.forEach(obj, function(value, key) {
if (dst[key] && dst[key].constructor && dst[key].constructor === Object) {
extendDeep(dst[key], value);
} else {
dst[key] = value;
}
});
}
});
return dst;
}
angular.module('oniyi.configurator', [])
.provider('configService', function configurationServiceProvider() {
var config = {
rootPath: 'configurations/',
envPath: 'env/',
filePostFix: '.json',
componentNamesSeparator: ',',
useHostnameAsEnvironment: false,
loadEnvConfig: true
},
configCatalog = {},
componentPromises = {};
function _setConfigProperty(name, value) {
if (name && !angular.isUndefined(value)) {
config[name] = value;
return config[name];
}
return false;
}
this.setRootPath = function(value) {
return _setConfigProperty('rootPath', value);
};
this.setEnvPath = function(value) {
return _setConfigProperty('envPath', value);
};
this.setFilePostFix = function(value) {
return _setConfigProperty('filePostFix', value);
};
this.setEnvironment = function(value) {
_setConfigProperty('loadEnvConfig', true);
return _setConfigProperty('environment', value);
};
this.setComponentNamesSeparator = function(value) {
return _setConfigProperty('componentNamesSeparator', value);
};
this.useHostnameAsEnvironment = function() {
_setConfigProperty('loadEnvConfig', true);
return _setConfigProperty('useHostnameAsEnvironment', true);
};
this.disableEnvironmentConfig = function() {
return _setConfigProperty('loadEnvConfig', false);
};
this.$get = function configurationServiceFactory($q, $http, $location, $log) {
function _loadConfiguration(componentName) {
var deferred = $q.defer();
if (angular.isString(componentName)) {
configCatalog[componentName] = configCatalog[componentName] || {};
componentPromises[componentName] = deferred.promise;
$http.get(config.rootPath + componentName + config.filePostFix)
.success(function(mainConfig) {
merge(configCatalog[componentName], mainConfig);
if (config.loadEnvConfig) {
var env = (config.useHostnameAsEnvironment) ? $location.host() : config.environment;
if (angular.isString(env)) {
$http.get(config.rootPath + config.envPath + env + '/' + componentName + config.filePostFix)
.success(function(envConfig) {
merge(configCatalog[componentName], envConfig);
deferred.resolve(configCatalog[componentName]);
})
.error(function(data, status) {
$log.warn('Failed to load environment configuration for component {' + componentName + '}');
$log.debug(status);
deferred.resolve(configCatalog[componentName]);
});
} else {
$log.warn('no environment configured');
deferred.resolve(configCatalog[componentName]);
}
} else {
deferred.resolve(configCatalog[componentName]);
}
})
.error(function(data, status, headers, config) {
$log.error('Failed to load configuration for component {' + componentName + '}');
$log.debug(status);
$log.debug(config);
deferred.reject(status);
});
} else {
deferred.reject('requested component is not valid');
$log.error('invalid component configuration requested');
$log.debug(componentName);
}
return deferred.promise;
}
function _getConfigurations(componentNames) {
var deferred = $q.defer();
if (angular.isString(componentNames)) {
componentNames = componentNames.split(config.componentNamesSeparator);
}
if (angular.isArray(componentNames)) {
var name;
if (componentNames.length > 1) {
var promises = {};
for (var i = 0; i < componentNames.length; i++) {
name = componentNames[i].trim();
promises[name] = (angular.isUndefined(componentPromises[name])) ? _loadConfiguration(name) : componentPromises[name];
}
$q.all(promises).then(function(componentConfigs) {
deferred.resolve(componentConfigs);
}, function(reason) {
$log.debug(reason);
deferred.reject('at least one of the requested config files was not loaded successfully');
});
} else {
name = componentNames[0].trim();
deferred.resolve((angular.isUndefined(componentPromises[name])) ? _loadConfiguration(name) : componentPromises[name]);
}
} else {
deferred.reject('illegal componentNames');
}
return deferred.promise;
}
return {
get: function(componentNames) {
if (angular.isUndefined(componentNames)) {
return $q.defer().resolve(configCatalog);
}
return _getConfigurations(componentNames);
}
};
};
});
})();