Refactor apps code into apps.js
This commit is contained in:
+51
@@ -0,0 +1,51 @@
|
||||
'use strict';
|
||||
|
||||
var DatabaseError = require('./databaseerror.js'),
|
||||
util = require('util'),
|
||||
debug = require('debug')('server:app'),
|
||||
assert = require('assert'),
|
||||
safe = require('safetydance'),
|
||||
appdb = require('./appdb.js'),
|
||||
superagent = require('superagent');
|
||||
|
||||
exports = module.exports = {
|
||||
install: install
|
||||
};
|
||||
|
||||
var STATUS_PENDING = 'pending';
|
||||
|
||||
function installTask() {
|
||||
appdb.getAll(function (error, apps) {
|
||||
if (error) {
|
||||
debug('Error reading apps table ' + error);
|
||||
return;
|
||||
}
|
||||
|
||||
apps.forEach(function (app) {
|
||||
if (app.status === 'Installed') return;
|
||||
|
||||
superagent
|
||||
.get(appServerUrl + '/api/v1/app/' + app.id + '/manifest')
|
||||
.set('Accept', 'application/x-yaml')
|
||||
.end(function (err, res) {
|
||||
console.log(err);
|
||||
console.log(res);
|
||||
res.pipe(process.stdout);
|
||||
// TODO: change status to Downloaded/Error
|
||||
// TODO: actually install the app
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function install(appId, callback) {
|
||||
appdb.add(appId, { status: STATUS_PENDING }, function (error) {
|
||||
if (error && error.reason === DatabaseError.ALREADY_EXISTS) return next(new HttpError(400, 'Already installed or installing'));
|
||||
if (error) return next(new HttpError(500, 'Internal error:' + error.message));
|
||||
|
||||
process.nextTick(installTask);
|
||||
|
||||
next(new HttpSuccess(200, { status: 'Downloading' }));
|
||||
});
|
||||
}
|
||||
|
||||
+5
-35
@@ -2,10 +2,8 @@
|
||||
|
||||
var HttpError = require('../httperror.js'),
|
||||
HttpSuccess = require('../httpsuccess.js'),
|
||||
debug = require('debug')('server:routes/app'),
|
||||
superagent = require('superagent'),
|
||||
appdb = require('../appdb.js'),
|
||||
DatabaseError = require('../databaseerror.js');
|
||||
debug = require('debug')('server:routes/apps'),
|
||||
apps = require('../apps.js');
|
||||
|
||||
exports = module.exports = {
|
||||
initialize: initialize,
|
||||
@@ -18,30 +16,6 @@ function initialize(config) {
|
||||
appServerUrl = config.appServerUrl;
|
||||
}
|
||||
|
||||
function installTask() {
|
||||
appdb.getAll(function (error, apps) {
|
||||
if (error) {
|
||||
debug('Error reading apps table ' + error);
|
||||
return;
|
||||
}
|
||||
|
||||
apps.forEach(function (app) {
|
||||
if (app.status === 'Installed') return;
|
||||
|
||||
superagent
|
||||
.get(appServerUrl + '/api/v1/app/' + app.id + '/manifest')
|
||||
.set('Accept', 'application/x-yaml')
|
||||
.end(function (err, res) {
|
||||
console.log(err);
|
||||
console.log(res);
|
||||
res.pipe(process.stdout);
|
||||
// TODO: change status to Downloaded/Error
|
||||
// TODO: actually install the app
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function installApp(req, res, next) {
|
||||
var data = req.body;
|
||||
|
||||
@@ -50,13 +24,9 @@ function installApp(req, res, next) {
|
||||
|
||||
console.log('will install app with id ' + data.app_id);
|
||||
|
||||
appdb.add(data.app_id, { status: 'Downloading' }, function (error) {
|
||||
if (error && error.reason === DatabaseError.ALREADY_EXISTS) return next(new HttpError(400, 'Already installed or installing'));
|
||||
if (error) return next(new HttpError(500, 'Internal error:' + error));
|
||||
|
||||
process.nextTick(installTask);
|
||||
|
||||
next(new HttpSuccess(200, { status: 'Downloading' }));
|
||||
apps.install(data.app_id, function (error) {
|
||||
if (error) return next(new HttpError(400, 'Error installing app: ' + error));
|
||||
next(new HttpSuccess(200));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user