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 /main.js | |
parent | a8bb0fe92506728f27f5c3ee5eede4838c5425d0 (diff) |
refactor code
Diffstat (limited to 'main.js')
-rw-r--r-- | main.js | 97 |
1 files changed, 12 insertions, 85 deletions
@@ -1,88 +1,15 @@ -var Connect = require('connect'); -var http = require('http'); -var walk = require('walk'); -var fs = require('fs'); -var url = require('url'); -var path = require('path'); -var files = []; -var walker = walk.walk('./slideshows',{ followLinks: false }); +#! /usr/bin/env node -walker.on("directories", function (root, dirAr, next) { - var lastDir = path.join(__dirname, root).split('/'); - if((lastDir.length - __dirname.split('/').length) < 2) { - dirAr.forEach(function(dir) { - files[dir.name] = []; - }); - } - next(); -}).on("file", function (root, fileStats, next) { +process.on('uncaughtException', function(err) { + console.error('uncaughtException: ' + (err.stack || err.message || err.toString())); +}); - if(fileStats.name == "desc") { - next(); - } - else if(fileStats.name == "template.html") { - var lastDir = root.split('/'); - files[lastDir[lastDir.length - 1]].push({ template: fileStats.name}); - next(); - } else { - var lastDir = path.join(root).split('/'); - if(files.hasOwnProperty(lastDir[lastDir.length - 1])) { - files[lastDir[lastDir.length - 1]].push({ file: fileStats.name, head: '', text: ''}); - } - next(); - } -}).on("errors", function (root, nodeStatsArray, next) { - next(); -}).on("end", function () { +process.chdir(__dirname); +var config = require('./config'); - for(var slides in files) { - if(!files.hasOwnProperty(slides)) { continue; } - var data = fs.readFileSync(__dirname+'/slideshows/'+slides+'/desc'); - var descs = data.toString().split('\n'); - for(var file in slides) { - if(!files[slides].hasOwnProperty(file)) { continue; } - if(!files[slides][file].hasOwnProperty('file')) { continue; } - for(var desc in descs) { - if(!descs.hasOwnProperty(desc)) { continue; } - var info = descs[desc].split('|'); - if(info[0] === files[slides][file]['file']) { - files[slides][file]['head'] = info[1]; - files[slides][file]['text'] = info[2]; - } - } - } - } - 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( __dirname + '/slideshows'), // Serve all static files in the current dir. - function(req, res) { - var purl = url.parse(req.url, true); - var slideset = 'default'; - if(purl['query'].hasOwnProperty('slides')) { - slideset = purl['query']['slides']; - } - if(!files.hasOwnProperty(slideset)) { res.end(); return; } - - fs.readFile('./slideshows/'+slideset+'/template.html', function(err,data){ - if(err) { - res.end(body); - } - var body = data.toString(); - var slides = ''; - for(var file in files[slideset]) { - if (!files[slideset].hasOwnProperty(file)) { continue; } - if (!files[slideset][file].hasOwnProperty('file')) { continue; } - slides = slides+'<li><img src="/'+slideset+'/'+files[slideset][file]['file']+'">' - if(files[slideset][file]['text']) { - slides = slides+'<div class="transover"><h2>'+files[slideset][file]['head']+' - '+files[slideset][file]['text']+'</div></img></li>'; - } - } - body = body.replace("$IMAGES", slides); - res.setHeader('Content-Length', body.length); - res.end(body); - }); - } - ).listen(8080); -}); +var model = require('./model_' + config.modelBackend); +model.start(config.Config); + +var frontend = require('./frontend'); +frontend.setModel(model); +frontend.start(config.Config); |