1
- var Cookies = Npm . require ( 'cookies' ) ;
2
- var HttpProxy = Npm . require ( 'http-proxy' ) ;
3
1
var urlParse = Npm . require ( 'url' ) . parse ;
4
2
var urlResolve = Npm . require ( 'url' ) . resolve ;
5
3
6
- Balancer = { } ;
7
- Balancer . proxy = HttpProxy . createProxyServer ( {
8
- xfwd : true
9
- } ) ;
10
-
11
4
Balancer . _buildCookie = function _buildCookie ( name , service ) {
12
5
return name + "::" + service ;
13
6
} ;
@@ -108,7 +101,7 @@ function _sendSockJsInfo(req, res, cookies) {
108
101
if ( ! endpoint ) {
109
102
// we can't process this here.
110
103
// TODO: better error handling
111
- console . error ( "cluster : no such endpoint for service:" + serviceName ) ;
104
+ console . error ( "Cluster : no such endpoint for service:" + serviceName ) ;
112
105
res . writeHead ( 404 ) ;
113
106
return res . end ( "no such endpoint for service: " + serviceName ) ;
114
107
}
@@ -170,7 +163,7 @@ Balancer._proxyWeb = function _proxyWeb(req, res, endpoint, cookies, retries) {
170
163
}
171
164
172
165
function printError ( ) {
173
- console . error ( "cluster : web proxy error: " , error . message ) ;
166
+ console . error ( "Cluster : web proxy error: " , error . message ) ;
174
167
res . writeHead ( 500 ) ;
175
168
res . end ( "Internal Error: Please reload." ) ;
176
169
}
@@ -183,107 +176,4 @@ Balancer._proxyWs = function proxyWs(req, socket, head, endpoint) {
183
176
// not sure we can re-try websockets, simply log it
184
177
console . error ( "WS proxying failed! to: " + endpoint ) ;
185
178
} ) ;
186
- } ;
187
-
188
- Balancer . handleHttp = function handleHttp ( req , res ) {
189
- if ( ! Cluster . discovery ) return false ;
190
-
191
- // if this is from a balance, we don't need to proxy it
192
- if ( req . headers [ 'from-balancer' ] ) {
193
- return false ;
194
- }
195
-
196
- var cookies = new Cookies ( req , res ) ;
197
-
198
- if ( / \/ s o c k j s \/ i n f o / . test ( req . url ) ) {
199
- Balancer . _sendSockJsInfo ( req , res , cookies ) ;
200
- return true ;
201
- }
202
-
203
- // try to get endpointHash from the our cluster-ddp url
204
- // this is for sockjs long polling requets
205
- var endpointInfo = Balancer . _rewriteDdpUrl ( req ) ;
206
- if ( endpointInfo ) {
207
- var endpoint =
208
- Balancer . _pickJustEndpoint ( endpointInfo . hash , endpointInfo . service ) ;
209
-
210
- if ( ! endpoint ) {
211
- // seems like sockjs connection is not there yet!
212
- // let's end it here.
213
- var message = "cluster: there is no endpoint but we've a hash: " ;
214
- console . error ( message + endpointInfo . hash ) ;
215
- res . writeHead ( 500 ) ;
216
- res . end ( ) ;
217
- return true ;
218
- }
219
- }
220
-
221
- if ( ! endpointInfo ) {
222
- // seems like this is just static resources
223
- // we can get the endpointHash from the cookie
224
- var serviceName = Cluster . _uiService ;
225
- var cookieName = Balancer . _buildCookie ( 'cluster-endpoint' , serviceName ) ;
226
- var endpointHash = cookies . get ( cookieName ) ;
227
- var endpoint = Balancer . _pickEndpoint ( endpointHash , cookies ) ;
228
- if ( ! endpoint ) return false ;
229
- }
230
-
231
- if ( endpoint === Cluster . _endpoint ) {
232
- return false ;
233
- }
234
-
235
- Balancer . _setFromBalanceUrlHeader ( req ) ;
236
- Balancer . _proxyWeb ( req , res , endpoint , cookies ) ;
237
- return true ;
238
- } ;
239
-
240
- Balancer . handleWs = function handleWs ( req , socket , head ) {
241
- if ( ! Cluster . discovery ) return false ;
242
-
243
- if ( req . headers [ 'from-balancer' ] ) {
244
- // if this is from a balance, we don't need to proxy it
245
- return false
246
- }
247
-
248
- // try to get endpointHash from the our cluster-ddp url
249
- var endpointInfo = Balancer . _rewriteDdpUrl ( req ) ;
250
- if ( endpointInfo ) {
251
- var endpoint =
252
- Balancer . _pickJustEndpoint ( endpointInfo . hash , endpointInfo . service ) ;
253
- }
254
-
255
- // try to get the endpointHash from the cookie
256
- // this is when there is no balancer url
257
- if ( ! endpointInfo ) {
258
- var cookies = new Cookies ( req ) ;
259
-
260
- var serviceName = Cluster . _uiService ;
261
- var cookieName = Balancer . _buildCookie ( 'cluster-endpoint' , serviceName ) ;
262
- var endpointHash = cookies . get ( cookieName ) ;
263
-
264
- if ( endpointHash ) {
265
- var endpoint = Balancer . _pickJustEndpoint ( endpointHash , serviceName ) ;
266
- } else {
267
- // seems like a direct connection
268
- // just process here. We don't need to route it to a random web service
269
- // because, it is possible that this endpoint is for some other service
270
- // than web.
271
- return false ;
272
- }
273
- }
274
-
275
- if ( ! endpoint ) {
276
- return false ;
277
- }
278
-
279
- if ( endpoint === Cluster . _endpoint ) {
280
- return false ;
281
- }
282
-
283
- Balancer . _setFromBalanceUrlHeader ( req ) ;
284
- Balancer . _proxyWs ( req , socket , head , endpoint ) ;
285
- return true ;
286
- } ;
287
-
288
- OverShadowServerEvent ( 'request' , Balancer . handleHttp ) ;
289
- OverShadowServerEvent ( 'upgrade' , Balancer . handleWs ) ;
179
+ } ;
0 commit comments