view modules/mod_http.js @ 51:22b06eade9a7

Fix typo error.
author Sonny Piers <sonny.piers@gmail.com>
date Mon, 21 Mar 2011 15:40:32 +0100
parents 0d3f18bb1d36
children 99bd1d1ac071
line wrap: on
line source

/*
 *  Copyright (C) 2010  Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
 *
 *  This file is part of PSĜS, a PubSub server written in JavaScript.
 *
 *  PSĜS is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 *  published by the Free Software Foundation, either version 3 of the
 *  License.
 *
 *  PSĜS is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with PSĜS.  If not, see <http://www.gnu.org/licenses/>.
 */

'use strict';

var sites = {};

var http = require('http');

var fs = require('fs');
var dir = require('./configuration').pluginsDir;
var files = fs.readdirSync(dir + '/http');
var regex = /^mod_.*\.js/;

var Script = process.binding('evals').Script;
var sandbox = {
	config: require('./configuration'),
	xmpp: require('xmpp'),
	errors: require('./errors'),
	forms: require('./forms'),
	makeError: require('./errors').makeError,
	notifs: require('./notifs'),
	NS: require('./namespaces'),
	require: require,
	storage: require('./storage'),
	toBare: JID.toBare,
	exports: {}
};

for (var i in files) {
	var file = files[i];
	if (!regex.test(file))
		continue;

	fs.readFile(dir + '/http/' + file, function(err, data) {
		if (err) {
			console.log('ERROR: loading http module “'+file+'”.'); // FIXME: file is always the last file of the list…
			return;
		}

		Script.runInNewContext(data, sandbox, dir + '/http/' + file); // FIXME: the same.
		var module = sandbox.exports;
		sandbox.exports = {};

		for (var j in module) {
			if (typeof sites[j] != 'undefined')
				console.log('WARNING: module http/'+j+' already loaded.');

			sites[j] = module[j];
		}
	});
}

http.createServer(function (req, res) {
	var sent = false;
	for (var i in sites) {
		var site = sites[i];
		if (!site.url.test(req.url))
			continue;

		sent = site.func(req, res);
		return;
	}
	if (!sent) {
		res.writeHead(404, {'Content-Type': 'text/plain'});
		res.end('Error 404');
	}
}).listen(8484);