From 2c0d9c1209f3ed3a23ed6b42ab3a5c7aa27ec6c4 Mon Sep 17 00:00:00 2001 From: Muse Date: Tue, 20 Oct 2026 10:02:05 +0000 Subject: [PATCH] =?UTF-8?q?=E7=B2=BE=E7=AE=80:=20=E5=8E=BB=E6=8E=89=20API?= =?UTF-8?q?=20=E6=96=87=E6=A1=A3(Swagger)=E5=90=8E=E7=AB=AF=E8=B7=AF?= =?UTF-8?q?=E7=94=B1=E4=B8=8E=20openapi.json=20=E5=88=86=E5=8F=91=20handle?= =?UTF-8?q?r?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/web/controller/api.go | 2 -- internal/web/controller/dist.go | 48 --------------------------------- 2 files changed, 50 deletions(-) diff --git a/internal/web/controller/api.go b/internal/web/controller/api.go index 490047d..09d79ac 100644 --- a/internal/web/controller/api.go +++ b/internal/web/controller/api.go @@ -174,8 +174,6 @@ func (a *APIController) initRouter(g *gin.RouterGroup) { api.Use(middleware.ConfigEnvelopeMiddleware()) api.Use(middleware.CSRFMiddleware()) - api.GET("/openapi.json", ServeOpenAPISpec) - // Inbounds API inbounds := api.Group("/inbounds") a.inboundController = NewInboundController(inbounds) diff --git a/internal/web/controller/dist.go b/internal/web/controller/dist.go index 54cb224..67e9ff2 100644 --- a/internal/web/controller/dist.go +++ b/internal/web/controller/dist.go @@ -2,7 +2,6 @@ package controller import ( "bytes" - "encoding/json" htmlpkg "html" "io/fs" "net/http" @@ -24,53 +23,6 @@ func SetDistFS(fsys fs.FS) { var distPageBuildTime = time.Now() -// ServeOpenAPISpec returns the generated OpenAPI 3.0 description of the -// panel API. Postman / Insomnia / openapi-generator consume this URL -// directly; the in-panel Swagger UI page also fetches it. The spec is -// produced at frontend build time by scripts/build-openapi.mjs and -// embedded into the binary via the dist FS. -func ServeOpenAPISpec(c *gin.Context) { - body, err := fs.ReadFile(distFS, "dist/openapi.json") - if err != nil { - c.JSON(http.StatusNotFound, gin.H{"success": false, "msg": "openapi.json not found"}) - return - } - - // The embedded spec ships with `servers: [{url: "/"}]`. When the panel runs - // under a non-root web base path, Swagger UI "Try it out" and external - // generators must target that prefix, so rewrite the single server entry to - // the runtime base path before serving. - if basePath := c.GetString("base_path"); basePath != "" && basePath != "/" { - if rebuilt, err := withServerBasePath(body, basePath); err != nil { - logger.Warning("openapi.json: could not inject base path:", err) - } else { - body = rebuilt - } - } - - c.Header("Cache-Control", "public, max-age=300") - c.Data(http.StatusOK, "application/json; charset=utf-8", body) -} - -// withServerBasePath rewrites the spec's `servers` entry so requests target the -// panel's configured web base path. Only the top-level `servers` field is -// replaced; every other field is preserved verbatim via json.RawMessage. -func withServerBasePath(spec []byte, basePath string) ([]byte, error) { - var doc map[string]json.RawMessage - if err := json.Unmarshal(spec, &doc); err != nil { - return nil, err - } - servers, err := json.Marshal([]map[string]string{{ - "url": strings.TrimSuffix(basePath, "/"), - "description": "Current panel", - }}) - if err != nil { - return nil, err - } - doc["servers"] = servers - return json.Marshal(doc) -} - func normalizeWebBasePath(basePath string) string { if basePath == "" { return "/"