Commit fe57d070 authored by Antek Grzanka's avatar Antek Grzanka

loading API configuration from endpoint.

parent 1ca25f29
(function(){ (function(){
angular.module('ctfApp').controller('TaskController', ['$scope', '$stateParams', 'APIProvider', 'AppSettings', function($scope, $stateParams, APIProvider, AppSettings) { angular.module('ctfApp').controller('TaskController', ['$scope', '$stateParams', 'APIProvider', 'AppSettings', function($scope, $stateParams, APIProvider, AppSettings) {
$scope.submitUnknown = false; $scope.submitUnknown = false;
$scope.apiAddress = 'http://' + AppSettings.apiAddress; $scope.apiAddress = AppSettings.getApiAddress();
$scope.selectedTaskId = $stateParams.taskLevel; $scope.selectedTaskId = $stateParams.taskLevel;
// console.log('SELECTED TASK ID', $scope.selectedTaskId); // console.log('SELECTED TASK ID', $scope.selectedTaskId);
APIProvider.getTaskById($scope.selectedTaskId, function(data){ APIProvider.getTaskById($scope.selectedTaskId, function(data){
......
(function(){ (function(){
angular.module('ctfApp').controller('TasksController', ['$scope', '$http', 'APIProvider', 'AppSettings', function($scope, $http, APIProvider, AppSettings) { angular.module('ctfApp').controller('TasksController', ['$scope', '$http', 'APIProvider', 'AppSettings', function($scope, $http, APIProvider, AppSettings) {
$scope.apiAddress = AppSettings.apiAddress; $scope.apiAddress = AppSettings.getApiAddress();
APIProvider.getTasks(function(data){ APIProvider.getTasks(function(data){
// console.log(data); // console.log(data);
APIProvider.getMyCompletedLevelsList(function(completed){ APIProvider.getMyCompletedLevelsList(function(completed){
......
...@@ -37,12 +37,31 @@ ...@@ -37,12 +37,31 @@
}]); }]);
app.factory('AppSettings', function() { app.factory('AppSettings', ['$http', function($http) {
var dev = "localhost:8080/api/v1", prod = "52.28.244.24:8080/api/v1";
// fallback into defaults, if everything fails, world collapses and /startup will not resolve
var domainName = '52.28.244.24', port = "http", schema = 8080;
return { return {
apiAddress: prod
}; initAPI: function(){
$http.get('/api/v1/startup').success(function(data){
console.debug(data);
domainName = data.domainName;
port = data.port;
schema = data.schema;
}); });
},
getApiAddress: function(){
return schema + "://" + domainName + ":" + port + "/api/v1";
},
getApiAddressWithCredentials: function(username, password){
return schema + "://" + username + ":" + password + "@" + domainName + ":" + port + "/api/v1";
}
};
}]);
app.config(['$httpProvider', function ($httpProvider) { app.config(['$httpProvider', function ($httpProvider) {
...@@ -59,8 +78,10 @@ ...@@ -59,8 +78,10 @@
}]); }]);
app.run(['$rootScope', '$location', '$cookieStore', '$http', 'NavbarService', app.run(['$rootScope', '$location', '$cookieStore', '$http', 'NavbarService', 'AppSettings',
function ($rootScope, $location, $cookieStore, $http, NavbarService) { function ($rootScope, $location, $cookieStore, $http, NavbarService, AppSettings) {
AppSettings.initAPI();
$(".button-collapse").sideNav(); $(".button-collapse").sideNav();
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
getScores: function(callback){ getScores: function(callback){
$http.get('http://' + AppSettings.apiAddress + '/solutions/all'). $http.get(AppSettings.getApiAddress() + '/solutions/all').
success(function(data) { success(function(data) {
scores = data; scores = data;
if (callback) callback(data); if (callback) callback(data);
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
}, },
getTasks: function(callback){ getTasks: function(callback){
$http.get('http://' + AppSettings.apiAddress + '/tasks'). $http.get(AppSettings.getApiAddress() + '/tasks').
success(function(data) { success(function(data) {
this.getMySolutions(function(solutions){ this.getMySolutions(function(solutions){
if (solutions){ if (solutions){
...@@ -69,7 +69,7 @@ ...@@ -69,7 +69,7 @@
* @param callback * @param callback
*/ */
getMyCompletedLevelsList: function(callback){ getMyCompletedLevelsList: function(callback){
$http.get('http://' + AppSettings.apiAddress + '/solutions/my/completed'). $http.get(AppSettings.getApiAddress() + '/solutions/my/completed').
success(function(data) { success(function(data) {
if (callback) callback(data); if (callback) callback(data);
}); });
...@@ -82,7 +82,7 @@ ...@@ -82,7 +82,7 @@
* @returns {HttpPromise} * @returns {HttpPromise}
*/ */
submitFlag: function(level, flag){ submitFlag: function(level, flag){
return $http.post('http://' + AppSettings.apiAddress + '/solutions/' + level, flag); return $http.post(AppSettings.getApiAddress() + '/solutions/' + level, flag);
} }
}; };
}]); }]);
......
...@@ -15,7 +15,7 @@ ...@@ -15,7 +15,7 @@
*/ */
login: function (username, password, callback) { login: function (username, password, callback) {
$http.get('http://' + username + ":" + password + "@" + AppSettings.apiAddress + '/whoami', { $http.get(AppSettings.getApiAddressWithCredentials(username, password) + '/whoami', {
headers: {'Authorization': 'Basic ' + Base64.encode(username + ':' + password)} headers: {'Authorization': 'Basic ' + Base64.encode(username + ':' + password)}
}) })
.then(function (response) { .then(function (response) {
...@@ -35,7 +35,7 @@ ...@@ -35,7 +35,7 @@
*/ */
logout: function (callback) { logout: function (callback) {
$http.get('http://' + 'askdjfadsf' + ':' + 'adsfasskdjfadsf' + '@' + AppSettings.apiAddress + '/whoami', { $http.get(AppSettings.getApiAddressWithCredentials('askdjfadsf', 'adsfasskdjfadsf') + '/whoami', {
headers: {'Authorization': 'Basic ' + Base64.encode('askdjfadsf' + ':' + 'adsfasskdjfadsf')} headers: {'Authorization': 'Basic ' + Base64.encode('askdjfadsf' + ':' + 'adsfasskdjfadsf')}
}).then(function(response){ }).then(function(response){
this.clearCredentials(); this.clearCredentials();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment