diff options
author | Benjamin Kiessling <mittagessen@l.unchti.me> | 2011-10-24 01:36:27 +0200 |
---|---|---|
committer | Benjamin Kiessling <mittagessen@l.unchti.me> | 2011-10-24 01:36:27 +0200 |
commit | 3fd29331bf3342166f8d9fcb9173c7f40bc91b97 (patch) | |
tree | 6caf18d266a596daa99ce19d39b7789633dcff98 /frontend.js | |
parent | a8bb0fe92506728f27f5c3ee5eede4838c5425d0 (diff) |
refactor code
Diffstat (limited to 'frontend.js')
-rw-r--r-- | frontend.js | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/frontend.js b/frontend.js new file mode 100644 index 0000000..5b2d486 --- /dev/null +++ b/frontend.js @@ -0,0 +1,48 @@ +var Connect = require('connect'); +var http = require('http'); +var url = require('url'); +var path = require('path'); +var fs = require('fs'); + +var configuration; +var model; + +exports.setModel = function(m) { + model = m; +}; + +exports.start = function(config) { + configuration = config; + Connect.createServer( + Connect.logger(), // Log responses to the terminal using Common Log Format. + Connect.responseTime(), // Add a special header with timing information. + Connect.staticCache(), + Connect.static(path.join(__dirname, configuration.dir)), + function(req, res) { + var purl = url.parse(req.url, true); + var slideset = 'default'; + if(purl['query'].hasOwnProperty('slides')) { + slideset = purl['query']['slides']; + } + model.getSlides(slideset, function(slides) { + fs.readFile(path.join(__dirname, configuration.dir, slides['template']) , function(err,data) { + if(err) { res.end(body); } + var body = data.toString(); + var slideStr = ''; + for(var slide in slides['slides']) { + if(!slides['slides'].hasOwnProperty(slide)) { continue; } + if(slides['slides'][slide]['type'] === 'image') { + slideStr = slideStr +'<li><img src="/'+slides['slides'][slide]['file']+'">'; + } + if(slides['slides'][slide]['text'].length > 0) { + slideStr = slideStr+'<div class="transover"><h2>'+slides['slides'][slide]['head']+' - '+slides['slides'][slide]['text']+'</h2></div></img></li>'; + } + } + body = body.replace("$IMAGES", slideStr); + res.setHeader('Content-Length', body.length); + res.end(body); + }); + }); + } + ).listen(8080); +}; |