Skip to content

Commit ff0c16a

Browse files
committed
feat: 添加加载静态资源功能
1 parent d1f0d8a commit ff0c16a

File tree

2 files changed

+44
-4
lines changed

2 files changed

+44
-4
lines changed

src/main/java/top/meethigher/proxy/http/ReverseHttpProxy.java

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io.vertx.ext.web.Route;
88
import io.vertx.ext.web.Router;
99
import io.vertx.ext.web.RoutingContext;
10+
import io.vertx.ext.web.handler.StaticHandler;
1011
import org.slf4j.Logger;
1112
import org.slf4j.LoggerFactory;
1213

@@ -162,6 +163,11 @@ public class ReverseHttpProxy {
162163
*/
163164
protected static final String INTERNAL_STATUS_CODE = "INTERNAL_STATUS_CODE";
164165

166+
/**
167+
* 静态资源前缀
168+
*/
169+
protected static final String STATIC = "static:";
170+
165171
protected static final char[] ID_CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
166172

167173

@@ -329,16 +335,20 @@ public void stop() {
329335
}
330336

331337
public ReverseHttpProxy addRoute(ProxyRoute proxyRoute) {
332-
return addRoute(proxyRoute, null);
338+
return addRoute(proxyRoute, null, true);
333339
}
334340

341+
public ReverseHttpProxy addRoute(ProxyRoute proxyRoute, Integer order) {
342+
return addRoute(proxyRoute, order, true);
343+
}
335344

336345
/**
337346
* order越小,优先级越高
338347
*/
339348
public ReverseHttpProxy addRoute(
340349
ProxyRoute proxyRoute,
341-
Integer order
350+
Integer order,
351+
boolean printLog
342352
) {
343353
Route route = router.route(proxyRoute.getSourceUrl()).setName(proxyRoute.getName());
344354
if (order != null) {
@@ -348,8 +358,20 @@ public ReverseHttpProxy addRoute(
348358
for (String key : map.keySet()) {
349359
setRouteMetadata(route, key, map.get(key));
350360
}
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+
}
353375
return this;
354376
}
355377

src/test/java/top/meethigher/proxy/http/ReverseHttpProxyTest.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,24 @@ public void testAllMethod() throws Exception {
4343
proxy.stop();
4444
}
4545

46+
@Test
47+
public void testStatic() throws Exception {
48+
ReverseHttpProxy proxy = ReverseHttpProxy.create(
49+
vertx
50+
);
51+
proxy.port(8080);
52+
proxy.start();
53+
ProxyRoute proxyRoute = new ProxyRoute();
54+
proxyRoute.setHttpKeepAlive(false);
55+
// proxyRoute.setSourceUrl("/*");
56+
// proxyRoute.setTargetUrl("static:D:/Desktop");
57+
proxyRoute.setSourceUrl("/blog/*");
58+
proxyRoute.setTargetUrl("static:D:/3Develop/www/hexoBlog/blog/public");
59+
proxy.addRoute(proxyRoute);
60+
TimeUnit.HOURS.sleep(1);
61+
proxy.stop();
62+
}
63+
4664

4765
/**
4866
* 用于测试省略端口时的反代场景

0 commit comments

Comments
 (0)