dbot/modules/web/web.js

56 lines
1.6 KiB
JavaScript
Raw Normal View History

2012-12-24 06:47:47 +01:00
var express = require('express'),
2013-01-13 16:51:54 +01:00
_ = require('underscore')._,
2012-12-24 06:47:47 +01:00
fs = require('fs');
var webInterface = function(dbot) {
var pub = 'public';
var app = express();
app.use(express.static(pub));
app.set('view engine', 'jade');
app.get('/', function(req, res) {
res.render('index', { 'name': dbot.config.name });
});
2013-01-23 00:29:49 +01:00
var server = app.listen(dbot.config.web.webPort);
this.reloadPages = function() {
var pages = dbot.pages;
2012-12-24 06:47:47 +01:00
for(var p in pages) {
2013-01-13 16:51:54 +01:00
if(_.has(pages, p)) {
2012-12-24 06:47:47 +01:00
var func = pages[p];
var mod = func.module;
app.get(p, (function(req, resp) {
// Crazy shim to seperate module views.
var shim = Object.create(resp);
shim.render = (function(view, one, two) {
// Render with express.js
resp.render(this.module + '/' + view, one, two);
2012-12-24 06:47:47 +01:00
}).bind(this);
shim.render_core = resp.render;
this.call(this.module, req, shim);
}).bind(func));
}
}
}.bind(this);
this.onDestroy = function() {
server.close();
};
this.api = {
'getUrl': function(path) {
if(this.config.externalPath) {
return this.config.externalPath + '/' + path;
} else {
return 'http://' + this.config.webHost + ':' + port + '/' + path;
}
};
};
};
exports.fetch = function(dbot) {
return new webInterface(dbot);
};