Skip to content

Commit

Permalink
Add listenOn field to config
Browse files Browse the repository at this point in the history
  • Loading branch information
dora-korpar committed Jan 17, 2017
1 parent b21d83a commit 07ebbf8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions config.json
@@ -1,5 +1,6 @@
{
"port": 8000,
"listenOn": [],
"regions": {
"ap-northeast-1": ["s3.ap-northeast-1.amazonaws.com"],
"ap-southeast-1": ["s3.ap-southeast-1.amazonaws.com"],
Expand Down
24 changes: 24 additions & 0 deletions lib/Config.js
@@ -1,6 +1,7 @@
import assert from 'assert';
import fs from 'fs';
import path from 'path';
import { ipCheck } from 'arsenal';

import authDataChecker from './auth/in_memory/checker';

Expand Down Expand Up @@ -43,6 +44,29 @@ class Config {
this.port = config.port;
}

this.listenOn = [];
if (config.listenOn !== undefined) {
assert(Array.isArray(config.listenOn)
&& config.listenOn.every(e => typeof e === 'string'),
'bad config: listenOn must be a list of strings');
config.listenOn.forEach(item => {
const lastColon = item.lastIndexOf(':');
// if address is IPv6 format, it includes brackets
// that have to be removed from the final IP address
const ipAddress = item.indexOf(']') > 0 ?
item.substr(1, lastColon - 2) :
item.substr(0, lastColon);
// the port should not include the colon
const port = item.substr(lastColon + 1);
// parseIp returns as empty object if the address is invalid
assert(Object.keys(ipCheck.parseIp(ipAddress)).length !== 0,
'bad config: listenOn IP address must be valid');
assert(parseInt(port, 10),
'bad config: listenOn port must be a positive integer');
this.listenOn.push({ ip: ipAddress, port });
});
}

assert(typeof config.regions === 'object',
'bad config: the list of regions is mandatory');
assert(Object.keys(config.regions).every(
Expand Down
18 changes: 14 additions & 4 deletions lib/server.js
Expand Up @@ -36,7 +36,7 @@ class S3Server {
/*
* This starts the http server.
*/
startup(port) {
startup(port, ipAddress) {
// Todo: http.globalAgent.maxSockets, http.globalAgent.maxFreeSockets
if (_config.https) {
this.server = https.createServer({
Expand Down Expand Up @@ -69,13 +69,17 @@ class S3Server {
});
this.server.on('listening', () => {
const addr = this.server.address() || {
address: '[::]',
address: ipAddress || '[::]',
port,
};
logger.info('server started', { address: addr.address,
port: addr.port, pid: process.pid });
});
this.server.listen(port);
if (ipAddress !== undefined) {
this.server.listen(port, ipAddress);
} else {
this.server.listen(port);
}
}

/*
Expand Down Expand Up @@ -112,7 +116,13 @@ class S3Server {
log.info('initial health check succeeded', {
healthStatus: results,
});
this.startup(_config.port);
if (_config.listenOn.length > 0) {
_config.listenOn.forEach(item => {
this.startup(item.port, item.ip);
});
} else {
this.startup(_config.port);
}
}
});
}
Expand Down

0 comments on commit 07ebbf8

Please sign in to comment.