AlexStocks commented on code in PR #3280:
URL: https://github.com/apache/dubbo-go/pull/3280#discussion_r3001518454


##########
protocol/triple/openapi/integration.go:
##########
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package openapi
+
+import (
+       "net/http"
+       "sync"
+)
+
+import (
+       "github.com/dubbogo/gost/log/logger"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/common"
+       "dubbo.apache.org/dubbo-go/v3/global"
+)
+
+var (
+       globalService   *DefaultService
+       globalHandler   *RequestHandler
+       swaggerHandler  *SwaggerUIHandler
+       redocHandler    *RedocHandler
+       serviceInitOnce sync.Once
+)
+
+func InitService(cfg *global.OpenAPIConfig) {
+       serviceInitOnce.Do(func() {

Review Comment:
   [P1] 这里把 OpenAPI 服务做成了进程级单例,后续 Triple server 再次调用 `InitService` 时,新的 
`OpenAPIConfig`(例如不同的 `Path`、标题、分组策略)都会被 `sync.Once` 直接忽略。一个进程里同时启动多个 Triple 
服务是常见场景,这样后启动的服务会共用第一份配置和文档实例,表现会直接错乱。建议把 OpenAPI service/handler 绑定到具体 
`Server` 实例,或者至少按监听地址/配置维度做隔离,而不是全局单例。



##########
protocol/triple/openapi/handler.go:
##########
@@ -0,0 +1,151 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package openapi
+
+import (
+       "net/http"
+       "strings"
+)
+
+import (
+       "dubbo.apache.org/dubbo-go/v3/global"
+)
+
+type RequestHandler struct {
+       service *DefaultService
+       config  *global.OpenAPIConfig
+}
+
+func NewRequestHandler(service *DefaultService, config *global.OpenAPIConfig) 
*RequestHandler {
+       return &RequestHandler{
+               service: service,
+               config:  config,
+       }
+}
+
+func (h *RequestHandler) Handle(req *http.Request) (string, string, bool) {
+       path := req.URL.Path
+       basePath := h.config.Path
+
+       apiDocsPath := basePath + "/api-docs"
+       if strings.HasPrefix(path, apiDocsPath) {
+               return h.handleAPIDocs(path, basePath)
+       }
+
+       if strings.HasPrefix(path, basePath) {

Review Comment:
   [P1] 这里用 `HasPrefix` 做入口判断过宽了。像 `/dubbo/openapi-private` 这种并不属于 OpenAPI 
的路径也会命中这一分支,然后在 `handleOpenAPI` 里走到默认逻辑,最终返回合并后的文档而不是 404。结果是错误路由也能暴露 OpenAPI 
内容。建议至少要求“完全等于 basePath”或“以 `basePath/` 为边界”,`api-docs` 分支也一样要做边界判断。



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to