3
0
mirror of https://github.com/reality/dbot.git synced 2024-12-26 04:32:37 +01:00
dbot/modules/web/web.js

76 lines
2.3 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) {
2013-04-14 19:21:24 +02:00
this.pub = 'public';
this.app = express();
2013-04-14 19:21:24 +02:00
this.app.use(express.static(this.pub));
this.app.set('view engine', 'jade');
2013-01-23 00:29:49 +01:00
2013-04-14 19:21:24 +02:00
var server = this.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;
2013-04-14 19:21:24 +02:00
this.app.get(p, (function(req, resp) {
2012-12-24 06:47:47 +01:00
// 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.onLoad = function() {
var routes = _.pluck(dbot.modules.web.app.routes.get, 'path');
var moduleNames = _.keys(dbot.modules);
var indexModules = [];
_.each(moduleNames, function(moduleName) {
var modulePath = '/' + moduleName;
if(_.include(routes, modulePath)) {
indexModules.push(moduleName);
}
});
console.log(indexModules);
// TODO: get list of loaded modules
this.app.get('/', function(req, res) {
res.render('index', {
'name': dbot.config.name,
'routes': indexModules
});
});
}.bind(this);
this.onDestroy = function() {
server.close();
};
this.api = {
'getUrl': function(path) {
if(path.charAt(0) == '/') path = path.substr(1);
if(this.config.externalPath) {
return this.config.externalPath + '/' + path;
} else {
2013-04-14 05:53:53 +02:00
return 'http://' + this.config.webHost + ':' + this.config.webPort + '/' + path;
}
2013-04-13 20:38:53 +02:00
}
};
};
exports.fetch = function(dbot) {
return new webInterface(dbot);
};