Move server files to lib/ to clean up root directory

This commit is contained in:
calzoneman 2013-09-05 13:48:05 -05:00
parent 7b54b2fcc0
commit 7840fa35e8
26 changed files with 83 additions and 51 deletions

6
.gitignore vendored Normal file
View File

@ -0,0 +1,6 @@
*.swp
cfg.json
chandump
chanlogs
*.log
node_modules

View File

@ -1,3 +1,14 @@
Thu Sep 5 13:45 2013 CDT
* acp.js, actionlog.js, api.js, channel.js, chatcommand.js, config.js,
customembed.js, database.js, filter.js, get-info.js, logger.js,
media.js, notwebsocket.js, playlist.js, poll.js, rank.js, server.js,
stats.js, ullist.js, user.js, utilities.js: move server
files into lib/ folder to clean up the root directory of the project.
* api.js: replace regex with $util.isValidChannelName (L68);
fix relative file paths (per moving api.js to lib/)
* server.js: fix relative file paths
* channel.js: fix relative file paths
Wed Sep 4 22:45 2013 CDT Wed Sep 4 22:45 2013 CDT
* changelog: initialize changelog file * changelog: initialize changelog file

BIN
lib/.logger.js.swp Normal file

Binary file not shown.

View File

View File

@ -11,6 +11,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
var Logger = require("./logger"); var Logger = require("./logger");
var fs = require("fs"); var fs = require("fs");
var path = require("path");
var $util = require("./utilities"); var $util = require("./utilities");
module.exports = function (Server) { module.exports = function (Server) {
@ -60,16 +61,16 @@ module.exports = function (Server) {
}); });
/* REGION channels */ /* REGION channels */
/* data about a specific channel */ /* data about a specific channel */
app.get("/api/channels/:channel", function (req, res) { app.get("/api/channels/:channel", function (req, res) {
Server.stats.record("api", "/api/channels/:channel"); Server.stats.record("api", "/api/channels/:channel");
var name = req.params.channel; var name = req.params.channel;
if(!name.match(/^[\w-_]+$/)) { if(!$util.isValidChannelName(name)) {
res.send(404); res.send(404);
return; return;
} }
var data = { var data = {
name: name, name: name,
loaded: false loaded: false
@ -125,7 +126,7 @@ module.exports = function (Server) {
} }
// If we get here, the filter is public channels // If we get here, the filter is public channels
var channels = []; var channels = [];
for(var key in Server.channels) { for(var key in Server.channels) {
var channel = Server.channels[key]; var channel = Server.channels[key];
@ -170,11 +171,11 @@ module.exports = function (Server) {
}); });
return; return;
} }
// Only record login-success for admins // Only record login-success for admins
if(row.global_rank >= 255) if(row.global_rank >= 255)
ActionLog.record(getIP(req), name, "login-success"); ActionLog.record(getIP(req), name, "login-success");
res.jsonp({ res.jsonp({
success: true, success: true,
name: name, name: name,
@ -201,7 +202,7 @@ module.exports = function (Server) {
}); });
return; return;
} }
if(toomany) { if(toomany) {
ActionLog.record(ip, name, "register-failure", ActionLog.record(ip, name, "register-failure",
"Too many recent registrations"); "Too many recent registrations");
@ -225,7 +226,7 @@ module.exports = function (Server) {
if(!$util.isValidUserName(name)) { if(!$util.isValidUserName(name)) {
ActionLog.record(ip, name, "register-failure", ActionLog.record(ip, name, "register-failure",
"Invalid name"); "Invalid name");
res.jsonp({ res.jsonp({
success: false, success: false,
@ -338,11 +339,11 @@ module.exports = function (Server) {
} }
var msg = "A password reset request was issued for your " + var msg = "A password reset request was issued for your " +
"account '"+ name + "' on " + Server.cfg["domain"] + "account '"+ name + "' on " + Server.cfg["domain"] +
". This request is valid for 24 hours. If you did "+ ". This request is valid for 24 hours. If you did "+
"not initiate this, there is no need to take action."+ "not initiate this, there is no need to take action."+
" To reset your password, copy and paste the " + " To reset your password, copy and paste the " +
"following link into your browser: " + "following link into your browser: " +
Server.cfg["domain"] + "/reset.html?"+hash; Server.cfg["domain"] + "/reset.html?"+hash;
var mail = { var mail = {
@ -435,7 +436,7 @@ module.exports = function (Server) {
}); });
return; return;
} }
db.setUserProfile(name, { image: img, text: text }, db.setUserProfile(name, { image: img, text: text },
function (err, dbres) { function (err, dbres) {
if(err) { if(err) {
@ -483,8 +484,7 @@ module.exports = function (Server) {
} }
if(email.match(/.*@(localhost|127\.0\.0\.1)/i)) { if(email.match(/.*@(localhost|127\.0\.0\.1)/i)) {
res.jsonp({ res.jsonp({ success: false,
success: false,
error: "Nice try, but no" error: "Nice try, but no"
}); });
return; return;
@ -583,7 +583,7 @@ module.exports = function (Server) {
ActionLog.listActions(types, function (err, actions) { ActionLog.listActions(types, function (err, actions) {
if(err) if(err)
actions = []; actions = [];
res.jsonp(actions); res.jsonp(actions);
}); });
}); });
@ -597,10 +597,11 @@ module.exports = function (Server) {
return; return;
} }
var start = data.size - len; var start = data.size - len;
if(start < 0) { if(start < 0)
start = 0; start = 0;
}
var end = data.size - 1; var end = data.size - 1;
if(end < 0)
end = 0;
fs.createReadStream(file, { start: start, end: end }) fs.createReadStream(file, { start: start, end: end })
.pipe(res); .pipe(res);
}); });
@ -630,7 +631,7 @@ module.exports = function (Server) {
return; return;
} }
pipeLast(res, "sys.log", 1048576); pipeLast(res, path.join(__dirname, "../sys.log"), 1048576);
}); });
}); });
@ -658,7 +659,7 @@ module.exports = function (Server) {
return; return;
} }
pipeLast(res, "error.log", 1048576); pipeLast(res, path.join(__dirname, "../error.log"), 1048576);
}); });
}); });
@ -692,9 +693,11 @@ module.exports = function (Server) {
return; return;
} }
fs.exists("chanlogs/" + chan + ".log", function(exists) { fs.exists(path.join(__dirname, "../chanlogs", chan + ".log"),
function(exists) {
if(exists) { if(exists) {
pipeLast(res, "chanlogs/" + chan + ".log", 1048576); pipeLast(res, path.join(__dirname, "../chanlogs",
chan + ".log"), 1048576);
} else { } else {
res.send(404); res.send(404);
} }

View File

@ -11,6 +11,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
*/ */
var fs = require("fs"); var fs = require("fs");
var path = require("path");
var Poll = require("./poll.js").Poll; var Poll = require("./poll.js").Poll;
var Media = require("./media.js").Media; var Media = require("./media.js").Media;
var Logger = require("./logger.js"); var Logger = require("./logger.js");
@ -101,7 +102,8 @@ var Channel = function(name, Server) {
self.ip_alias = {}; self.ip_alias = {};
self.name_alias = {}; self.name_alias = {};
self.login_hist = []; self.login_hist = [];
self.logger = new Logger.Logger("chanlogs/" + self.canonical_name + ".log"); self.logger = new Logger.Logger(path.join(__dirname, "../chanlogs",
self.canonical_name + ".log"));
self.i = 0; self.i = 0;
self.time = new Date().getTime(); self.time = new Date().getTime();
self.plmeta = { self.plmeta = {
@ -146,7 +148,8 @@ Channel.prototype.loadDump = function() {
var self = this; var self = this;
if(self.name === "") if(self.name === "")
return; return;
fs.stat("chandump/" + self.name, function (err, stats) { fs.stat(path.join(__dirname, "../chandump", self.name),
function (err, stats) {
if(!err) { if(!err) {
var mb = stats.size / 1048576; var mb = stats.size / 1048576;
mb = parseInt(mb * 100) / 100; mb = parseInt(mb * 100) / 100;
@ -154,13 +157,14 @@ Channel.prototype.loadDump = function() {
Logger.errlog.log("Large chandump detected: " + self.name + Logger.errlog.log("Large chandump detected: " + self.name +
" (" + mb + " MB)"); " (" + mb + " MB)");
self.updateMotd("Your channel file has exceeded the " + self.updateMotd("Your channel file has exceeded the " +
"maximum size of 1MB and cannot be " + "maximum size of 1MB and cannot be " +
"loaded. Please ask an administrator " + "loaded. Please ask an administrator " +
"for assistance in restoring it."); "for assistance in restoring it.");
return; return;
} }
} }
fs.readFile("chandump/" + self.name, function(err, data) { fs.readFile(path.join(__dirname, "../chandump", self.name),
function(err, data) {
if(err) { if(err) {
if(err.code == "ENOENT") { if(err.code == "ENOENT") {
Logger.errlog.log("WARN: missing dump for " + self.name); Logger.errlog.log("WARN: missing dump for " + self.name);
@ -286,7 +290,7 @@ Channel.prototype.saveDump = function() {
js: this.js js: this.js
}; };
var text = JSON.stringify(dump); var text = JSON.stringify(dump);
fs.writeFileSync("chandump/" + this.name, text); fs.writeFileSync(path.join(__dirname, "../chandump", this.name), text);
} }
// Save channel dumps every 5 minutes, in case of crash // Save channel dumps every 5 minutes, in case of crash
@ -398,7 +402,7 @@ Channel.prototype.tryRegister = function (user) {
return; return;
} }
self.server.actionlog.record(user.ip, user.name, self.server.actionlog.record(user.ip, user.name,
"channel-register-success", self.name); "channel-register-success", self.name);
self.registered = true; self.registered = true;
self.initialized = true; self.initialized = true;
@ -438,7 +442,7 @@ Channel.prototype.unregister = function (user) {
}); });
return; return;
} }
self.registered = false; self.registered = false;
user.socket.emit("unregisterChannel", { success: true }); user.socket.emit("unregisterChannel", { success: true });
}); });
@ -638,7 +642,7 @@ Channel.prototype.tryIPBan = function(actor, name, range) {
} }
self.ipbans[ip] = [name, actor.name]; self.ipbans[ip] = [name, actor.name];
self.logger.log("*** " + actor.name + " banned " + ip + self.logger.log("*** " + actor.name + " banned " + ip +
" (" + name + ")"); " (" + name + ")");
for(var i = 0; i < self.users.length; i++) { for(var i = 0; i < self.users.length; i++) {
@ -1271,7 +1275,7 @@ Channel.prototype.tryQueue = function(user, data) {
Channel.prototype.addMedia = function(data, user) { Channel.prototype.addMedia = function(data, user) {
var self = this; var self = this;
if(data.type === "yp" && if(data.type === "yp" &&
!self.hasPermission(user, "playlistaddlist")) { !self.hasPermission(user, "playlistaddlist")) {
user.socket.emit("queueFail", "You don't have permission to add " + user.socket.emit("queueFail", "You don't have permission to add " +
"playlists"); "playlists");
@ -1488,7 +1492,7 @@ Channel.prototype.tryDequeue = function(user, data) {
if(typeof data !== "number") if(typeof data !== "number")
return; return;
var plitem = this.playlist.items.find(data); var plitem = this.playlist.items.find(data);
if(plitem && plitem.media) if(plitem && plitem.media)
this.logger.log("### " + user.name + " deleted " + plitem.media.title); this.logger.log("### " + user.name + " deleted " + plitem.media.title);
@ -1508,7 +1512,7 @@ Channel.prototype.tryUncache = function(user, data) {
if(err) if(err)
return; return;
self.logger.log("*** " + user.name + " deleted " + data.id + self.logger.log("*** " + user.name + " deleted " + data.id +
" from library"); " from library");
}); });
} }
@ -1619,12 +1623,12 @@ Channel.prototype.move = function(data, user) {
if(typeof data.moveby !== "undefined") if(typeof data.moveby !== "undefined")
moveby = data.moveby; moveby = data.moveby;
var fromit = chan.playlist.items.find(data.from); var fromit = chan.playlist.items.find(data.from);
var afterit = chan.playlist.items.find(data.after); var afterit = chan.playlist.items.find(data.after);
var aftertitle = afterit && afterit.media ? afterit.media.title : ""; var aftertitle = afterit && afterit.media ? afterit.media.title : "";
if(fromit) { if(fromit) {
chan.logger.log("### " + user.name + " moved " + fromit.media.title chan.logger.log("### " + user.name + " moved " + fromit.media.title
+ (aftertitle ? " after " + aftertitle : "")); + (aftertitle ? " after " + aftertitle : ""));
} }
@ -2066,7 +2070,7 @@ Channel.prototype.trySetRank = function(user, data) {
receiver.rank = data.rank; receiver.rank = data.rank;
if(receiver.loggedIn) { if(receiver.loggedIn) {
self.saveRank(receiver, function (err, res) { self.saveRank(receiver, function (err, res) {
self.logger.log("*** " + user.name + " set " + self.logger.log("*** " + user.name + " set " +
data.user + "'s rank to " + data.rank); data.user + "'s rank to " + data.rank);
self.sendAllWithPermission("acl", "setChannelRank", data); self.sendAllWithPermission("acl", "setChannelRank", data);
}); });
@ -2080,8 +2084,8 @@ Channel.prototype.trySetRank = function(user, data) {
return; return;
self.server.db.setChannelRank(self.name, data.user, self.server.db.setChannelRank(self.name, data.user,
data.rank, function (err, res) { data.rank, function (err, res) {
self.logger.log("*** " + user.name + " set " + self.logger.log("*** " + user.name + " set " +
data.user + "'s rank to " + data.rank); data.user + "'s rank to " + data.rank);
self.sendAllWithPermission("acl", "setChannelRank", data); self.sendAllWithPermission("acl", "setChannelRank", data);
}); });

View File

@ -10,6 +10,7 @@ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLI
*/ */
var fs = require("fs"); var fs = require("fs");
var path = require("path");
function getTimeString() { function getTimeString() {
var d = new Date(); var d = new Date();
@ -51,8 +52,8 @@ Logger.prototype.close = function () {
} }
} }
var errlog = new Logger("error.log"); var errlog = new Logger(path.join(__dirname, "../error.log"));
var syslog = new Logger("sys.log"); var syslog = new Logger(path.join(__dirname, "../sys.log"));
errlog.actualLog = errlog.log; errlog.actualLog = errlog.log;
errlog.log = function(what) { console.log(what); this.actualLog(what); } errlog.log = function(what) { console.log(what); this.actualLog(what); }
syslog.actualLog = syslog.log; syslog.actualLog = syslog.log;

View File

@ -96,7 +96,8 @@ var Server = {
this.db = new Database(self.cfg); this.db = new Database(self.cfg);
this.db.init(); this.db.init();
this.actionlog = require("./actionlog")(self); this.actionlog = require("./actionlog")(self);
this.httpaccess = new Logger.Logger("httpaccess.log"); this.httpaccess = new Logger.Logger(path.join(__dirname,
"../httpaccess.log"));
this.app = express(); this.app = express();
this.app.use(express.bodyParser()); this.app.use(express.bodyParser());
// channel path // channel path
@ -108,7 +109,9 @@ var Server = {
else { else {
self.stats.record("http", "/r/" + c); self.stats.record("http", "/r/" + c);
self.logHTTP(req); self.logHTTP(req);
res.sendfile(__dirname + "/www/channel.html"); res.sendfile("channel.html", {
root: path.join(__dirname, "../www")
});
} }
}); });
@ -118,13 +121,15 @@ var Server = {
self.app.get("/", function (req, res, next) { self.app.get("/", function (req, res, next) {
self.logHTTP(req); self.logHTTP(req);
self.stats.record("http", "/"); self.stats.record("http", "/");
res.sendfile(__dirname + "/www/index.html"); res.sendfile("index.html", {
root: path.join(__dirname, "../www")
});
}); });
// default path // default path
self.app.get("/:thing(*)", function (req, res, next) { self.app.get("/:thing(*)", function (req, res, next) {
var opts = { var opts = {
root: __dirname + "/www", root: path.join(__dirname, "../www"),
maxAge: self.cfg["asset-cache-ttl"] maxAge: self.cfg["asset-cache-ttl"]
} }
res.sendfile(req.params.thing, opts, function (err) { res.sendfile(req.params.thing, opts, function (err) {
@ -231,15 +236,17 @@ var Server = {
Logger.syslog.log("Starting CyTube v" + VERSION); Logger.syslog.log("Starting CyTube v" + VERSION);
fs.exists("chanlogs", function (exists) { var chanlogpath = path.join(__dirname, "../chanlogs");
exists || fs.mkdir("chanlogs"); fs.exists(chanlogpath, function (exists) {
exists || fs.mkdir(chanlogpath);
}); });
fs.exists("chandump", function (exists) { var chandumppath = path.join(__dirname, "../chandump");
exists || fs.mkdir("chandump"); fs.exists(chandumppath, function (exists) {
exists || fs.mkdir(chandumppath);
}); });
Config.load(Server, "cfg.json", function () { Config.load(Server, path.join(__dirname, "../cfg.json"), function () {
Server.init(); Server.init();
if(!Server.cfg["debug"]) { if(!Server.cfg["debug"]) {
process.on("uncaughtException", function (err) { process.on("uncaughtException", function (err) {

2
run.sh
View File

@ -2,7 +2,7 @@
while : while :
do do
node server.js node lib/server.js
sleep 2 sleep 2
done done

View File

@ -1,5 +1,5 @@
var Config = require("./config.js"); var Config = require("./lib/config.js");
var Database = require("./database.js"); var Database = require("./lib/database.js");
var updates = { var updates = {
"2013-08-20-utf8fix": fixDBUnicode, "2013-08-20-utf8fix": fixDBUnicode,