diff --git a/lib/cloudcmd.js b/lib/cloudcmd.js index ab8c72b7..8d999277 100644 --- a/lib/cloudcmd.js +++ b/lib/cloudcmd.js @@ -77,6 +77,7 @@ funcs = [ authFunc, + config.serve, rest, route, diff --git a/lib/server/config.js b/lib/server/config.js index 648a5bfd..0d21f5c8 100644 --- a/lib/server/config.js +++ b/lib/server/config.js @@ -10,12 +10,18 @@ HOME = (HOME_UNIX || HOME_WIN) + '/', - fs = require('fs'), + crypto = require('crypto'), Util = require(DIR_LIB + 'util'), + CloudFunc = require(DIR_LIB + 'cloudfunc'), + + ponse = require(DIR_SERVER + 'ponse'), + pipe = require(DIR_SERVER + 'pipe'), tryRequire = require(DIR_SERVER + 'tryRequire'), + apiURL = CloudFunc.apiURL, + ConfigPath = DIR + 'json/config.json', ConfigHome = HOME + '.cloudcmd.json', @@ -23,7 +29,11 @@ tryRequire(ConfigHome) || tryRequire(ConfigPath, {log: true}) || {}; - module.exports = function(key, value) { + module.exports = set; + module.exports.save = save; + module.exports.serve = serve; + + function set(key, value) { var result; if (value === undefined) @@ -32,9 +42,9 @@ config[key] = value; return result; - }; + } - module.exports.save = function(callback) { + function save(callback) { var data = Util.json.stringify(config); Util.checkArgs(arguments, ['callback']); @@ -45,6 +55,75 @@ callback({ message: 'Error: config is empty!' }); - }; + } + + function serve(req, res, next) { + console.log(req.url) + if (req.url !== apiURL + '/config') { + next(); + } else { + switch(req.method) { + case 'GET': + get(req, res, next); + break; + + case 'PUT': + put(req, res, next); + break; + + default: + next(); + } + } + } + + function get(req, res) { + var data = Util.json.stringify(config); + + console.log('>>>>') + + ponse.send(data, { + name : 'config.json', + request : req, + response: res, + cache : false + }); + } + + function put(req, res, callback) { + var options = { + name : 'config.json', + request : req, + response: res, + cache : false + }; + + pipe.getBody(req, function(error, body) { + var data = '', + json = Util.json.parse(body) || {}, + passwd = json.passwd, + sha = crypto.createHash('sha1'); + + if (error) { + callback(error); + } else if (json.passwd) { + sha.update(passwd); + passwd = sha.digest('hex'); + json.password = passwd; + } + + Object.keys(json).forEach(function(name) { + data = CloudFunc.formatMsg('config', name); + set(name, json[name]); + }); + + save(function(error) { + if (error) + ponse.sendError(error, options); + else + ponse.send(data, options); + }); + }); + } })(); diff --git a/lib/server/rest.js b/lib/server/rest.js index 86b7f135..ca74ee78 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -11,12 +11,9 @@ var DIR = './', DIR_LIB = DIR + '../', - DIR_ROOT = __dirname + '/' + DIR_LIB + '../', - DIR_JSON = DIR_ROOT + 'json/', fs = require('fs'), path = require('path'), - crypto = require('crypto'), Util = require(DIR_LIB + 'util'), CloudFunc = require(DIR_LIB + 'cloudfunc'), @@ -30,7 +27,6 @@ mellow = require(DIR + 'mellow'), ponse = require(DIR + 'ponse'), pipe = require(DIR + 'pipe'), - config = require(DIR + 'config'), isWin32 = process.platform === 'win32', @@ -239,15 +235,6 @@ callback(null, {name: 'api.json'}, p.data); break; - case 'config': - ponse.sendFile({ - name : DIR_JSON + 'config.json', - request : p.request, - response: p.response, - cache : false - }); - break; - default: json = { message: 'Error: command not found!' @@ -264,7 +251,7 @@ * @param pParams {command, method, body, requrest, response} */ function onPUT(name, body, callback) { - var cmd, files, json, data, from, to, error; + var cmd, files, data, from, to, error; Util.checkArgs(arguments, ['name', 'body', 'callback']); @@ -377,29 +364,6 @@ break; - case 'config': - var passwd = files && files.password, - sha = crypto.createHash('sha1'); - - if (passwd) { - sha.update(passwd); - passwd = sha.digest('hex'); - files.password = passwd; - } - - Object.keys(files).forEach(function(name) { - config(name, files[name]); - }); - - json = Util.json.stringify(config) + '\n'; - - config.save(function(error) { - data = formatMsg('config', name); - callback(error, data); - }); - - break; - default: callback(); break;