mirror of
https://github.com/coderaiser/cloudcmd.git
synced 2026-07-18 17:05:17 +00:00
improved rest module
This commit is contained in:
parent
74527489ab
commit
ecf8080b0d
3 changed files with 87 additions and 53 deletions
|
|
@ -48,7 +48,7 @@
|
|||
|
||||
/* Additional Modules */
|
||||
|
||||
exports.auth = srvrequire('auth'),
|
||||
exports.auth = srvrequire('auth').auth,
|
||||
exports.appcache = srvrequire('appcache'),
|
||||
exports.cache = srvrequire('cache').Cache,
|
||||
exports.cloudfunc = librequire('cloudfunc'),
|
||||
|
|
|
|||
|
|
@ -6,9 +6,9 @@
|
|||
var DIR = process.cwd() + '/',
|
||||
main = require(DIR + 'lib/server/main'),
|
||||
Util = main.util,
|
||||
|
||||
APIURL = '/api/v1',
|
||||
OK = 200;
|
||||
OK = 200,
|
||||
Header = main.generateHeaders('api.json', false);
|
||||
|
||||
/**
|
||||
* rest interface
|
||||
|
|
@ -21,49 +21,81 @@
|
|||
lUrl = lReq.url,
|
||||
lMethod = lReq.method;
|
||||
|
||||
console.log(lUrl);
|
||||
console.log(lMethod);
|
||||
|
||||
if( Util.isContainStr(lUrl, APIURL) ){
|
||||
var lHead = main.generateHeaders('api.json', false);
|
||||
lRes.writeHead(OK, lHead);
|
||||
lRes.end(APIURL);
|
||||
var lCommand = Util.removeStr(lUrl, APIURL),
|
||||
lData = getData(lMethod, lCommand);
|
||||
|
||||
send(lRes, lData);
|
||||
|
||||
lRet = true;
|
||||
}
|
||||
|
||||
return lRet;
|
||||
/*
|
||||
switch(req.method){
|
||||
case 'GET':
|
||||
switch(lCommand){
|
||||
case 'count':
|
||||
lResult = {
|
||||
count : Users.length,
|
||||
};
|
||||
break;
|
||||
|
||||
case 'list':
|
||||
lResult = Users;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'PUT':
|
||||
if( lCommand.indexOf('register') === 0 ){
|
||||
lResult = {
|
||||
registered : true,
|
||||
name : req.body.name,
|
||||
server_time : Util.getTime(),
|
||||
client_time : req.body.time
|
||||
};
|
||||
Users.push(req.body.name);
|
||||
|
||||
console.log(lResult);
|
||||
console.log(req.connection.remoteAddress);
|
||||
}
|
||||
break;
|
||||
}
|
||||
*/
|
||||
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* send data
|
||||
*
|
||||
* @param pRes
|
||||
* @param pData
|
||||
*/
|
||||
function send(pRes, pData){
|
||||
pRes.writeHead(OK, Header);
|
||||
pRes.end( pData.toString() );
|
||||
}
|
||||
|
||||
/**
|
||||
* getting data on method and command
|
||||
*
|
||||
* @param pMethod
|
||||
* @param pCommand
|
||||
*/
|
||||
function getData(pMethod, pCommand){
|
||||
var lResult;
|
||||
|
||||
switch(pMethod){
|
||||
case 'GET':
|
||||
lResult = onGET(pCommand);
|
||||
break;
|
||||
|
||||
case 'PUT':
|
||||
lResult = onPUT(pCommand);
|
||||
break;
|
||||
}
|
||||
|
||||
return lResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* process data on GET request
|
||||
*
|
||||
* @param pCommand
|
||||
*/
|
||||
function onGET(pCommand){
|
||||
var lResult;
|
||||
|
||||
switch(pCommand){
|
||||
case '':
|
||||
lResult = {info: 'Cloud Commander API v1'};
|
||||
break;
|
||||
}
|
||||
|
||||
return lResult;
|
||||
}
|
||||
|
||||
/**
|
||||
* process data on PUT request
|
||||
*
|
||||
* @param pCommand
|
||||
*/
|
||||
function onPUT(pCommand){
|
||||
var lResult;
|
||||
|
||||
switch(pCommand){
|
||||
}
|
||||
|
||||
return lResult;
|
||||
}
|
||||
|
||||
})();
|
||||
|
|
|
|||
24
server.js
24
server.js
|
|
@ -70,9 +70,10 @@ var CloudServer = {
|
|||
/* КОНСТАНТЫ */
|
||||
INDEX : 'index.html'
|
||||
},
|
||||
|
||||
|
||||
DirPath = '/',
|
||||
|
||||
OK = 200,
|
||||
DIR = process.cwd() + '/',
|
||||
main = require(DIR + 'lib/server/main.js'),
|
||||
|
||||
|
|
@ -271,7 +272,7 @@ CloudServer._controller = function(pReq, pRes)
|
|||
CloudServer.Responses[lName] = pRes;
|
||||
|
||||
/* saving status OK for current file */
|
||||
CloudServer.Statuses[lName] = 200;
|
||||
CloudServer.Statuses[lName] = OK;
|
||||
|
||||
/* Берём значение из кэша
|
||||
* сжатый файл - если gzip-поддерживаеться браузером
|
||||
|
|
@ -347,7 +348,7 @@ CloudServer._controller = function(pReq, pRes)
|
|||
lResult = false;
|
||||
|
||||
if(!lResult)
|
||||
Fs.readFile(lName, lReadFileFunc_f);
|
||||
Fs.readFile(lName, lReadFileFunc_f);
|
||||
}else{/* если мы имеем дело с файловой системой*/
|
||||
/* если путь не начинаеться с no-js - значит
|
||||
* js включен
|
||||
|
|
@ -384,7 +385,7 @@ CloudServer._controller = function(pReq, pRes)
|
|||
|
||||
/* если в итоге путь пустой
|
||||
* делаем его корневым
|
||||
*/
|
||||
*/
|
||||
if (pathname === '')
|
||||
pathname = '/';
|
||||
|
||||
|
|
@ -392,10 +393,10 @@ CloudServer._controller = function(pReq, pRes)
|
|||
|
||||
CloudServer.Responses[DirPath] = pRes;
|
||||
|
||||
CloudServer.Statuses[DirPath] = 200;
|
||||
CloudServer.Statuses[DirPath] = OK;
|
||||
|
||||
/* saving query of current file */
|
||||
CloudServer.Queries[DirPath] = lQuery;
|
||||
CloudServer.Queries[DirPath] = lQuery;
|
||||
Util.log(lQuery);
|
||||
|
||||
console.log(DirPath);
|
||||
|
|
@ -403,12 +404,13 @@ CloudServer._controller = function(pReq, pRes)
|
|||
/* читаем основные данные о файле */
|
||||
if( lQuery && lQuery.indexOf('code=') === 0){
|
||||
var lAuth = main.auth;
|
||||
|
||||
if(lAuth) lAuth.auth(lQuery, function(){
|
||||
|
||||
if( Util.isFunction(lAuth) )
|
||||
lAuth(lQuery, function(){
|
||||
Fs.stat(DirPath, CloudServer._stated);
|
||||
});
|
||||
console.log('***********');
|
||||
}else
|
||||
}else
|
||||
Fs.stat(DirPath, CloudServer._stated);
|
||||
|
||||
/* если установлено сжатие
|
||||
|
|
@ -418,13 +420,13 @@ CloudServer._controller = function(pReq, pRes)
|
|||
CloudServer.INDEX = (CloudServer.Minify._allowed.html ?
|
||||
'.' + CloudServer.Minify.MinFolder + 'index.min.html'
|
||||
: CloudServer.INDEX);
|
||||
|
||||
|
||||
/*
|
||||
* сохраним указатель на response
|
||||
* и на статус ответа
|
||||
*/
|
||||
CloudServer.Responses[CloudServer.INDEX] = pRes;
|
||||
CloudServer.Statuses [CloudServer.INDEX] = 200;
|
||||
CloudServer.Statuses [CloudServer.INDEX] = OK;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue