1
- var http_proxy = require ( 'http-proxy' ) ;
1
+ var http_proxy = require ( 'http-proxy' ) ,
2
+ proxyModel = require ( '../models/proxy' ) ;
2
3
3
4
var Proxy = exports . Proxy = function ( app , haibu ) {
4
5
var self = this ;
@@ -44,7 +45,7 @@ var Proxy = exports.Proxy = function(app, haibu) {
44
45
45
46
var route_message = this . req . body . domain + ':' + port + ' > ' + pkg . host + ':' + pkg . port ;
46
47
47
- if ( self . set ( port , pkg , this . req . body . domain ) )
48
+ if ( self . set ( port , pkg , this . req . body . domain , true ) )
48
49
haibu . sendResponse ( this . res , 200 , { message : 'Proxy route added: ' + route_message } ) ;
49
50
else
50
51
haibu . sendResponse ( this . res , 500 , { message : 'Could not create route: ' + route_message } ) ;
@@ -130,7 +131,15 @@ Proxy.prototype.start = function(port) {
130
131
//forward request to target
131
132
proxy . proxyRequest ( req , res , self . proxies [ port ] . routes [ host ] ) ;
132
133
}
133
- } ) . listen ( port ) ;
134
+ } ) ;
135
+
136
+ this . proxies [ port ] . proxy . on ( 'error' , function ( err ) {
137
+ //if proxy failed to start, remove it
138
+ console . log ( 'Could not start proxy on ' + port , err ) ;
139
+ delete self . proxies [ port ] ;
140
+ } ) ;
141
+
142
+ this . proxies [ port ] . proxy . listen ( port ) ;
134
143
135
144
return true ;
136
145
}
@@ -141,7 +150,7 @@ Proxy.prototype.start = function(port) {
141
150
//stops proxy on given port
142
151
Proxy . prototype . stop = function ( port ) {
143
152
if ( this . proxies [ port ] ) {
144
- this . proxies [ port ] . close ( ) ;
153
+ this . proxies [ port ] . proxy . close ( ) ;
145
154
146
155
return true ;
147
156
}
@@ -161,16 +170,25 @@ Proxy.prototype.clean = function(port) {
161
170
} ;
162
171
163
172
//sets domain target
164
- Proxy . prototype . set = function ( port , pkg , domain ) {
165
- if ( domain . trim ( ) != '' && this . proxies [ port ] && pkg . host && pkg . port ) {
166
- this . proxies [ port ] . routes [ domain ] = {
167
- host : pkg . host ,
168
- port : ( pkg . env . PORT ) ,
169
- user : pkg . user ,
170
- appid : pkg . name
171
- } ;
172
-
173
- return true ;
173
+ Proxy . prototype . set = function ( port , pkg , domain , persist ) {
174
+ if ( domain . trim ( ) != '' && this . proxies [ port ] && pkg . host ) {
175
+ var target_port = ( pkg . env ? pkg . env . PORT : null ) || pkg . port ;
176
+
177
+ if ( target_port ) {
178
+ this . proxies [ port ] . routes [ domain ] = {
179
+ host : pkg . host ,
180
+ port : target_port ,
181
+ user : pkg . user ,
182
+ appid : pkg . name ,
183
+ persist : persist
184
+ } ;
185
+
186
+ //save to db if persistent
187
+ if ( persist )
188
+ proxyModel . createOrUpdate ( { host : domain , port : port } , this . proxies [ port ] . routes [ domain ] , function ( ) { } ) ;
189
+
190
+ return true ;
191
+ }
174
192
}
175
193
176
194
return false ;
@@ -213,6 +231,10 @@ Proxy.prototype.getBy = function(port, match) {
213
231
//delete domain target
214
232
Proxy . prototype . delete = function ( port , domain ) {
215
233
if ( this . proxies [ port ] && this . proxies [ port ] . routes [ domain ] ) {
234
+ //remove from db if persistent
235
+ if ( this . proxies [ port ] . routes [ domain ] . persist )
236
+ proxyModel . deleteBy ( { source : { host : domain , port : port } } , console . log ) ;
237
+
216
238
delete this . proxies [ port ] . routes [ domain ] ;
217
239
218
240
return true ;
@@ -260,3 +282,22 @@ Proxy.prototype.load = function(port, pkg) {
260
282
return false ;
261
283
} ;
262
284
285
+ //load all persistent routes from db
286
+ Proxy . prototype . autoload = function ( ) {
287
+ var self = this ;
288
+
289
+ proxyModel . get ( function ( err , routes ) {
290
+ if ( err )
291
+ return console . log ( err ) ;
292
+
293
+ if ( routes . length > 0 ) {
294
+ routes . forEach ( function ( route ) {
295
+ //create proxy if it doesn't exist
296
+ if ( ! self . proxies [ route . source . port ] )
297
+ self . start ( route . source . port ) ;
298
+
299
+ self . set ( route . source . port , route . target , route . source . host , true ) ;
300
+ } ) ;
301
+ }
302
+ } ) ;
303
+ } ;
0 commit comments