feature(config) add

This commit is contained in:
coderaiser 2014-09-16 04:51:22 -04:00
parent 7d93c9930c
commit cd296f84be
5 changed files with 81 additions and 115 deletions

40
lib/server/config.js Normal file
View file

@ -0,0 +1,40 @@
(function() {
var DIR_SERVER = __dirname + '/',
DIR_LIB = DIR_SERVER + '../',
DIR = DIR_SERVER + '../../',
fs = require('fs'),
Util = require(DIR_LIB + 'util'),
tryRequire = require(DIR_SERVER + 'tryRequire'),
ConfigPath = DIR + 'json/config.json',
config = tryRequire(ConfigPath) || {};
module.exports = function(key, value) {
var result;
if (value === undefined)
result = config[key];
else
config[key] = value;
return result;
};
module.exports.save = function(callback) {
var data = Util.stringifyJSON(config);
Util.checkArgs(arguments, ['callback']);
if (data)
fs.writeFile(ConfigPath, data, callback);
else
callback({
message: 'Error: config is empty!'
});
};
})();

View file

@ -1,21 +1,15 @@
/* RESTful module */
(function() {
'use strict';
if (!global.cloudcmd)
return console.log(
'# rest.js' + '\n' +
'# -----------' + '\n' +
'# Module is part of Cloud Commander,' + '\n' +
'# used for work with REST API.' + '\n' +
'# If you wont to see at work set rest: true' + '\n' +
'# and apiURL in config.json' + '\n' +
'# http://cloudcmd.io' + '\n');
/*
'# rest.js' + '\n' +
'# -----------' + '\n' +
'# Module is part of Cloud Commander,' + '\n' +
'# used for work with REST API.' + '\n' +
'# http://cloudcmd.io' + '\n');
*/
var main = global.cloudcmd.main,
DIR = './',
var DIR = './',
DIR_LIB = DIR + '../',
DIR_ROOT = __dirname + '/' + DIR_LIB + '../',
DIR_JSON = DIR_ROOT + 'json/',
@ -36,6 +30,7 @@
mellow = require(DIR + 'mellow'),
ponse = require(DIR + 'ponse'),
pipe = require(DIR + 'pipe'),
config = require(DIR + 'config'),
isWin32 = process.platform === 'win32',
@ -270,7 +265,7 @@
* @param pParams {command, method, body, requrest, response}
*/
function onPUT(name, body, callback) {
var cmd, files, json, config, data, from, to, error;
var cmd, files, json, data, from, to, error;
Util.checkArgs(arguments, ['name', 'body', 'callback']);
@ -386,7 +381,6 @@
case 'config':
var passwd = files && files.password,
sha = crypto.createHash('sha1');
config = main.config;
if (passwd) {
sha.update(passwd);
@ -395,12 +389,12 @@
}
Object.keys(files).forEach(function(name) {
config[name] = files[name];
config(name, files[name]);
});
json = Util.stringifyJSON(config) + '\n';
fs.writeFile(DIR_JSON + 'config.json', json, function(error) {
config.save(function(error) {
data = formatMsg('config', name);
callback(error, data);
});

View file

@ -9,18 +9,16 @@
fs = require('fs'),
main = require(DIR_SERVER + 'main'),
mellow = require(DIR_SERVER + 'mellow'),
ponse = require(DIR_SERVER + 'ponse'),
files = require(DIR_SERVER + 'files'),
minify = require(DIR_SERVER + 'minify'),
config = require(DIR_SERVER + 'config'),
Util = require(DIR_LIB + 'util'),
CloudFunc = require(DIR_LIB + 'cloudfunc'),
format = require(DIR_LIB + 'format'),
Config = main.config,
PATH_INDEX = DIR_FS + 'index.html',
TMPL_PATH = [
@ -51,7 +49,7 @@
data = options.data,
panel = options.panel;
if (!Config.showKeysPanel) {
if (!config('showKeysPanel')) {
keysPanel = '<div class="keyspanel';
data = data.replace(keysPanel + '"', keysPanel +' hidden"');
}
@ -139,7 +137,7 @@
p.name = DIR_HTML + name + '.html';
ponse.sendFile(p);
} else if (isFS) {
name = Util.rmStrOnce(name, CloudFunc.FS) || main.SLASH;
name = Util.rmStrOnce(name, CloudFunc.FS) || '/';
path = mellow.convertPath(name);
mellow.read(path, function(error, dir) {
@ -176,7 +174,7 @@
}
function buildIndex(json, callback) {
var isMinify = Config.minify;
var isMinify = config('minify');
Util.exec.if(!isMinify, function(error, name) {
fs.readFile(name || PATH_INDEX, 'utf8', function(error, template) {