1 var Class = require("../lib/mootools/mootools-node.js").Class;
  2 
  3 var RequestProcessor = require("../lib/RequestProcessor.js").RequestProcessor;
  4 var Cache = require("./cache/Cache.js").Cache;
  5 var http = require('http');
  6 
  7 /**
  8  * Create Server on given port.
  9  * @class TemplateEngine Main TemplateEngine Server class.
 10  * @requires Class 
 11  * @requires RequestProcessor 
 12  * @requires Cache 
 13  * @requires http
 14  * @param {Number} [port] port on which should server start. Default is 8080.
 15  */
 16 var TemplateEngine = function(){
 17 
 18     /**
 19      * Server port number
 20      * 
 21      * @private
 22      * @type {Number}
 23      */
 24     this._port = 8080;
 25     
 26     
 27     /**
 28      * Server template path
 29      * 
 30      * @private
 31      * @type {String}
 32      */
 33     this._templatePath = "examples/templates/"; 
 34    
 35     /**@ignore*/
 36     this.initialize = function(port){
 37         port ? this.setPort(port) : false;
 38     };
 39     
 40     /**
 41      * Returns a server port number
 42      * 
 43      * @returns {Number}
 44      */
 45     this.getPort = function(){
 46         return this._port;
 47     };
 48 
 49     /**
 50      * Sets server port number
 51      * 
 52      * @param {Number} port TCP/IP Port number
 53      */
 54     this.setPort = function(port){
 55         this._port = port;
 56     };
 57     
 58     /**
 59      * Returns a server template path
 60      * 
 61      * @returns {String}
 62      */
 63     this.getTemplatePath = function(){
 64         return this._templatePath;
 65     };
 66 
 67     /**
 68      * Sets server template path
 69      * 
 70      * @param {string} templatePath Path to templates folder.
 71      */
 72     this.setTemplatePath = function(templatePath){
 73         this._templatePath = templatePath;
 74     };
 75     
 76    
 77     /**
 78      * Creates template engine server, caching defined resources
 79      * @public
 80      */
 81     this.create = function(){
 82         //create http server and setting up request handler
 83         var that = this;
 84         var server = http.createServer(function(request, response){
 85             that._onRequestHandler(request, response);
 86         });
 87 
 88         var cache = new Cache([
 89             {
 90                 className: "JqTplCache",
 91                 resourcePath: this._templatePath,
 92                 excludePattern: ".svn"
 93             }/*,
 94             {
 95                 className: "StylusCache",
 96                 resourcePath: "examples/stylus/",
 97                 excludePattern: ".svn"
 98             }*/
 99         ]);
100 
101         cache.cache();
102 
103         
104         server.listen(this.getPort());
105         console.log('Server runing @' + this.getPort() + '...');
106     };
107     
108     /**
109      * Handling incoming request
110      * 
111      * @private
112      * 
113      * @param {http.ServerRequest} request
114      * @param {http.ServerResponse} response
115      */
116     this._onRequestHandler = function(request, response){
117         console.log('-- request start');
118         var that = this;
119         if (request.method == 'POST') {
120             var postData = [];
121             request.on('data', function(data) {
122                 postData.push(data);
123             });
124             request.on('end', function(){
125                 request.data = postData.join("");
126                 var requestProcessor = new RequestProcessor(request, response);
127                 setTimeout(function(){
128                      requestProcessor.process();
129                   }
130                 , 2000);
131             });
132         }
133     };
134 
135    
136 };
137 
138 TemplateEngine = new Class(new TemplateEngine());
139 exports.TemplateEngine = TemplateEngine;