From 9ed5aac9552c2e02ae7b77fa906dfe53950f47ce Mon Sep 17 00:00:00 2001 From: Johannes Zellner Date: Mon, 23 Jun 2014 23:17:41 -0700 Subject: [PATCH] Serve up a config object containing appstoreOrigin This also changes the webadmin to pickup the appstoreOrigin from the config object to avoid hardcoding --- src/server.js | 9 +++++++++ webadmin/js/appstore.js | 8 +++++--- webadmin/js/client.js | 24 +++++++++++++++++++++++- webadmin/js/index.js | 4 ---- webadmin/views/myapps.js | 4 ++-- 5 files changed, 39 insertions(+), 10 deletions(-) diff --git a/src/server.js b/src/server.js index c7bd6e2c3..408bdef07 100644 --- a/src/server.js +++ b/src/server.js @@ -104,6 +104,12 @@ Server.prototype._getVersion = function (req, res, next) { res.send(200, { version: pkg.version }); }; +Server.prototype._getConfig = function (req, res, next) { + res.send(200, { + appstoreOrigin: this.config.appServerUrl + }); +}; + /* Middleware which makes the route require a password in the body besides a token. */ @@ -189,6 +195,9 @@ Server.prototype._initializeExpressSync = function () { router.get('/api/v1/firsttime', this._firstTime.bind(this)); router.post('/api/v1/createadmin', routes.user.createAdmin); // FIXME any number of admins can be created without auth! + // config.json + router.get('/api/v1/config', bearer, this._getConfig.bind(this)); + // routes controlled by app.router router.post('/api/v1/token', both, routes.user.createToken); // TODO remove that route router.get('/api/v1/user/token', both, routes.user.createToken); diff --git a/webadmin/js/appstore.js b/webadmin/js/appstore.js index 6736451e2..7a9da7f40 100644 --- a/webadmin/js/appstore.js +++ b/webadmin/js/appstore.js @@ -3,7 +3,7 @@ /* global angular:false */ angular.module('YellowTent') -.service('AppStore', function ($http, Config) { +.service('AppStore', function ($http, Client) { function AppStoreError(statusCode, message) { Error.call(this); @@ -22,14 +22,16 @@ angular.module('YellowTent') AppStore.prototype.getApps = function (callback) { if (this._appsCache !== null) return callback(null, this._appsCache); + if (Client.getConfig() === null) return callback(new AppStoreError(500, 'Not yet initialized')); var that = this; - $http.get(Config.APPSTORE_URL + '/api/v1/apps') + + $http.get(Client.getConfig().appstoreOrigin + '/api/v1/apps') .success(function (data, status, headers) { if (status !== 200) return callback(new AppStoreError(status, data)); var apps = data.apps; - apps.forEach(function (app) { app.iconUrl = Config.APPSTORE_URL + "/api/v1/app/" + app.id + "/icon"; }); + apps.forEach(function (app) { app.iconUrl = Client.getConfig().appstoreOrigin + '/api/v1/app/' + app.id + '/icon'; }); that._appsCache = apps; return callback(null, apps); }).error(function (data, status, headers) { diff --git a/webadmin/js/client.js b/webadmin/js/client.js index 235aa218a..e42bf6f64 100644 --- a/webadmin/js/client.js +++ b/webadmin/js/client.js @@ -22,6 +22,7 @@ angular.module('YellowTent') this._token = null; this._clientId = null; this._clientSecret = null; + this._config = null; this.setToken(localStorage.token); } @@ -34,6 +35,10 @@ angular.module('YellowTent') return this._userInfo; }; + Client.prototype.getConfig = function () { + return this._config; + }; + Client.prototype.setToken = function (token) { console.debug('Set client token to ', token); $http.defaults.headers.common.Authorization = 'Token ' + token; @@ -53,6 +58,17 @@ angular.module('YellowTent') /* * Rest API wrappers */ + Client.prototype.config = function (callback) { + $http.get('/api/v1/config') + .success(function(data, status, headers, config) { + if (status !== 200) return callback(new ClientError(status, data)); + callback(null, data); + }) + .error(function(data, status, headers, config) { + callback(new ClientError(status, data)); + }); + }; + Client.prototype.createVolume = function (name, password, callback) { var data = { password: password, name: name }; $http.post('/api/v1/volume/create', data) @@ -271,7 +287,13 @@ angular.module('YellowTent') that._userInfo = data.userInfo; that.setToken(data.token); - callback(null, data.token); + that.config(function (error, result) { + if (error) callback(error); + + that._config = result; + + callback(null, data.token); + }); }) .error(function(data, status, headers, config) { that.setToken(null); diff --git a/webadmin/js/index.js b/webadmin/js/index.js index 66237546b..9ef914e64 100644 --- a/webadmin/js/index.js +++ b/webadmin/js/index.js @@ -63,7 +63,3 @@ app.run(function (acuteSelectService) { acuteSelectService.updateSetting('templatePath', '/3rdparty/templates'); }); -app.service('Config', function () { - this.APPSTORE_URL = 'https://selfhost.io:5050'; -}); - diff --git a/webadmin/views/myapps.js b/webadmin/views/myapps.js index 13a3f9671..1ad494672 100644 --- a/webadmin/views/myapps.js +++ b/webadmin/views/myapps.js @@ -1,6 +1,6 @@ 'use strict'; -var MyAppsController = function ($scope, $http, $location, Config) { +var MyAppsController = function ($scope, $http, $location, Client) { $scope.LOADING = 1; $scope.ERROR = 2; $scope.LOADED = 3; @@ -14,7 +14,7 @@ var MyAppsController = function ($scope, $http, $location, Config) { $http.get('/api/v1/apps') .success(function (data, status, headers) { data.apps.forEach(function (app) { - app.iconUrl = Config.APPSTORE_URL + "/api/v1/app/" + app.id + "/icon"; + app.iconUrl = Client.getConfig().appstoreOrigin + '/api/v1/app/' + app.id + '/icon'; app.url = 'https://' + app.location + '.' + window.location.hostname; }); $scope.apps = data.apps;