From 876e79eddb61eb30bfee7287b23b078cf0959fb7 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Thu, 15 Nov 2012 07:22:17 -0500 Subject: [PATCH] work on rest module --- cloudcmd.js | 15 ++++++++++++++- lib/server/main.js | 1 + lib/server/rest.js | 34 ++++++++++++++++----------------- server.js | 47 ++++++++++++++++++++++++++++++---------------- 4 files changed, 62 insertions(+), 35 deletions(-) diff --git a/cloudcmd.js b/cloudcmd.js index fc1c4906..a8039787 100644 --- a/cloudcmd.js +++ b/cloudcmd.js @@ -18,7 +18,11 @@ Config = main.config; readConfig(); - Server.start(Config, indexProcessing, appCacheProcessing); + Server.start(Config, { + index : indexProcessing, + appcache : appCacheProcessing, + rest : rest + }); if(update) update.get(); @@ -75,6 +79,15 @@ lAppCache.createManifest(); } + /** + * rest interface + * @pConnectionData {request, responce} + */ + function rest(pConnectionData){ + console.log('rest'); + Util.exec(main.rest, pConnectionData); + } + function readConfig(){ /* Determining server.js directory diff --git a/lib/server/main.js b/lib/server/main.js index b5daa013..11931c62 100644 --- a/lib/server/main.js +++ b/lib/server/main.js @@ -24,6 +24,7 @@ exports.http = require('http'), exports.https = require('https'), exports.path = require('path'), + exports.url = require('url'), exports.querystring = require('querystring'), /* Needed Modules */ diff --git a/lib/server/rest.js b/lib/server/rest.js index 982ae4c4..2b49f909 100644 --- a/lib/server/rest.js +++ b/lib/server/rest.js @@ -4,28 +4,26 @@ "use strict"; var DIR = process.cwd() + '/', - main = require(DIR + 'lib/server/main.js'), - SRVDIR = main.SRVDIR, + main = require(DIR + 'lib/server/main'), Util = main.util, - APIURL = '/api/v1/'; + APIURL = '/api/v1/'; - exports.rest = function(req, res, pCallBack){ - var lUrl = req.url, - lMethod = req.method; - - console.log(lUrl); - /* if lUrl contains api url */ - if( Util.isContainStr(lUrl, APIURL) ){ - lUrl = lUrl.replace(APIURL, ''); + /** + * rest interface + * @pConnectionData {request, responce} + */ + exports.rest = function(pConnectionData){ + var lReq = pConnectionData.request, + lRes = pConnectionData.response, + lUrl = lReq.url, + lMethod = lReq.method; + console.log(lUrl); - } - - console.log(req.url); - console.log(lMethod); - - Util.exec(pCallBack); - + console.log(lMethod); + + if( Util.isContainStr(lUrl, APIURL) ) + console.log('api !!!!!!!!!!!! '); /* switch(req.method){ case 'GET': diff --git a/server.js b/server.js index 529e7595..99098c4e 100644 --- a/server.js +++ b/server.js @@ -140,7 +140,10 @@ CloudServer.init = (function(pAppCachProcessing){ * Функция создаёт сервер * @param pConfig */ -CloudServer.start = function (pConfig, pIndexProcessing, pAppCachProcessing) { +CloudServer.start = function (pConfig, pProcessing) { + if(!pProcessing) + pProcessing = {}; + if(pConfig) this.Config = pConfig; @@ -151,9 +154,10 @@ CloudServer.start = function (pConfig, pIndexProcessing, pAppCachProcessing) { var lConfig = this.Config; - CloudServer.indexProcessing = pIndexProcessing; + CloudServer.indexProcessing = pProcessing.index; + CloudServer.rest = pProcessing.rest; - this.init(pAppCachProcessing); + this.init(pProcessing.appcache); this.Port = process.env.PORT || /* c9 */ process.env.app_port || /* nodester */ @@ -255,10 +259,11 @@ CloudServer.generateHeaders = function(pName, pGzip){ CloudServer._controller = function(pReq, pRes) { /* Читаем содержимое папки, переданное в url */ - var url = require("url"), - lParsedUrl = url.parse(pReq.url), - pathname = lParsedUrl.pathname, - + var lConfig = CloudServer.Config, + url = main.url, + lParsedUrl = url.parse(pReq.url), + pathname = lParsedUrl.pathname, + /* varible contain one of queris: * download - change content-type for * make downloading process @@ -269,7 +274,7 @@ CloudServer._controller = function(pReq, pRes) * query like this * ?json */ - lQuery = lParsedUrl.query; + lQuery = lParsedUrl.query; if(lQuery) console.log('query = ' + lQuery); @@ -295,16 +300,21 @@ CloudServer._controller = function(pReq, pRes) console.log("request for " + pathname + " received..."); + if( lConfig.rest ) + Util.exec(CloudServer.rest, { + request : pReq, + response : pRes + }); /* если в пути нет информации ни о ФС, * ни об отсутствии js, * ни о том, что это корневой * каталог - загружаем файлы проэкта */ - if ( !Util.isContainStr(pathname, lFS_s) && - !Util.isContainStr(pathname, lNoJS_s) && - !Util.strCmp(pathname, '/') && - !Util.strCmp(lQuery, 'json') ) { + else if ( !Util.isContainStr(pathname, lFS_s) && + !Util.isContainStr(pathname, lNoJS_s) && + !Util.strCmp(pathname, '/') && + !Util.strCmp(lQuery, 'json') ) { /* если имена файлов проекта - загружаем их*/ /* убираем слеш и читаем файл с текущец директории*/ @@ -313,7 +323,7 @@ CloudServer._controller = function(pReq, pRes) console.log('reading '+lName); /* watching is file changed */ - if(CloudServer.Config.appcache) + if(lConfig.appcache) CloudServer.AppCache.watch(lName); /* сохраняем указатель на response и имя */ @@ -372,7 +382,7 @@ CloudServer._controller = function(pReq, pRes) */ else if(lName.indexOf('min') < 0 && CloudServer.Minify){ - var lMin_o = CloudServer.Config.minification, + var lMin_o = lConfig.minification, lCheck_f = function(pExt){ return CloudFunc.checkExtension(lName,pExt); @@ -799,7 +809,12 @@ CloudServer.sendResponse = function(pHead, pData, pName){ } }; -exports.start = function(pConfig, pIndexProcessing, pAppCachProcessing){ - CloudServer.start(pConfig, pIndexProcessing, pAppCachProcessing); +/** + * start server function + * @param pConfig + * @param pProcessing {index, appcache, rest} + */ +exports.start = function(pConfig, pProcessing){ + CloudServer.start(pConfig, pProcessing); }; exports.CloudServer = CloudServer; \ No newline at end of file