3
3
*/
4
4
5
5
var _ = require ( 'lodash' ) ,
6
- util = require ( '../util' ) ,
7
6
EventEmitter = require ( 'events' ) . EventEmitter ,
8
7
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 ;
11
17
12
18
13
19
@@ -18,127 +24,203 @@ var _ = require( 'lodash' ),
18
24
*/
19
25
20
26
function Sails ( ) {
27
+
28
+ /**
29
+ * Expose utilities
30
+ *
31
+ * @api private
32
+ */
33
+
21
34
this . util = util ;
22
- _ . bindAll ( this ) ;
23
- }
24
35
25
36
26
- /**
27
- * Extend from EventEmitter to allow hooks to listen to stuff
28
- *
29
- * @api private
30
- */
31
37
32
- Sails . prototype = new EventEmitter ( ) ;
33
- Sails . prototype . constructor = Sails ;
38
+ /**
39
+ * Load the pieces of a Sails app
40
+ *
41
+ * @api private
42
+ */
34
43
44
+ this . load = function ( cb ) {
45
+ return loadSails ( this ) ( cb ) ;
46
+ } ;
35
47
36
48
37
- /**
38
- * Load the pieces of a Sails app
39
- *
40
- * @api private
41
- */
42
49
43
- Sails . prototype . load = function ( ) {
44
- return loadSails ( this ) ;
45
- } ;
50
+ /**
51
+ * Start the Sails server
52
+ *
53
+ * @api private
54
+ */
46
55
56
+ this . initialize = function ( cb ) {
47
57
58
+ var sails = this ;
48
59
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 + '...' ) ;
54
62
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 ;
56
65
66
+ async . auto ( {
57
67
68
+ bindBeforeShutdown : function ( cb ) {
58
69
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
+ }
66
88
67
- Sails . prototype . lift = function ( configOverride , cb ) {
89
+ cb ( ) ;
90
+ } ,
68
91
69
- var sails = this ;
92
+ startServer : function ( cb ) {
70
93
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
+ }
73
103
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
+ } ,
77
111
78
- this . initialize ( sails )
112
+ verifyServerStartedSuccessfully : function ( cb ) {
79
113
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
+ }
81
122
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
+ }
86
125
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 ) {
90
157
91
158
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.' ) ;
94
162
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 ) ) ;
98
166
167
+ sails . log ( ) ;
168
+ sails . log ( '( to see your app, visit: ' + ( usingSSL ? 'https' : 'http' ) + '://' + sails . config . host + ':' + sails . config . port + ' )' ) ;
169
+ }
99
170
171
+ return cb && cb ( err , sails ) ;
172
+ } ) ;
173
+ } ;
100
174
101
- /**
102
- * Kill the server
103
- * ( Socket.io server is stopped automatically when Express server is closed )
104
- *
105
- * @api public
106
- */
107
175
108
- Sails . prototype . lower = function ( cb ) {
109
- sails . express . server . close ( ) ;
110
- } ;
111
176
177
+ /**
178
+ * Kill the server
179
+ * ( Socket.io server is stopped automatically when Express server is closed )
180
+ *
181
+ * @api public
182
+ */
112
183
184
+ this . lower = function ( cb ) {
185
+ sails . express . server . close ( ) ;
186
+ } ;
113
187
114
- /**
115
- * Run the grunt build task
116
- *
117
- * @api public
118
- */
119
188
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 ) ;
131
189
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
+ */
136
195
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
+ }
137
215
138
216
139
217
140
218
/**
141
- * Expose a new Sails instance
219
+ * Extend from EventEmitter to allow hooks to listen to stuff
220
+ *
221
+ * @api private
142
222
*/
143
223
144
- module . exports = new Sails ( ) ;
224
+ Sails . prototype = new EventEmitter ( ) ;
225
+ Sails . prototype . constructor = Sails ;
226
+
0 commit comments