3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-24 12:59:34 +01:00
dbot/modules/web.js
Alexander D Brown 52cdddf3f7 Added in URL support for the web interface.
The Jade templating code in `views/quotes.jade` will now check against the Regular Expression which has been added to `snippets.js` and is delivered to the template by `modules/web.js`.

The reguar expression used should be able to handle any URL and ensure no non-URL elements are made into URLs. It may be worth adding checking for 4XX error codes at a later date.

This shouldn't affect any of the other modules; features have only been added, not modified.
2011-11-09 23:51:01 +00:00

45 lines
1.2 KiB
JavaScript

var express = require('express');
var webInterface = function(dbot) {
var dbot = dbot;
var pub = 'public';
var app = express.createServer();
app.use(express.compiler({ src: pub, enable: ['sass'] }));
app.use(express.static(pub));
app.set('view engine', 'jade');
app.get('/', function(req, res) {
res.redirect('/quotes');
//res.render('index', { });
});
app.get('/quotes', function(req, res) {
// Lists the quote categories
res.render('quotelist', { 'quotelist': Object.keys(dbot.db.quoteArrs) });
});
app.get('/quotes/:key', function(req, res) {
// Lists the quotes in a category
var key = req.params.key.toLowerCase();
if(dbot.db.quoteArrs.hasOwnProperty(key)) {
res.render('quotes', { 'quotes': dbot.db.quoteArrs[key], locals: {url_regex: RegExp.prototype.url_regex()}});
} else {
res.render('error', { 'message': 'No quotes under that key.' });
}
});
app.listen(9443);
return {
'onDestroy': function() {
app.close();
}
};
};
exports.fetch = function(dbot) {
return webInterface(dbot);
};