Jelajahi Sumber

2.47.1 修门户回归: Java 网关控制器代理 / 与 /index.html(20MB 门户, 放开 WebClient 缓冲; Path=/ 谓词不匹配根请求); pages_audit 恢复

zhouyang.xie 1 Minggu lalu
induk
melakukan
630c4c94c0

+ 27 - 8
app_backEnd/backend-gateway/src/main/java/com/guanlan/gateway/web/PortalController.java

@@ -1,24 +1,43 @@
 package com.guanlan.gateway.web;
 package com.guanlan.gateway.web;
 
 
-import org.springframework.http.HttpStatus;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.http.MediaType;
 import org.springframework.http.ResponseEntity;
 import org.springframework.http.ResponseEntity;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RestController;
 import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.reactive.function.client.ExchangeStrategies;
+import org.springframework.web.reactive.function.client.WebClient;
 import reactor.core.publisher.Mono;
 import reactor.core.publisher.Mono;
 
 
-import java.net.URI;
+import java.time.Duration;
 
 
 /**
 /**
- * 门户入口重定向(P12):`/` → `/web/`。
+ * 门户代理(P12 入口切换):根路径 `/` 与 `/index.html` 从**旧网关**(退到 28086)取回门户 HTML 原样返回。
  *
  *
- * <p>用户令「先把入口切到 Java 网关」:原来的门户页由 Python 网关提供,切换后统一由 Java 网关承接,
- * 根路径直接送到 Vue 工作台(`/web/`,由 web 路由 StripPrefix 后打到 28110)。
+ * <p>为什么不用路由谓词:`Path=/` 在 Spring Cloud Gateway 里不匹配根请求(实逮 `/` 仍 404),
+ * 故用控制器显式代理 —— 这样"入口是 Java 网关"与"根路径仍是门户页"(门禁 `scripts/pages_audit.py` 的判据)
+ * 同时成立;Vue 工作台仍走 `/web/`。
  */
  */
 @RestController
 @RestController
 public class PortalController {
 public class PortalController {
 
 
-    @GetMapping("/")
-    public Mono<ResponseEntity<Void>> portal() {
-        return Mono.just(ResponseEntity.status(HttpStatus.FOUND).location(URI.create("/web/")).build());
+    private final WebClient web;
+
+    public PortalController(@Value("${guanlan.portal-upstream:http://127.0.0.1:28086}") String upstream) {
+        // ★实逮:门户页 20 MB(内联经典图表脚本)远超 WebClient 默认 256 KB 内存缓冲 ⇒ 必须放开
+        this.web = WebClient.builder().baseUrl(upstream)
+                .exchangeStrategies(ExchangeStrategies.builder()
+                        .codecs(c -> c.defaultCodecs().maxInMemorySize(-1)).build())
+                .build();
+    }
+
+    @GetMapping(value = {"/", "/index.html"}, produces = "text/html; charset=utf-8")
+    public Mono<ResponseEntity<String>> portal() {
+        return web.get().uri("/").retrieve().bodyToMono(String.class)
+                .timeout(Duration.ofSeconds(60))
+                .map(html -> ResponseEntity.ok().contentType(MediaType.valueOf("text/html; charset=utf-8")).body(html))
+                .onErrorResume(e -> Mono.just(ResponseEntity.status(503)
+                        .contentType(MediaType.valueOf("text/html; charset=utf-8"))
+                        .body("<h1>门户暂不可用</h1><p>旧网关(28086)未启动: " + e.getClass().getSimpleName() + "</p>")));
     }
     }
 }
 }

+ 7 - 0
app_backEnd/backend-gateway/src/main/resources/application.yml

@@ -50,6 +50,13 @@ spring:
           filters:
           filters:
             - StripPrefix=1
             - StripPrefix=1
         # ★P12 入口切换:/ops 运维台由**旧网关**(退到 28086)继续提供;Java 网关原样转发(不剥前缀)
         # ★P12 入口切换:/ops 运维台由**旧网关**(退到 28086)继续提供;Java 网关原样转发(不剥前缀)
+        # ★P12:根路径与 /index.html **原样转发到 28086 的门户**(与入口切换前行为一致;
+        #   门禁 scripts/pages_audit.py 正是校验这一点)
+        - id: portal
+          uri: http://127.0.0.1:28086
+          predicates:
+            - Path=/
+            - Path=/index.html
         - id: ops
         - id: ops
           uri: http://127.0.0.1:28086
           uri: http://127.0.0.1:28086
           predicates:
           predicates: