Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 436ff0d

Browse files
committed
Move frontend configs to backend.
1 parent c2a79b6 commit 436ff0d

File tree

11 files changed

+154
-62
lines changed

11 files changed

+154
-62
lines changed

src/config.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,108 @@ module.exports = {
6868
},
6969
},
7070
};
71+
72+
const frontendConfigs = {
73+
"local":{
74+
"JWT_V3_NAME":"v3jwt",
75+
"JWT_V2_NAME":"tcjwt",
76+
"COOKIES_SECURE":false,
77+
"TC_LOGIN_URL": "https://accounts.topcoder-dev.com/member",
78+
"TC_USER_PROFILE_URL": "http://api.topcoder-dev.com/v2/user/profile",
79+
"API_URL": "https://127.0.0.1:8443",
80+
"ADMIN_TOOL_URL": "http://localhost:8080/api/v2",
81+
"ACCOUNTS_CONNECTOR_URL": "https://accounts.topcoder-dev.com/connector.html",
82+
"DIRECT_URL_BASE": "https://www.topcoder-dev/direct/projectOverview?formData.projectId=",
83+
"OWNER_LOGIN_GITHUB_URL":"/api/v1/github/owneruser/login",
84+
"OWNER_LOGIN_GITLAB_URL":"/api/v1/gitlab/owneruser/login",
85+
"TOPCODER_URL": "https://topcoder-dev.com",
86+
"GITHUB_TEAM_URL": "https://github.com/orgs/",
87+
"GITLAB_GROUP_URL": "https://gitlab.com/groups/"
88+
89+
},
90+
"heroku":{
91+
"JWT_V3_NAME":"v3jwt",
92+
"JWT_V2_NAME":"tcjwt",
93+
"COOKIES_SECURE":false,
94+
"TC_LOGIN_URL": "https://accounts.topcoder-dev.com/member",
95+
"TC_USER_PROFILE_URL": "https://api.topcoder-dev.com/v2/user/profile",
96+
"API_URL": "https://api.topcoder-dev.com",
97+
"ADMIN_TOOL_URL": "https://api.topcoder-dev.com/v2",
98+
"ACCOUNTS_CONNECTOR_URL": "https://accounts.topcoder-dev.com/connector.html",
99+
"DIRECT_URL_BASE": "https://www.topcoder-dev.com/direct/projectOverview?formData.projectId=",
100+
"OWNER_LOGIN_GITHUB_URL":"/api/v1/github/owneruser/login",
101+
"OWNER_LOGIN_GITLAB_URL":"/api/v1/gitlab/owneruser/login",
102+
"TOPCODER_URL": "https://topcoder-dev.com",
103+
"GITHUB_TEAM_URL": "https://github.com/orgs/",
104+
"GITLAB_GROUP_URL": "https://gitlab.com/groups/"
105+
},
106+
"dev":{
107+
"JWT_V3_NAME":"v3jwt",
108+
"JWT_V2_NAME":"tcjwt",
109+
"COOKIES_SECURE":false,
110+
"TC_LOGIN_URL": "https://accounts.topcoder-dev.com/member",
111+
"TC_USER_PROFILE_URL": "https://api.topcoder-dev.com/v2/user/profile",
112+
"API_URL": "https://api.topcoder-dev.com",
113+
"ADMIN_TOOL_URL": "https://api.topcoder-dev.com/v2",
114+
"ACCOUNTS_CONNECTOR_URL": "https://accounts.topcoder-dev.com/connector.html",
115+
"DIRECT_URL_BASE": "https://www.topcoder-dev.com/direct/projectOverview?formData.projectId=",
116+
"OWNER_LOGIN_GITHUB_URL":"/api/v1/github/owneruser/login",
117+
"OWNER_LOGIN_GITLAB_URL":"/api/v1/gitlab/owneruser/login",
118+
"TOPCODER_URL": "https://topcoder-dev.com",
119+
"GITHUB_TEAM_URL": "https://github.com/orgs/",
120+
"GITLAB_GROUP_URL": "https://gitlab.com/groups/"
121+
},
122+
"qa":{
123+
"JWT_V3_NAME":"v3jwt",
124+
"JWT_V2_NAME":"tcjwt",
125+
"COOKIES_SECURE":false,
126+
"TC_LOGIN_URL": "https://accounts.topcoder-dev.com/member",
127+
"TC_USER_PROFILE_URL": "https://api.topcoder-dev.com/v2/user/profile",
128+
"API_URL": "https://api.topcoder-qa.com",
129+
"ADMIN_TOOL_URL": "https://api.topcoder-qa.com/v2",
130+
"ACCOUNTS_CONNECTOR_URL": "https://accounts.topcoder-qa.com/connector.html",
131+
"DIRECT_URL_BASE": "https://www.topcoder-dev.com/direct/projectOverview?formData.projectId=",
132+
"OWNER_LOGIN_GITHUB_URL":"/api/v1/github/owneruser/login",
133+
"OWNER_LOGIN_GITLAB_URL":"/api/v1/gitlab/owneruser/login",
134+
"TOPCODER_URL": "https://topcoder-dev.com",
135+
"GITHUB_TEAM_URL": "https://github.com/orgs/",
136+
"GITLAB_GROUP_URL": "https://gitlab.com/groups/"
137+
},
138+
"prod":{
139+
"JWT_V3_NAME":"v3jwt",
140+
"JWT_V2_NAME":"tcjwt",
141+
"COOKIES_SECURE":false,
142+
"TC_LOGIN_URL": "https://accounts.topcoder.com/member",
143+
"TC_USER_PROFILE_URL": "https://api.topcoder.com/v2/user/profile",
144+
"API_URL": "https://api.topcoder.com",
145+
"ADMIN_TOOL_URL": "https://api.topcoder.com/v2",
146+
"ACCOUNTS_CONNECTOR_URL": "https://accounts.topcoder.com/connector.html",
147+
"DIRECT_URL_BASE": "https://www.topcoder.com/direct/projectOverview?formData.projectId=",
148+
"OWNER_LOGIN_GITHUB_URL":"/api/v1/github/owneruser/login",
149+
"OWNER_LOGIN_GITLAB_URL":"/api/v1/gitlab/owneruser/login",
150+
"TOPCODER_URL": "https://topcoder-dev.com",
151+
"GITHUB_TEAM_URL": "https://github.com/orgs/",
152+
"GITLAB_GROUP_URL": "https://gitlab.com/groups/"
153+
}
154+
};
155+
156+
const activeEnv = module.exports.TOPCODER_ENV;
157+
module.exports.frontendConfigs = {
158+
helpLink: module.exports.HELP_LINK,
159+
copilotRole: module.exports.COPILOT_ROLE,
160+
administratorRoles: module.exports.ADMINISTRATOR_ROLES,
161+
JWT_V3_NAME: process.env.JWT_V3_NAME || frontendConfigs[activeEnv].JWT_V3_NAME,
162+
JWT_V2_NAME: process.env.JWT_V2_NAME || frontendConfigs[activeEnv].JWT_V2_NAME,
163+
COOKIES_SECURE: process.env.COOKIES_SECURE || frontendConfigs[activeEnv].COOKIES_SECURE,
164+
TC_LOGIN_URL: process.env.TC_LOGIN_URL || frontendConfigs[activeEnv].TC_LOGIN_URL,
165+
TC_USER_PROFILE_URL: process.env.TC_USER_PROFILE_URL || frontendConfigs[activeEnv].TC_USER_PROFILE_URL,
166+
API_URL: process.env.API_URL || frontendConfigs[activeEnv].API_URL,
167+
ADMIN_TOOL_URL: process.env.ADMIN_TOOL_URL || frontendConfigs[activeEnv].ADMIN_TOOL_URL,
168+
ACCOUNTS_CONNECTOR_URL: process.env.ACCOUNTS_CONNECTOR_URL || frontendConfigs[activeEnv].ACCOUNTS_CONNECTOR_URL,
169+
DIRECT_URL_BASE: process.env.DIRECT_URL_BASE || frontendConfigs[activeEnv].DIRECT_URL_BASE,
170+
OWNER_LOGIN_GITHUB_URL: process.env.OWNER_LOGIN_GITHUB_URL || frontendConfigs[activeEnv].OWNER_LOGIN_GITHUB_URL,
171+
OWNER_LOGIN_GITLAB_URL: process.env.OWNER_LOGIN_GITLAB_URL || frontendConfigs[activeEnv].OWNER_LOGIN_GITLAB_URL,
172+
TOPCODER_URL: process.env.TOPCODER_URL || frontendConfigs[activeEnv].TOPCODER_URL,
173+
GITHUB_TEAM_URL: process.env.GITHUB_TEAM_URL || frontendConfigs[activeEnv].GITHUB_TEAM_URL,
174+
GITLAB_GROUP_URL: process.env.GITLAB_GROUP_URL || frontendConfigs[activeEnv].GITLAB_GROUP_URL
175+
};

src/front/src/app/app.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ angular.module('topcoderX', [
5252
});
5353
}],
5454
currentUser: ['AuthService', function (AuthService) {
55-
return AuthService.getCurrentUser();
55+
return AuthService.getAppConfig().then(function () {
56+
return AuthService.getCurrentUser();
57+
});
5658
}],
5759

5860
}
@@ -63,11 +65,10 @@ angular.module('topcoderX', [
6365
templateUrl: 'components/common/content.html',
6466
resolve: {
6567
currentUser: ['AuthService', function (AuthService) {
66-
return AuthService.getCurrentUser();
67-
}],
68-
AppConfig: ['AuthService', function (AuthService) {
69-
return AuthService.getAppConfig();
70-
}],
68+
return AuthService.getAppConfig().then(function () {
69+
return AuthService.getCurrentUser();
70+
});
71+
}]
7172
},
7273
})
7374
.state('app.main', {

src/front/src/app/auth/auth.config.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,9 @@ angular.module('topcoderX')
3131
jwtInterceptorProvider.tokenGetter = [
3232
'AuthService', '$http', 'Helper', '$rootScope', 'config',
3333
function (AuthService, $http, Helper, $rootScope, config) {
34+
if (!$rootScope.appConfig) return;
3435
// token V2 for API V2
35-
if (config.url.indexOf(Helper.config().ADMIN_TOOL_URL) > -1) {
36+
if (config.url.indexOf($rootScope.appConfig.ADMIN_TOOL_URL) > -1) {
3637
if (AuthService.getTokenV2()) {
3738
return AuthService.getTokenV2();
3839
}
@@ -45,7 +46,7 @@ angular.module('topcoderX')
4546
if (refreshingToken === null) {
4647
refreshingToken = $http({
4748
method: 'GET',
48-
url: Helper.config().API_URL + "/v3/authorizations/1",
49+
url: $rootScope.appConfig.API_URL + "/v3/authorizations/1",
4950
headers: {
5051
'Authorization': "Bearer " + currentToken
5152
}

src/front/src/app/auth/auth.service.js

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22

33
angular.module('topcoderX')
44
.factory('AuthService', [
5-
'$q', '$log', 'jwtHelper', '$cookies', '$window', '$state', '$rootScope', '$http',
6-
'COOKIES_SECURE', 'JWT_V3_NAME', 'JWT_V2_NAME', 'Helper',
7-
function ($q, $log, jwtHelper, $cookies, $window, $state, $rootScope, $http,
8-
COOKIES_SECURE, JWT_V3_NAME, JWT_V2_NAME, Helper) {
5+
'$q', '$log', 'jwtHelper', '$cookies', '$window', '$state', '$rootScope', '$http', 'Helper',
6+
function ($q, $log, jwtHelper, $cookies, $window, $state, $rootScope, $http, Helper) {
97
// these constants are for AuthService internal usage only
108
// they don't depend on the environment thus don't have to be placed in global config
9+
1110
var GET_FRESH_TOKEN_REQUEST = 'GET_FRESH_TOKEN_REQUEST';
1211
var GET_FRESH_TOKEN_SUCCESS = 'GET_FRESH_TOKEN_SUCCESS';
1312
var GET_FRESH_TOKEN_FAILURE = 'GET_FRESH_TOKEN_FAILURE';
@@ -153,14 +152,14 @@ angular.module('topcoderX')
153152
AuthService.logginOut = null;
154153
// remove only token V3, which we set from the script manually
155154
// token V2 will be removed automatically during logout server request
156-
$cookies.remove(JWT_V3_NAME, { path: '/' });
155+
$cookies.remove($rootScope.appConfig.JWT_V3_NAME, { path: '/' });
157156
});
158157

159158
return AuthService.logginOut;
160159
}
161160

162161
AuthService.login = function () {
163-
$window.location.href = Helper.config().TC_LOGIN_URL + '?retUrl=' + encodeURIComponent($window.location.href);
162+
$window.location.href = $rootScope.appConfig.TC_LOGIN_URL + '?retUrl=' + encodeURIComponent($window.location.href);
164163
}
165164

166165
/**
@@ -170,7 +169,7 @@ angular.module('topcoderX')
170169
AuthService.init = function () {
171170
// add hidden iframe which is used to get API v3 token
172171
configureConnector({
173-
connectorUrl: Helper.config().ACCOUNTS_CONNECTOR_URL,
172+
connectorUrl: $rootScope.appConfig ? $rootScope.appConfig.ACCOUNTS_CONNECTOR_URL : null,
174173
frameId: 'tc-accounts-iframe',
175174
});
176175
}
@@ -219,15 +218,15 @@ angular.module('topcoderX')
219218
* @return {String} token v3
220219
*/
221220
AuthService.getTokenV3 = function () {
222-
return $cookies.get(JWT_V3_NAME);
221+
return $cookies.get($rootScope.appConfig.JWT_V3_NAME);
223222
}
224223

225224
/**
226225
* Save token V3 to cookies
227226
*/
228227
AuthService.setTokenV3 = function (token) {
229-
return $cookies.put(JWT_V3_NAME, token, {
230-
secure: COOKIES_SECURE,
228+
return $cookies.put($rootScope.appConfig.JWT_V3_NAME, token, {
229+
secure: $rootScope.appConfig.COOKIES_SECURE,
231230
});
232231
}
233232

@@ -246,7 +245,7 @@ angular.module('topcoderX')
246245
* @return {String} token v2
247246
*/
248247
AuthService.getTokenV2 = function () {
249-
return $cookies.get(JWT_V2_NAME);
248+
return $cookies.get($rootScope.appConfig.JWT_V2_NAME);
250249
}
251250

252251
/**
@@ -282,17 +281,20 @@ angular.module('topcoderX')
282281
* gets the application configurations
283282
*/
284283
AuthService.getAppConfig = function () {
285-
var tctV3 = AuthService.getTokenV3();
286-
287-
if (!tctV3) {
288-
return null;
284+
if ($rootScope.appConfig) {
285+
return $q.resolve($rootScope.appConfig);
289286
}
290-
return $http.get(Helper.baseUrl + '/api/v1/appConfig').then(function (res) {
291-
$rootScope.appConfig = res.data;
292-
return $q.resolve(res.data);
293-
}).catch(function (err) {
294-
return $q.reject(err);
295-
});
287+
return $http.get(Helper.baseUrl + '/api/v1/appConfig')
288+
.then(function (res) {
289+
$rootScope.appConfig = res.data;
290+
if (connectorIFrame && !connectorIFrame.src) {
291+
connectorIFrame.src = $rootScope.appConfig.ACCOUNTS_CONNECTOR_URL;
292+
url = $rootScope.appConfig.ACCOUNTS_CONNECTOR_URL;
293+
}
294+
return $q.resolve(res.data);
295+
}).catch(function (err) {
296+
return $q.reject(err);
297+
});
296298
};
297299

298300
return AuthService;

src/front/src/app/helper.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,13 @@
66
'use strict';
77

88
angular.module('topcoderX')
9-
.factory('Helper', ['$rootScope', '$location', 'TC_LOGIN_URL', 'TC_USER_PROFILE_URL', 'API_URL', 'ADMIN_TOOL_URL', 'ACCOUNTS_CONNECTOR_URL', 'DIRECT_URL_BASE', function ($rootScope, $location, TC_LOGIN_URL, TC_USER_PROFILE_URL, API_URL, ADMIN_TOOL_URL, ACCOUNTS_CONNECTOR_URL, DIRECT_URL_BASE) {
9+
.factory('Helper', ['$rootScope', '$location', function ($rootScope, $location) {
1010
var baseUrl = $location.protocol() + '://' + $location.host();
1111

1212
var service = {};
1313

1414
service.baseUrl = baseUrl;
1515

16-
/**
17-
* gets the config based on host env
18-
*/
19-
service.config = function () {
20-
return {
21-
TC_LOGIN_URL: TC_LOGIN_URL, // eslint-disable-line object-shorthand
22-
TC_USER_PROFILE_URL: TC_USER_PROFILE_URL, // eslint-disable-line object-shorthand
23-
API_URL: API_URL, // eslint-disable-line object-shorthand
24-
ADMIN_TOOL_URL: ADMIN_TOOL_URL, // eslint-disable-line object-shorthand
25-
ACCOUNTS_CONNECTOR_URL: ACCOUNTS_CONNECTOR_URL, // eslint-disable-line object-shorthand
26-
DIRECT_URL_BASE: DIRECT_URL_BASE, // eslint-disable-line object-shorthand
27-
};
28-
};
29-
3016
service.isAdminUser = function (currentUser) {
3117
var userRoles = currentUser.roles.map(function (x) {
3218
return x.toUpperCase();

src/front/src/app/members/member.controller.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict';
22

33
angular.module('topcoderX')
4-
.controller('MemberController', ['$scope', '$stateParams', 'GITHUB_TEAM_URL', 'GITLAB_GROUP_URL', function ($scope, $stateParams, GITHUB_TEAM_URL, GITLAB_GROUP_URL) {
4+
.controller('MemberController', ['$scope', '$rootScope', '$stateParams', function ($scope, $rootScope, $stateParams) {
55
$scope.title = 'Members';
66
$scope.provider = $stateParams.provider;
77

@@ -10,9 +10,9 @@ angular.module('topcoderX')
1010
const params = url.split('_');
1111
const org = params[0];
1212
const team = url.replace(org, '').substring(1);
13-
$scope.link = GITHUB_TEAM_URL + org + '/teams/' + team;
13+
$scope.link = $rootScope.appConfig.GITHUB_TEAM_URL + org + '/teams/' + team;
1414
} else {
15-
$scope.link = GITLAB_GROUP_URL + url;
15+
$scope.link = $rootScope.appConfig.GITLAB_GROUP_URL + url;
1616
}
1717
};
1818
_getUrl($scope.provider, $stateParams.url);

src/front/src/app/projects/projects.controller.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ angular.module('topcoderX')
77
$scope.title = 'Project Management';
88

99
//direct base
10-
$scope.directUrlBase = Helper.config().DIRECT_URL_BASE;
10+
$scope.directUrlBase = $rootScope.appConfig.DIRECT_URL_BASE;
1111
$scope.isAdminUser = Helper.isAdminUser(currentUser);
1212
$scope.filter = {
1313
showAll: $scope.isAdminUser,

src/front/src/app/settings/settings.controller.js

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
'use strict';
22

3-
angular.module('topcoderX').controller('SettingController', ['currentUser', '$scope', 'OWNER_LOGIN_GITHUB_URL',
4-
'OWNER_LOGIN_GITLAB_URL', 'SettingService', '$rootScope', 'Dialog', 'Alert', 'Helper', '$window',
3+
angular.module('topcoderX').controller('SettingController', ['currentUser', '$scope',
4+
'SettingService', '$rootScope', 'Dialog', 'Alert', 'Helper', '$window',
55
'Tutorial',
6-
function (currentUser, $scope, OWNER_LOGIN_GITHUB_URL,
7-
OWNER_LOGIN_GITLAB_URL, SettingService, $rootScope, Dialog, Alert, Helper, $window, Tutorial) {
6+
function (currentUser, $scope, SettingService, $rootScope, Dialog, Alert, Helper, $window, Tutorial) {
87
$scope.settings = {};
98
$scope.isLoaded = false;
109

@@ -21,8 +20,8 @@ angular.module('topcoderX').controller('SettingController', ['currentUser', '$sc
2120
_getSetting();
2221

2322
$scope.loginUrl = {
24-
github: Helper.baseUrl + OWNER_LOGIN_GITHUB_URL,
25-
gitlab: Helper.baseUrl + OWNER_LOGIN_GITLAB_URL,
23+
github: Helper.baseUrl + $rootScope.appConfig.OWNER_LOGIN_GITHUB_URL,
24+
gitlab: Helper.baseUrl + $rootScope.appConfig.OWNER_LOGIN_GITLAB_URL,
2625
}
2726

2827
$scope.$on('dialog.finished', function (event, args) {

src/front/src/components/common/navigation.controller.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
'use strict';
22

33
angular.module('topcoderX') // eslint-disable-line angular/no-services
4-
.controller('NavController', ['$scope', '$log', '$state', '$cookies', '$http', '$rootScope', 'TC_USER_PROFILE_URL',
5-
function ($scope, $log, $state, $cookies, $http, $rootScope, TC_USER_PROFILE_URL) {
4+
.controller('NavController', ['$scope', '$log', '$state', '$cookies', '$http', '$rootScope',
5+
function ($scope, $log, $state, $cookies, $http, $rootScope) {
66
$scope.$state = $state;
77
$scope.menuList = false;
88
$scope.user = {};
99
$scope.appConfig = $rootScope.appConfig;
1010

1111
const token = $cookies.get('tcjwt');
1212
const req = {
13-
url: TC_USER_PROFILE_URL,
13+
url: $rootScope.appConfig.TC_USER_PROFILE_URL,
1414
method: 'Get',
1515
headers: {
1616
Authorization: 'Bearer ' + token,

src/routes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,8 @@ module.exports = {
224224
get: {
225225
controller: 'AppConfigController',
226226
method: 'getAppConfig',
227+
allowNormalUser: true,
228+
allowAnonymous: true
227229
},
228230
},
229231
};

src/services/AppConfigService.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,7 @@ const helper = require('../common/helper');
1616
* @returns {Object} the configuration details
1717
*/
1818
async function getAppConfig() {
19-
return {
20-
helpLink: config.HELP_LINK,
21-
copilotRole: config.COPILOT_ROLE,
22-
administratorRoles: config.ADMINISTRATOR_ROLES,
23-
};
19+
return config.frontendConfigs;
2420
}
2521

2622
module.exports = {

0 commit comments

Comments
 (0)