mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-26 01:18:15 +00:00
server separeted to two files: cloudcmd.js and server.js
This commit is contained in:
parent
0d29b68aa2
commit
4cf75a4b82
2 changed files with 89 additions and 62 deletions
73
cloudcmd.js
Normal file
73
cloudcmd.js
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
var Server = cloudRequire('./server'),
|
||||
path = cloudRequire('path'),
|
||||
fs = cloudRequire('fs');
|
||||
|
||||
var Config = readConfig();
|
||||
|
||||
Config ? Server.start(Config) : Server.start();
|
||||
|
||||
|
||||
function readConfig(){
|
||||
|
||||
/* Determining server.js directory
|
||||
* and chang current process directory
|
||||
* (usually /) to it.
|
||||
* argv[1] - is always script name
|
||||
*/
|
||||
var lServerDir = path.dirname(process.argv[1]),
|
||||
lProcessDir = process.cwd();
|
||||
lConfig = {};
|
||||
|
||||
if(lProcessDir !== lServerDir){
|
||||
console.log('current dir: ' + lProcessDir);
|
||||
process.chdir(lServerDir);
|
||||
}
|
||||
console.log('server dir: ' + lServerDir);
|
||||
|
||||
console.log('reading configuretion file config.json...');
|
||||
var lConfig = cloudRequire('./config');
|
||||
if(lConfig){
|
||||
console.log('config.json readed');
|
||||
|
||||
/* if command line parameter testing resolved
|
||||
* setting config to testing, so server
|
||||
* not created, just init and
|
||||
* all logs writed to screen
|
||||
*/
|
||||
if (process.argv[2] === 'test') {
|
||||
console.log(process.argv);
|
||||
lConfig.server = false;
|
||||
lConfig.logs = false;
|
||||
}
|
||||
|
||||
if (lConfig.logs) {
|
||||
console.log('log param setted up in config.json\n' +
|
||||
'from now all logs will be writed to log.txt');
|
||||
writeLogsToFile();
|
||||
}
|
||||
|
||||
return lConfig;
|
||||
}
|
||||
else return false;
|
||||
}
|
||||
|
||||
/* function sets stdout to file log.txt */
|
||||
function writeLogsToFile(){
|
||||
var stdo = fs.createWriteStream('./log.txt');
|
||||
|
||||
process.stdout.write = (function(write) {
|
||||
return function(string, encoding, fd) {
|
||||
stdo.write(string);
|
||||
};
|
||||
})(process.stdout.write);
|
||||
}
|
||||
|
||||
/* function do safe require of needed module */
|
||||
function cloudRequire(pModule){
|
||||
try{
|
||||
return require(pModule);
|
||||
}
|
||||
catch(pError){
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue