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
* 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 fs = require("fs");
var path = require("path");
var $util = require("./utilities");
module.exports = function (Server) {
@ -65,7 +66,7 @@ module.exports = function (Server) {
app.get("/api/channels/:channel", function (req, res) {
Server.stats.record("api", "/api/channels/:channel");
var name = req.params.channel;
if(!name.match(/^[\w-_]+$/)) {
if(!$util.isValidChannelName(name)) {
res.send(404);
return;
}
@ -483,8 +484,7 @@ module.exports = function (Server) {
}
if(email.match(/.*@(localhost|127\.0\.0\.1)/i)) {
res.jsonp({
success: false,
res.jsonp({ success: false,
error: "Nice try, but no"
});
return;
@ -597,10 +597,11 @@ module.exports = function (Server) {
return;
}
var start = data.size - len;
if(start < 0) {
if(start < 0)
start = 0;
}
var end = data.size - 1;
if(end < 0)
end = 0;
fs.createReadStream(file, { start: start, end: end })
.pipe(res);
});
@ -630,7 +631,7 @@ module.exports = function (Server) {
return;
}
pipeLast(res, "sys.log", 1048576);
pipeLast(res, path.join(__dirname, "../sys.log"), 1048576);
});
});
@ -658,7 +659,7 @@ module.exports = function (Server) {
return;
}
pipeLast(res, "error.log", 1048576);
pipeLast(res, path.join(__dirname, "../error.log"), 1048576);
});
});
@ -692,9 +693,11 @@ module.exports = function (Server) {
return;
}
fs.exists("chanlogs/" + chan + ".log", function(exists) {
fs.exists(path.join(__dirname, "../chanlogs", chan + ".log"),
function(exists) {
if(exists) {
pipeLast(res, "chanlogs/" + chan + ".log", 1048576);
pipeLast(res, path.join(__dirname, "../chanlogs",
chan + ".log"), 1048576);
} else {
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 path = require("path");
var Poll = require("./poll.js").Poll;
var Media = require("./media.js").Media;
var Logger = require("./logger.js");
@ -101,7 +102,8 @@ var Channel = function(name, Server) {
self.ip_alias = {};
self.name_alias = {};
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.time = new Date().getTime();
self.plmeta = {
@ -146,7 +148,8 @@ Channel.prototype.loadDump = function() {
var self = this;
if(self.name === "")
return;
fs.stat("chandump/" + self.name, function (err, stats) {
fs.stat(path.join(__dirname, "../chandump", self.name),
function (err, stats) {
if(!err) {
var mb = stats.size / 1048576;
mb = parseInt(mb * 100) / 100;
@ -160,7 +163,8 @@ Channel.prototype.loadDump = function() {
return;
}
}
fs.readFile("chandump/" + self.name, function(err, data) {
fs.readFile(path.join(__dirname, "../chandump", self.name),
function(err, data) {
if(err) {
if(err.code == "ENOENT") {
Logger.errlog.log("WARN: missing dump for " + self.name);
@ -286,7 +290,7 @@ Channel.prototype.saveDump = function() {
js: this.js
};
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

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 path = require("path");
function getTimeString() {
var d = new Date();
@ -51,8 +52,8 @@ Logger.prototype.close = function () {
}
}
var errlog = new Logger("error.log");
var syslog = new Logger("sys.log");
var errlog = new Logger(path.join(__dirname, "../error.log"));
var syslog = new Logger(path.join(__dirname, "../sys.log"));
errlog.actualLog = errlog.log;
errlog.log = function(what) { console.log(what); this.actualLog(what); }
syslog.actualLog = syslog.log;

View File

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

2
run.sh
View File

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

View File

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