-
-
Save sudodoki/530fadbed23d3545d167 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var cheerio = require('cheerio'); | |
var Chance = require('chance'); | |
var chance = new Chance(); | |
function getRandomData(type, hint) { | |
if (type == 'number') { | |
return (Math.random() * 1000000 | 0) / 100; | |
} else if (type == 'boolean') { | |
return Math.random() >= 0.5; | |
} else { | |
if (hint == 'name') | |
return chance.name() | |
if (hint == 'address') | |
return chance.address() | |
else | |
return chance.word() | |
} | |
} | |
function buildListModel(list, size, $, done) { | |
var columns = []; | |
list.each(function (n, e) { | |
var elem = $(e); | |
var bind = elem.attr('bind'); | |
var type = elem.attr('type'); | |
//console.log('e:', bind,type); | |
var column = { bind: bind, type: type}; | |
columns.push(column); | |
}) | |
var rows = []; | |
for (var i = 0; i < size; i++) { | |
var row = {}; | |
for (var j = 0; j < columns.length; j++) { | |
row[columns[j].bind] = getRandomData(columns[j].type, columns[j].bind); | |
} | |
row.id = (Math.random()*10000)|0; | |
rows.push(row); | |
} | |
done(rows); | |
} | |
function buildFormModel(form, $, done) { | |
var model = {}; | |
form.each(function (n, e) { | |
if (e.name=='form-section') | |
$(e).children().each(function (n, e2) { | |
var elem = $(e2); | |
var bind = elem.attr('bind'); | |
if (bind) { | |
if (bind.split('.')[0]=='data') bind = bind.split('.').slice(1).join('.'); | |
// console.log("bind",bind,"for",e2) | |
model[bind] = ''; | |
} else { | |
console.log("Elemen of form section without bind:", e2.name); | |
} | |
}); | |
}) | |
done(model); | |
} | |
function buildForModel(model, size, done) { | |
fs.readFile(model, function (err, data) { | |
if (err) throw err; | |
var $ = cheerio.load(data); | |
var children = $('list-model').children(); | |
if (children.length != 0) return buildListModel(children,size, $, done); | |
children = $('form-model').children(); | |
if (children.length != 0) return buildFormModel(children, $, done); | |
else return done({unknown:true}); | |
}); | |
} | |
/* | |
buildForModel('view-models/list-model.html', function(data) { | |
console.log(JSON.stringify(data)); | |
});*/ | |
var cache = {}; | |
function middleware(req, res, next) { | |
var url = req.url; | |
if (url.substr(0, 9) == '/samples/') { | |
//var model = url.substr(9); | |
var parts = url.substr(9).split('?'); | |
var model = parts.shift(); | |
//console.log("builind sample for ", model); | |
var params = { size: 30 }; | |
parts.join('?').split('&').forEach(function(pair) { | |
var pairs = pair.split('='); | |
params[pairs[0]] = pairs[1]; | |
}); | |
var file = 'view-models/' + model + '-model.html'; | |
if (!fs.existsSync(file)) { | |
res.statusCode = 404; | |
res.end('Model not found'); | |
return; | |
} | |
res.setHeader('Content-Type', 'application/json'); | |
var stat = fs.statSync(file); | |
if (cache[model] && cache[model].mtime.getTime() == stat.mtime.getTime()) { | |
res.end(cache[model].data); | |
} else | |
buildForModel(file, params.size|0, function (data) { | |
if (model) { | |
res.end(JSON.stringify(data)); | |
cache[model] = { | |
data: JSON.stringify(data), | |
mtime: stat.mtime | |
}; | |
} else res.end("{}"); | |
}); | |
} else | |
next(); | |
} | |
module.exports = middleware; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment