Skip to content

Commit f73918e

Browse files
committed
Top level cleanup
1 parent 6dd1549 commit f73918e

File tree

3 files changed

+170
-175
lines changed

3 files changed

+170
-175
lines changed

lib/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
module.exports = require('./lift');
1+
var Sails = require('./lift');
2+
3+
module.exports = new Sails();

lib/lift/index.js

+167-85
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,17 @@
33
*/
44

55
var _ = require( 'lodash' ),
6-
util = require( '../util'),
76
EventEmitter= require('events').EventEmitter,
87
async = require('async'),
9-
loadSails = require('./load'),
10-
initSails = require('./init');
8+
util = require( '../util'),
9+
loadSails = require('./load');
10+
11+
12+
/**
13+
* Expose `Sails` constructor
14+
*/
15+
16+
module.exports = Sails;
1117

1218

1319

@@ -18,127 +24,203 @@ var _ = require( 'lodash' ),
1824
*/
1925

2026
function Sails () {
27+
28+
/**
29+
* Expose utilities
30+
*
31+
* @api private
32+
*/
33+
2134
this.util = util;
22-
_.bindAll(this);
23-
}
2435

2536

26-
/**
27-
* Extend from EventEmitter to allow hooks to listen to stuff
28-
*
29-
* @api private
30-
*/
3137

32-
Sails.prototype = new EventEmitter();
33-
Sails.prototype.constructor = Sails;
38+
/**
39+
* Load the pieces of a Sails app
40+
*
41+
* @api private
42+
*/
3443

44+
this.load = function (cb) {
45+
return loadSails(this)(cb);
46+
};
3547

3648

37-
/**
38-
* Load the pieces of a Sails app
39-
*
40-
* @api private
41-
*/
4249

43-
Sails.prototype.load = function () {
44-
return loadSails(this);
45-
};
50+
/**
51+
* Start the Sails server
52+
*
53+
* @api private
54+
*/
4655

56+
this.initialize = function (cb) {
4757

58+
var sails = this;
4859

49-
/**
50-
* Start the Sails server
51-
*
52-
* @api private
53-
*/
60+
// Indicate that server is starting
61+
sails.log('Starting app at ' + sails.config.appPath + '...');
5462

55-
Sails.prototype.initialize = initSails;
63+
// Used to warn about possible issues if starting the server is taking a very long time
64+
var liftAbortTimer;
5665

66+
async.auto({
5767

68+
bindBeforeShutdown: function (cb) {
5869

59-
/**
60-
* Factory method to generate a sails instance,
61-
* lift() is also the entry point for the Sails.js runtime
62-
* Loads the app, then starts the server.
63-
*
64-
* @api public
65-
*/
70+
// Add beforeShutdown event
71+
var exiting;
72+
process.on('SIGINT', function() {
73+
beforeShutdown(process.exit);
74+
});
75+
process.on('SIGTERM', function() {
76+
beforeShutdown(process.exit);
77+
});
78+
process.on('exit', function() {
79+
if (!exiting) beforeShutdown();
80+
});
81+
function beforeShutdown(cb) {
82+
exiting = true;
83+
if(_.isFunction(sails.config.beforeShutdown)) {
84+
sails.config.beforeShutdown(cb);
85+
}
86+
else cb && cb();
87+
}
6688

67-
Sails.prototype.lift = function (configOverride, cb) {
89+
cb();
90+
},
6891

69-
var sails = this;
92+
startServer: function (cb) {
7093

71-
// Stow CLI/env override
72-
this.config = _.clone(configOverride || {});
94+
// Start Express server (implicitly starts socket.io)
95+
// If host is explicitly declared, include it in express's listen() call
96+
if (sails.explicitHost) {
97+
sails.log.verbose('Restricting access to explicit host: '+sails.explicitHost);
98+
sails.express.server.listen(sails.config.port, sails.explicitHost, cb);
99+
}
100+
else {
101+
sails.express.server.listen(sails.config.port, cb);
102+
}
73103

74-
async.series([
75-
76-
this.load(sails),
104+
// Start timer in case this takes suspiciously long...
105+
liftAbortTimer = setTimeout(function failedToStart() {
106+
sails.log.warn('');
107+
sails.log.warn('Server doesn\'t seem to be starting.');
108+
sails.log.warn('Perhaps something else is already running on port '+sails.config.port+ ' with hostname ' + sails.explicitHost + '?');
109+
}, 2500);
110+
},
77111

78-
this.initialize(sails)
112+
verifyServerStartedSuccessfully: function (cb) {
79113

80-
], function sailsReady (err, results) {
114+
// Check for port conflicts
115+
// Ignore this check if explicit host is set
116+
if(!sails.explicitHost && !sails.express.server.address()) {
117+
sails.log.error('Trying to start server on port ' + sails.config.port + '...');
118+
sails.log.error('But something else is already running on that port!');
119+
sails.log.error('Please disable the other server, or choose a different port, and try again.');
120+
process.exit(1);
121+
}
81122

82-
sails.log();
83-
sails.log.ship();
84-
sails.log('Sails (v'+sails.version +')');
85-
sails.log('Sails lifted on port ' + sails.config.port + ' in ' + sails.config.environment + ' mode.');
123+
cb();
124+
}
86125

87-
if (sails.config.environment === 'development') {
88-
var usingSSL = ( ( sails.config.serverOptions && sails.config.serverOptions.key && sails.config.serverOptions.cert ) ||
89-
( sails.config.express && sails.config.express.serverOptions && sails.config.express.serverOptions.key && sails.config.express.serverOptions.cert ));
126+
}, function (err) {
127+
clearTimeout(liftAbortTimer);
128+
return cb && cb(err);
129+
});
130+
131+
};
132+
133+
134+
135+
/**
136+
* Factory method to generate a sails instance,
137+
* lift() is also the entry point for the Sails.js runtime
138+
* Loads the app, then starts the server.
139+
*
140+
* @api public
141+
*/
142+
143+
this.lift = function (configOverride, cb) {
144+
145+
var sails = this;
146+
147+
// Stow CLI/env override
148+
this.config = _.clone(configOverride || {});
149+
150+
async.series([
151+
152+
this.load,
153+
154+
this.initialize
155+
156+
], function sailsReady (err, results) {
90157

91158
sails.log();
92-
sails.log('( to see your app, visit: ' + ( usingSSL ? 'https' : 'http' ) + '://' + sails.config.host + ':' + sails.config.port + ' )');
93-
}
159+
sails.log.ship();
160+
sails.log('Sails (v'+sails.version +')');
161+
sails.log('Sails lifted on port ' + sails.config.port + ' in ' + sails.config.environment + ' mode.');
94162

95-
return cb && cb(err, sails);
96-
});
97-
};
163+
if (sails.config.environment === 'development') {
164+
var usingSSL = ( ( sails.config.serverOptions && sails.config.serverOptions.key && sails.config.serverOptions.cert ) ||
165+
( sails.config.express && sails.config.express.serverOptions && sails.config.express.serverOptions.key && sails.config.express.serverOptions.cert ));
98166

167+
sails.log();
168+
sails.log('( to see your app, visit: ' + ( usingSSL ? 'https' : 'http' ) + '://' + sails.config.host + ':' + sails.config.port + ' )');
169+
}
99170

171+
return cb && cb(err, sails);
172+
});
173+
};
100174

101-
/**
102-
* Kill the server
103-
* ( Socket.io server is stopped automatically when Express server is closed )
104-
*
105-
* @api public
106-
*/
107175

108-
Sails.prototype.lower = function (cb) {
109-
sails.express.server.close();
110-
};
111176

177+
/**
178+
* Kill the server
179+
* ( Socket.io server is stopped automatically when Express server is closed )
180+
*
181+
* @api public
182+
*/
112183

184+
this.lower = function (cb) {
185+
sails.express.server.close();
186+
};
113187

114-
/**
115-
* Run the grunt build task
116-
*
117-
* @api public
118-
*/
119188

120-
Sails.prototype.build = function (taskName, cb) {
121-
122-
// Default to 'build' task
123-
taskName = taskName || 'build';
124-
125-
var log = this.log;
126-
log.info('Building assets into directory...');
127-
128-
var startGrunt = require('../automation')(this);
129-
startGrunt(taskName, function (err) {
130-
if (err) return cb && cb(err);
131189

132-
log.info('Successfully built \'www\' directory in the application root.');
133-
cb && cb();
134-
});
135-
};
190+
/**
191+
* Run the grunt build task
192+
*
193+
* @api public
194+
*/
136195

196+
this.build = function (taskName, cb) {
197+
198+
// Default to 'build' task
199+
taskName = taskName || 'build';
200+
201+
var log = this.log;
202+
log.info('Building assets into directory...');
203+
204+
var startGrunt = require('../automation')(this);
205+
startGrunt(taskName, function (err) {
206+
if (err) return cb && cb(err);
207+
208+
log.info('Successfully built \'www\' directory in the application root.');
209+
cb && cb();
210+
});
211+
};
212+
213+
_.bindAll(this);
214+
}
137215

138216

139217

140218
/**
141-
* Expose a new Sails instance
219+
* Extend from EventEmitter to allow hooks to listen to stuff
220+
*
221+
* @api private
142222
*/
143223

144-
module.exports = new Sails();
224+
Sails.prototype = new EventEmitter();
225+
Sails.prototype.constructor = Sails;
226+

0 commit comments

Comments
 (0)