Skip to content

Commit

Permalink
add winston logger, version to 1.7.0
Browse files Browse the repository at this point in the history
  • Loading branch information
djfun committed Sep 29, 2015
1 parent 8046964 commit 89c0508
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 21 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ node_modules/lazystream
node_modules/gulp-casperjs
node_modules/gulp-shell
node_modules/require-dir
node_modules/winston
assets/html/google*
public/*
lib/*
lib/*
log/*
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mc-map-item-tool [![Build Status](https://travis-ci.org/djfun/mc-map-item-tool.p

[nodejs](http://nodejs.org) webtool for converting images to Minecraft map items.

Other code/libraries used: [jquery](http://jquery.com/), [bootstrap](http://twitter.github.com/bootstrap/), [Colour.js](http://stevehanov.ca/blog/index.php?id=116), [cookies.js](https://github.com/ScottHamper/Cookies), [node-nbt](https://github.com/djfun/node-nbt), [archiver](https://github.com/ctalkington/node-archiver), [babel](https://github.com/babel/babel), [lazystream](https://github.com/jpommerening/node-lazystream), [moment](https://github.com/moment/moment), [node-static](https://github.com/cloudhead/node-static)
Other code/libraries used: [jquery](http://jquery.com/), [bootstrap](http://twitter.github.com/bootstrap/), [Colour.js](http://stevehanov.ca/blog/index.php?id=116), [cookies.js](https://github.com/ScottHamper/Cookies), [node-nbt](https://github.com/djfun/node-nbt), [archiver](https://github.com/ctalkington/node-archiver), [babel](https://github.com/babel/babel), [lazystream](https://github.com/jpommerening/node-lazystream), [moment](https://github.com/moment/moment), [node-static](https://github.com/cloudhead/node-static), [winston](https://github.com/winstonjs/winston)

How to setup and run your own server
------------------------------------
Expand Down
1 change: 1 addition & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ gulp.task('default', ['clean'], function() {
.pipe(gulp.dest(DEST_SERVER));

mkdirp.sync('public/tmp');
mkdirp.sync('log');

return merge(client, vendor, assets, assets_html, server);
});
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minecraft-map-item-tool",
"version": "1.6.0",
"version": "1.7.0",
"description": "web tool for converting images to minecraft maps",
"main": "lib/server.js",
"scripts": {
Expand Down Expand Up @@ -46,6 +46,7 @@
"lazystream": "^0.1.0",
"moment": "^2.9.0",
"node-nbt": "^1.0.0",
"node-static": "^0.7.6"
"node-static": "^0.7.6",
"winston": "^1.0.2"
}
}
56 changes: 39 additions & 17 deletions src/server/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,31 @@ var crypto = require('crypto');
var archiver = require('archiver');
var moment = require('moment');
var lazystream = require('lazystream');
var winston = require('winston');

var file = new (nodeStatic.Server)('./public');
var TAG = require('node-nbt').TAG;
var NbtReader = require('node-nbt').NbtReader;
var NbtWriter = require('node-nbt').NbtWriter;

winston.remove(winston.transports.Console);
winston.add(winston.transports.File, {
filename: './log/mc-map.log',
handleExceptions: true,
json: false,
maxsize: 99999,
timestamp: function() { return moment().format('YYYY-MM-DD HH:mm:ss'); }
});
if (process.env.NODE_ENV != 'production') {
winston.add(winston.transports.Console, {
handleExceptions: true,
json: false,
timestamp: function() { return moment().format('YYYY-MM-DD HH:mm:ss'); },
colorize: true,
stringify: true
});
}

var TMP_DIR = './public/tmp/';

var tmpFiles = {
Expand All @@ -40,10 +59,6 @@ var tmpFiles = {

var app;

function log(message) {
console.log(`${moment().format('YYYY-MM-DD HH:mm:ss')} ${message}`);
}

function deleteTmpFiles (file) {
fs.unlink(`${TMP_DIR}${file}`, function (err) {
if (err) {
Expand All @@ -52,7 +67,7 @@ function deleteTmpFiles (file) {
error.fs_error = err;
throw error;
} else {
log(`Successfully deleted /tmp/${file}`);
winston.log('info', `Successfully deleted /tmp/${file}`);
}
});
}
Expand All @@ -61,8 +76,10 @@ var handlers = {
logReferer(req) {
var referer = req.headers.referer;
if (referer) {
if (referer.indexOf(req.headers.host) !== 7) {
log(`Referer: ${referer}`);
var host = req.headers['x-forwarded-host'] ? req.headers['x-forwarded-host'] : req.headers.host;
if (referer.indexOf(host) !== 7) {
// only log external referers
winston.log('info', `Referer: ${referer}`);
}
}
},
Expand All @@ -71,7 +88,7 @@ var handlers = {
var {query, pathname} = url_parts;
if (tmpFiles.files[pathname.substr(5)]) {
try {
log(`Serve tmp file: ${pathname}`);
winston.log('info', `Serve tmp file: ${pathname}`);

let mapnumber = parseInt(query.mapnumber, 10) || 0;
let downloadfilename = pathname.slice(-3) == 'dat' ? `map_${mapnumber}.dat` : 'map_items.zip';
Expand All @@ -93,7 +110,7 @@ var handlers = {
var randomid;
try {
let decodedBody = querystring.parse(body);
// log(JSON.stringify(decodedBody));
winston.log('debug', 'decoded body', decodedBody);
map_item_array = JSON.parse(decodedBody.map_item);
x_center = parseInt(decodedBody.x_center, 10);
z_center = parseInt(decodedBody.z_center, 10);
Expand All @@ -102,7 +119,7 @@ var handlers = {
if (randomid !== "") {
randomid+= "_";
}
// log(map_item_array.length);
winston.log('debug', 'array length', map_item_array.length);
if (map_item_array.length == 16384) {
for (let element of map_item_array.values()) {
if (element > 127) {
Expand Down Expand Up @@ -178,7 +195,7 @@ var handlers = {
let writeStream = new lazystream.Writable(function () {
return fs.createWriteStream(`${TMP_DIR}${filename}.dat`)
.on('close', function () {
log(`Map file written to disk: ${filename}.dat`);
winston.log('info', `Map file written to disk: ${filename}.dat`);
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.end(filename);
Expand All @@ -201,7 +218,7 @@ var handlers = {
let output = fs.createWriteStream(`${TMP_DIR}${zipname}.zip`);
let archive = archiver('zip');
output.on('close', function() {
log(`Zip file written to disk: ${zipname}.zip`);
winston.log('info', `Zip file written to disk: ${zipname}.zip`);
res.setHeader('Content-Type', 'text/html');
res.writeHead(200);
res.end(zipname);
Expand Down Expand Up @@ -230,7 +247,7 @@ var handlers = {
error.archive_error = err;
throw error;
}
log(`Zip file finalized: ${bytes} total bytes`);
winston.log('info', `Zip file finalized: ${bytes} total bytes`);
});
} catch (e) {
handlers.handleError(e, res);
Expand All @@ -241,11 +258,10 @@ var handlers = {
res.writeHead(err.http_code || 500);
if (err.http_code == 400) {
res.end("Bad request");
log('Bad request');
log(err.toString());
winston.log('info', 'Bad request', err.toString());
} else {
res.end("Internal server error");
log(err.toString());
winston.log('info', 'Internal Server Error', err.toString());
}
}
};
Expand Down Expand Up @@ -284,9 +300,15 @@ function startup() {

app = http.createServer(handler);
app.listen(process.env.PORT || 8080, process.env.HOST);
log('Started mc-map-item-tool server');
winston.log('info', 'Started mc-map-item-tool server');
}

process.on("SIGTERM", function() {
winston.log('info', 'Received signal SIGTERM');
process.exit();
});


if (!module.parent) {
startup();
} else {
Expand Down

0 comments on commit 89c0508

Please sign in to comment.