Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add variables & command-line options for binding #1105

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 21 additions & 5 deletions tools/meteor.js
Expand Up @@ -185,8 +185,12 @@ Fiber(function () {
// reparse args
// This help logic should probably move to run.js eventually
var opt = require('optimist')
.alias('port', 'p').default('port', 3000)
.describe('port', 'Port to listen on. NOTE: Also uses port N+1 and N+2.')
.alias('port', 'p').default('port', process.env.METEOR_PORT || 3000)
.describe('port', 'Port to listen on')
.describe('inner_port', 'Port for the internal server').default('inner_port', process.env.METEOR_INNER_PORT || 3001)
.describe('mongo_port', "MongoDB port").default('mongo_port', process.env.METEOR_MONGO_PORT || 3002)
.alias('bind', 'b').default('bind', process.env.METEOR_IP || "0.0.0.0")
.describe('inner_bind', 'IP of the internal server').default('inner_bind', process.env.METEOR_INNER_IP || "127.0.0.1")
.boolean('production')
.describe('production', 'Run in production mode. Minify and bundle CSS and JS files.')
.describe('settings', 'Set optional data for Meteor.settings on the server')
Expand Down Expand Up @@ -222,6 +226,10 @@ Fiber(function () {
maybePrintUserOverrideMessage();
runner.run(context, {
port: new_argv.port,
inner_port: new_argv.inner_port,
mongo_port: new_argv.mongo_port,
bind: new_argv.bind,
inner_bind: new_argv.inner_bind,
minify: new_argv.production,
once: new_argv.once,
settingsFile: new_argv.settings
Expand Down Expand Up @@ -677,7 +685,7 @@ Fiber(function () {
process.exit(1);
}

var mongo_url = "mongodb://127.0.0.1:" + mongod_port + "/meteor";
var mongo_url = "mongodb://" + new_argv.inner_bind + ":" + mongod_port + "/meteor";

if (new_argv.url)
console.log(mongo_url);
Expand Down Expand Up @@ -829,8 +837,12 @@ Fiber(function () {
// reparse args
// This help logic should probably move to run.js eventually
var opt = require('optimist')
.alias('port', 'p').default('port', 3000)
.describe('port', 'Port to listen on. NOTE: Also uses port N+1 and N+2.')
.alias('port', 'p').default('port', process.env.METEOR_PORT || 3000)
.describe('port', 'Port to listen on')
.describe('inner_port', 'Port for the internal server').default('inner_port', process.env.METEOR_INNER_PORT || 3001)
.describe('mongo_port', "MongoDB port").default('mongo_port', process.env.METEOR_MONGO_PORT || 3002)
.alias('bind', 'b').default('bind', process.env.METEOR_IP || "0.0.0.0")
.describe('inner_bind', 'IP of the internal server').default('inner_bind', process.env.METEOR_INNER_IP || "127.0.0.1")
.describe('deploy', 'Optionally, specify a domain to deploy to, rather than running locally.')
.boolean('production')
.describe('production', 'Run in production mode. Minify and bundle CSS and JS files.')
Expand Down Expand Up @@ -919,6 +931,10 @@ Fiber(function () {
} else {
runner.run(context, {
port: new_argv.port,
inner_port: new_argv.inner_port,
mongo_port: new_argv.mongo_port,
bind: new_argv.bind,
inner_bind: new_argv.inner_bind,
minify: new_argv.production,
once: new_argv.once,
testPackages: testPackages,
Expand Down
4 changes: 2 additions & 2 deletions tools/mongo_runner.js
Expand Up @@ -125,7 +125,7 @@ var find_mongo_and_kill_it_dead = function (port, callback) {
});
};

exports.launch_mongo = function (app_dir, port, launch_callback, on_exit_callback) {
exports.launch_mongo = function (app_dir, ip, port, launch_callback, on_exit_callback) {
var handle = {stop: function (callback) { callback(); } };
launch_callback = launch_callback || function () {};
on_exit_callback = on_exit_callback || function () {};
Expand Down Expand Up @@ -158,7 +158,7 @@ exports.launch_mongo = function (app_dir, port, launch_callback, on_exit_callbac
}

var proc = child_process.spawn(mongod_path, [
'--bind_ip', '127.0.0.1',
'--bind_ip', ip,
'--smallfiles',
'--port', port,
'--dbpath', data_path
Expand Down
27 changes: 16 additions & 11 deletions tools/run.js
Expand Up @@ -100,7 +100,7 @@ var request_queue = [];
// calls callback once proxy is actively listening on outer and
// proxying to inner.

var start_proxy = function (outer_port, inner_port, callback) {
var start_proxy = function (bind, inner_bind, outer_port, inner_port, callback) {
callback = callback || function () {};

var p = httpProxy.createServer(function (req, res, proxy) {
Expand All @@ -125,14 +125,14 @@ var start_proxy = function (outer_port, inner_port, callback) {
} else if (Status.listening) {
// server is listening. things are hunky dory!
proxy.proxyRequest(req, res, {
host: '127.0.0.1', port: inner_port
host: inner_bind, port: inner_port
});
} else {
// Not listening yet. Queue up request.
var buffer = httpProxy.buffer(req);
request_queue.push(function () {
proxy.proxyRequest(req, res, {
host: '127.0.0.1', port: inner_port,
host: inner_bind, port: inner_port,
buffer: buffer
});
});
Expand All @@ -144,14 +144,14 @@ var start_proxy = function (outer_port, inner_port, callback) {
if (Status.listening) {
// server is listening. things are hunky dory!
p.proxy.proxyWebSocketRequest(req, socket, head, {
host: '127.0.0.1', port: inner_port
host: inner_bind, port: inner_port
});
} else {
// Not listening yet. Queue up request.
var buffer = httpProxy.buffer(req);
request_queue.push(function () {
p.proxy.proxyWebSocketRequest(req, socket, head, {
host: '127.0.0.1', port: inner_port,
host: inner_bind, port: inner_port,
buffer: buffer
});
});
Expand All @@ -160,7 +160,7 @@ var start_proxy = function (outer_port, inner_port, callback) {

p.on('error', function (err) {
if (err.code == 'EADDRINUSE') {
process.stderr.write("Can't listen on port " + outer_port
process.stderr.write("Can't listen on " + bind + ":" + outer_port
+ ". Perhaps another Meteor is running?\n");
process.stderr.write("\n");
process.stderr.write("Running two copies of Meteor in the same application directory\n");
Expand All @@ -183,7 +183,7 @@ var start_proxy = function (outer_port, inner_port, callback) {
res.end('Unexpected error.');
});

p.listen(outer_port, callback);
p.listen(outer_port, bind, callback);
};

////////// MongoDB //////////
Expand Down Expand Up @@ -230,6 +230,7 @@ var start_server = function (options) {
for (var k in process.env)
env[k] = process.env[k];

env.IP = options.innerBind;
env.PORT = options.innerPort;
env.MONGO_URL = options.mongoURL;
env.ROOT_URL = env.ROOT_URL || ('http://localhost:' + options.outerPort);
Expand Down Expand Up @@ -586,12 +587,14 @@ exports.getSettings = function (filename) {
// options include: port, minify, once, settingsFile, testPackages
exports.run = function (context, options) {
var outer_port = options.port || 3000;
var inner_port = outer_port + 1;
var mongo_port = outer_port + 2;
var inner_port = options.inner_port || outer_port + 1;
var mongo_port = options.mongo_port || inner_port + 1;
var bind = options.bind || '0.0.0.0';
var inner_bind = options.inner_bind || '127.0.0.1';
var bundle_path = path.join(context.appDir, '.meteor', 'local', 'build');
// Allow override and use of external mongo. Matches code in launch_mongo.
var mongo_url = process.env.MONGO_URL ||
("mongodb://127.0.0.1:" + mongo_port + "/meteor");
("mongodb://" + inner_bind + ":" + mongo_port + "/meteor");
var firstRun = true;

var deps_info = null;
Expand Down Expand Up @@ -758,6 +761,7 @@ exports.run = function (context, options) {
bundlePath: bundle_path,
outerPort: outer_port,
innerPort: inner_port,
innerBind: inner_bind,
mongoURL: mongo_url,
onExit: function (code) {
// on server exit
Expand Down Expand Up @@ -785,6 +789,7 @@ exports.run = function (context, options) {
var launch = function () {
Status.mongoHandle = mongo_runner.launch_mongo(
context.appDir,
inner_bind,
mongo_port,
function () { // On Mongo startup complete
// don't print mongo startup is slow warning.
Expand Down Expand Up @@ -825,7 +830,7 @@ exports.run = function (context, options) {
});
};

start_proxy(outer_port, inner_port, function () {
start_proxy(bind, inner_bind, outer_port, inner_port, function () {
process.stdout.write("[[[[[ " + files.pretty_path(context.appDir) + " ]]]]]\n\n");

mongo_startup_print_timer = setTimeout(function () {
Expand Down
3 changes: 2 additions & 1 deletion tools/server/server.js
Expand Up @@ -169,6 +169,7 @@ var run = function () {

// check environment
var port = process.env.PORT ? parseInt(process.env.PORT) : 80;
var ip = process.env.IP || "127.0.0.1";

// check for a valid MongoDB URL right away
if (!process.env.MONGO_URL)
Expand Down Expand Up @@ -329,7 +330,7 @@ var run = function () {
_.each(__meteor_bootstrap__.startup_hooks, function (x) { x(); });

// only start listening after all the startup code has run.
httpServer.listen(port, function() {
httpServer.listen(port, ip, function() {
if (argv.keepalive)
console.log("LISTENING"); // must match run.js
});
Expand Down