Skip to content

Commit

Permalink
added config file support
Browse files Browse the repository at this point in the history
  • Loading branch information
inolen committed Aug 27, 2013
1 parent c3773f1 commit 19dc0fe
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 30 deletions.
67 changes: 56 additions & 11 deletions bin/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ var http = require('http');
var opt = require('optimist');
var path = require('path');
// var Throttle = require('throttle');
var zlib = require('zlib');

var argv = require('optimist')
.options({
'config': {
'description': 'Location of optional configuration file'
},
'root': {
'description': 'Root assets path',
'demand': true
Expand Down Expand Up @@ -103,21 +107,43 @@ function generateManifest(callback) {
getMods(function (err, mods) {
if (err) return callback(err);

async.concat(mods, getModFiles, function (err, files) {
async.concatSeries(mods, getModFiles, function (err, files) {
if (err) return callback(err);

async.map(files, function (file, cb) {
fs.stat(file, function (err, stat) {
if (err) return cb(err);
async.mapSeries(files, function (file, cb) {
console.log('processing ' + file);

var length = 0;
var sum = crypto.createHash('md5');

// stream each file in, generating a hash for it's original
// contents, and gzip'ing the buffer to determine the compressed
// length for the client so it can present accurate progress info
var stream = fs.createReadStream(file);

// gzip the file contents to determine the compressed length
// of the file so the client can present correct progress info
var gzip = zlib.createGzip();

checksum(file, function (err, checksum) {
if (err) return cb(err);
stream.on('error', function (err) {
callback(err);
});
stream.on('data', function (data) {
gzip.write(data);
sum.update(data);
});
stream.on('end', function () {
gzip.end();
});

cb(null, {
name: path.relative(argv.root, file),
size: stat.size,
checksum: checksum
});
gzip.on('data', function (data) {
length += data.length;
});
gzip.on('end', function () {
cb(null, {
name: path.relative(argv.root, file),
size: length,
checksum: sum.digest('hex')
});
});
}, function (err, entries) {
Expand Down Expand Up @@ -158,7 +184,26 @@ function handlePak(req, res, next) {
// sendfile2(absolutePath, res);
}

function loadConfig() {
if (!argv.config) {
return;
}

try {
console.log('Loading config file from ' + argv.config + '..');
var data = require(argv.config);
_.extend(config, data);
} catch (e) {
console.log('Failed to load config', e);
}

return config;
}

(function main() {
var config = loadConfig();
if (config) _.extend(argv, config);

// Setup the express app.
var app = express();
app.use(function (req, res, next) {
Expand Down
46 changes: 27 additions & 19 deletions bin/master.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,15 @@ var WebSocketServer = require('ws').Server;
var cursor = ansi(process.stdout);

var argv = require('optimist')
.describe('config', 'Location of the configuration file').default('config', './master.json')
.options({
'config': {
'description': 'Location of optional configuration file'
},
'port': {
'description': 'Server port',
'default': 9000
}
})
.argv;

if (argv.h || argv.help) {
Expand Down Expand Up @@ -255,23 +263,6 @@ function removeClient(conn) {
* main
*
**********************************************************/
function loadConfig() {
var config = {
port: 45735
};

try {
cursor.write('loading config file from ' + argv.config + '.. ');
var data = require(argv.config);
_.extend(config, data);
cursor.write('ok\n');
} catch (e) {
cursor.write('error\n');
}

return config;
}

function getRemoteAddress(ws) {
// By default, check the underlying socket's remote address.
var address = ws._socket.remoteAddress;
Expand Down Expand Up @@ -301,8 +292,25 @@ function connection(ws) {
this.port = getRemotePort(ws);
}

function loadConfig() {
if (!argv.config) {
return;
}

try {
console.log('Loading config file from ' + argv.config + '..');
var data = require(argv.config);
_.extend(config, data);
} catch (e) {
console.log('Failed to load config', e);
}

return config;
}

(function main() {
var config = loadConfig();
if (config) _.extend(argv, config);

var server = http.createServer();

Expand Down Expand Up @@ -354,7 +362,7 @@ function connection(ws) {
});
});

server.listen(config.port, function() {
server.listen(argv.port, function() {
console.log('master server is listening on port ' + server.address().port);
});

Expand Down

0 comments on commit 19dc0fe

Please sign in to comment.