7
7
import io .vertx .ext .web .Route ;
8
8
import io .vertx .ext .web .Router ;
9
9
import io .vertx .ext .web .RoutingContext ;
10
+ import io .vertx .ext .web .handler .StaticHandler ;
10
11
import org .slf4j .Logger ;
11
12
import org .slf4j .LoggerFactory ;
12
13
@@ -162,6 +163,11 @@ public class ReverseHttpProxy {
162
163
*/
163
164
protected static final String INTERNAL_STATUS_CODE = "INTERNAL_STATUS_CODE" ;
164
165
166
+ /**
167
+ * 静态资源前缀
168
+ */
169
+ protected static final String STATIC = "static:" ;
170
+
165
171
protected static final char [] ID_CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" .toCharArray ();
166
172
167
173
@@ -329,16 +335,20 @@ public void stop() {
329
335
}
330
336
331
337
public ReverseHttpProxy addRoute (ProxyRoute proxyRoute ) {
332
- return addRoute (proxyRoute , null );
338
+ return addRoute (proxyRoute , null , true );
333
339
}
334
340
341
+ public ReverseHttpProxy addRoute (ProxyRoute proxyRoute , Integer order ) {
342
+ return addRoute (proxyRoute , order , true );
343
+ }
335
344
336
345
/**
337
346
* order越小,优先级越高
338
347
*/
339
348
public ReverseHttpProxy addRoute (
340
349
ProxyRoute proxyRoute ,
341
- Integer order
350
+ Integer order ,
351
+ boolean printLog
342
352
) {
343
353
Route route = router .route (proxyRoute .getSourceUrl ()).setName (proxyRoute .getName ());
344
354
if (order != null ) {
@@ -348,8 +358,20 @@ public ReverseHttpProxy addRoute(
348
358
for (String key : map .keySet ()) {
349
359
setRouteMetadata (route , key , map .get (key ));
350
360
}
351
- route .handler (routingContextHandler (httpClient ));
352
- jsonLog (proxyRoute );
361
+ String targetUrl = proxyRoute .getTargetUrl ();
362
+ if (targetUrl .startsWith (STATIC )) {
363
+ String staticPath = targetUrl .replace (STATIC , "" );
364
+ StaticHandler staticHandler = StaticHandler .create (staticPath )
365
+ .setDirectoryListing (false )
366
+ .setAlwaysAsyncFS (true )
367
+ .setIndexPage ("index.html" );
368
+ route .handler (staticHandler );
369
+ } else {
370
+ route .handler (routingContextHandler (httpClient ));
371
+ }
372
+ if (printLog ) {
373
+ jsonLog (proxyRoute );
374
+ }
353
375
return this ;
354
376
}
355
377
0 commit comments