3
0
mirror of https://github.com/reality/dbot.git synced 2024-11-30 16:09:27 +01:00
dbot/modules/web.js

56 lines
1.6 KiB
JavaScript
Raw Normal View History

2011-09-13 22:23:46 +02:00
var express = require('express');
// Web interface module using the express framework
2011-09-13 22:23:46 +02:00
var webInterface = function(dbot) {
var dbot = dbot;
2011-11-08 19:59:29 +01:00
var pub = 'public';
2011-09-13 22:55:20 +02:00
var app = express.createServer();
app.use(express.compiler({ src: pub, enable: ['sass'] }));
app.use(express.static(pub));
app.set('view engine', 'jade');
2011-09-13 22:23:46 +02:00
app.get('/', function(req, res) {
2011-11-08 16:36:18 +01:00
res.redirect('/quotes');
//res.render('index', { });
});
// Lists the quote categories
2011-11-08 16:36:18 +01:00
app.get('/quotes', function(req, res) {
2012-03-10 18:49:57 +01:00
res.render('quotelist', { 'name': dbot.name, 'quotelist': Object.keys(dbot.db.quoteArrs) });
2011-09-13 22:23:46 +02:00
});
2011-09-15 13:30:52 +02:00
// Lists quotes in a category
2011-09-15 13:30:52 +02:00
app.get('/quotes/:key', function(req, res) {
2011-09-15 13:34:01 +02:00
var key = req.params.key.toLowerCase();
if(dbot.db.quoteArrs.hasOwnProperty(key)) {
2012-03-10 18:49:57 +01:00
res.render('quotes', { 'name': dbot.name, 'quotes': dbot.db.quoteArrs[key], locals: { 'url_regex': RegExp.prototype.url_regex() } });
2011-09-15 13:30:52 +02:00
} else {
2012-03-10 18:49:57 +01:00
res.render('error', { 'name': dbot.name, 'message': 'No quotes under that key.' });
2011-09-15 13:30:52 +02:00
}
});
// Load random quote category page
2012-02-11 16:55:14 +01:00
app.get('/rq', function(req, res) {
var rCategory = Object.keys(dbot.db.quoteArrs).random();
2012-03-10 18:49:57 +01:00
res.render('quotes', { 'name': dbot.name, 'quotes': dbot.db.quoteArrs[rCategory], locals: { 'url_regex': RegExp.prototype.url_regex() } });
});
2011-11-08 16:36:18 +01:00
2012-03-10 19:21:50 +01:00
app.listen(dbot.webPort);
2011-09-14 18:24:32 +02:00
return {
'onDestroy': function() {
2011-09-14 18:46:59 +02:00
app.close();
},
'name': 'web',
'ignorable': false
2011-09-14 18:24:32 +02:00
};
2011-09-13 22:23:46 +02:00
};
exports.fetch = function(dbot) {
return webInterface(dbot);
};