From 2ad85fd950d9ada073544867733009416e6edefd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 7 Jan 2026 23:31:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=8E=A5=E5=85=A5=E6=96=B0=E7=89=88?= =?UTF-8?q?=E6=9C=ACopenapi=E6=96=87=E6=A1=A3,=20=E9=A1=B5=E9=9D=A2?= =?UTF-8?q?=E6=B8=B2=E6=9F=93=E5=85=BC=E5=AE=B9=E6=80=A7=E5=BE=85=E8=B0=83?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- go.mod | 34 ++++++++------ go.sum | 90 +++++++++++++++++++++++------------- router/define.go | 14 ++++++ router/doc.go | 68 ++------------------------- router/option.go | 71 +++++----------------------- router/server.go | 104 ++++++++++++++++++++++++++++-------------- router/server_test.go | 13 +++++- 7 files changed, 192 insertions(+), 202 deletions(-) diff --git a/go.mod b/go.mod index be3359f..03fb433 100644 --- a/go.mod +++ b/go.mod @@ -5,8 +5,8 @@ go 1.24.1 toolchain go1.24.2 require ( - git.zhangdeman.cn/zhangdeman/api-doc v1.0.3-0.20251013152001-868ee8955623 - git.zhangdeman.cn/zhangdeman/consts v0.0.0-20260102134357-189dc7f57c1a + git.zhangdeman.cn/zhangdeman/api-doc v1.0.3-0.20260107152122-a877079712ee + git.zhangdeman.cn/zhangdeman/consts v0.0.0-20260106015608-42e268aea545 git.zhangdeman.cn/zhangdeman/dynamic-struct v0.0.0-20251207071238-9aa24be3d708 git.zhangdeman.cn/zhangdeman/exception v0.0.0-20250510123912-a0d52fc093ab git.zhangdeman.cn/zhangdeman/graceful v0.0.0-20250529070945-92833db6f3a4 @@ -15,8 +15,9 @@ require ( git.zhangdeman.cn/zhangdeman/rate_limit v0.0.0-20260104044956-ab445203aadd git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20251013044511-86c1a4a3a9dd git.zhangdeman.cn/zhangdeman/trace v0.0.0-20251013092356-b7b9fb5f8a76 - git.zhangdeman.cn/zhangdeman/util v0.0.0-20260103142706-1f6145405ec4 + git.zhangdeman.cn/zhangdeman/util v0.0.0-20260105024213-3d76b1bcde5a git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20251225094759-09c64ba2541c + github.com/getkin/kin-openapi v0.133.0 github.com/gin-contrib/pprof v1.5.3 github.com/gin-contrib/timeout v1.1.0 github.com/gin-gonic/gin v1.11.0 @@ -43,16 +44,16 @@ require ( github.com/gabriel-vasile/mimetype v1.4.12 // indirect github.com/gin-contrib/sse v1.1.0 // indirect github.com/go-ini/ini v1.67.0 // indirect - github.com/go-openapi/jsonpointer v0.22.1 // indirect - github.com/go-openapi/jsonreference v0.21.2 // indirect - github.com/go-openapi/spec v0.22.0 // indirect - github.com/go-openapi/swag/conv v0.25.1 // indirect - github.com/go-openapi/swag/jsonname v0.25.1 // indirect - github.com/go-openapi/swag/jsonutils v0.25.1 // indirect - github.com/go-openapi/swag/loading v0.25.1 // indirect - github.com/go-openapi/swag/stringutils v0.25.1 // indirect - github.com/go-openapi/swag/typeutils v0.25.1 // indirect - github.com/go-openapi/swag/yamlutils v0.25.1 // indirect + github.com/go-openapi/jsonpointer v0.22.4 // indirect + github.com/go-openapi/jsonreference v0.21.4 // indirect + github.com/go-openapi/spec v0.22.3 // indirect + github.com/go-openapi/swag/conv v0.25.4 // indirect + github.com/go-openapi/swag/jsonname v0.25.4 // indirect + github.com/go-openapi/swag/jsonutils v0.25.4 // indirect + github.com/go-openapi/swag/loading v0.25.4 // indirect + github.com/go-openapi/swag/stringutils v0.25.4 // indirect + github.com/go-openapi/swag/typeutils v0.25.4 // indirect + github.com/go-openapi/swag/yamlutils v0.25.4 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect github.com/go-redis/redis_rate/v10 v10.0.1 // indirect @@ -60,19 +61,25 @@ require ( github.com/goccy/go-json v0.10.5 // indirect github.com/goccy/go-yaml v1.19.1 // indirect github.com/gorilla/websocket v1.5.3 // indirect + github.com/josharian/intern v1.0.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.3.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible // indirect github.com/lestrrat-go/strftime v1.1.1 // indirect + github.com/mailru/easyjson v0.9.1 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.16 // indirect github.com/mitchellh/go-homedir v1.1.0 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect github.com/mozillazg/go-pinyin v0.21.0 // indirect github.com/mssola/user_agent v0.6.0 // indirect + github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 // indirect + github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 // indirect github.com/pelletier/go-toml/v2 v2.2.4 // indirect + github.com/perimeterx/marshmallow v1.1.5 // indirect github.com/pkg/errors v0.9.1 // indirect github.com/quic-go/qpack v0.6.0 // indirect github.com/quic-go/quic-go v0.58.0 // indirect @@ -88,6 +95,7 @@ require ( github.com/tidwall/pretty v1.2.1 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.3.1 // indirect + github.com/woodsbury/decimal128 v1.4.0 // indirect go.uber.org/multierr v1.11.0 // indirect go.yaml.in/yaml/v3 v3.0.4 // indirect golang.org/x/arch v0.23.0 // indirect diff --git a/go.sum b/go.sum index c0d7972..89f7b21 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,13 @@ -git.zhangdeman.cn/zhangdeman/api-doc v1.0.3-0.20251013152001-868ee8955623 h1:QiqETEQx2PBv2fF3UwPS11dsbVDcF3WNCmRRA8zrqAk= -git.zhangdeman.cn/zhangdeman/api-doc v1.0.3-0.20251013152001-868ee8955623/go.mod h1:ryyMI2gPgotFD1lZC50p2vRyX8bf3MRwO9tos41z4WU= -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20260102134357-189dc7f57c1a h1:ppfx1Yedkz8mNMi3kydXvmu+gfL2aYApp0YfWWC4rAk= -git.zhangdeman.cn/zhangdeman/consts v0.0.0-20260102134357-189dc7f57c1a/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc= +git.zhangdeman.cn/zhangdeman/api-doc v1.0.3-0.20260107135050-e98874d1ca4a h1:05TbWUkcN+VmKnnaoj2fNL7O2tK7TSHTXzuJ56IK2T4= +git.zhangdeman.cn/zhangdeman/api-doc v1.0.3-0.20260107135050-e98874d1ca4a/go.mod h1:EO06OJ2rIS+s3CaXoGiX6yTuyhKFeSrgHukOY7FofHw= +git.zhangdeman.cn/zhangdeman/api-doc v1.0.3-0.20260107151313-55fc40a43a86 h1:cAnGeZE8Qqx9S55PLtqJrffPFh2ppmk8aCRjT/3O/HM= +git.zhangdeman.cn/zhangdeman/api-doc v1.0.3-0.20260107151313-55fc40a43a86/go.mod h1:EO06OJ2rIS+s3CaXoGiX6yTuyhKFeSrgHukOY7FofHw= +git.zhangdeman.cn/zhangdeman/api-doc v1.0.3-0.20260107151524-cc15acf0fcf7 h1:LJo0jkJs3JfjbTEQIJ43bc1CUtHNUGQn//jxMa/ChSA= +git.zhangdeman.cn/zhangdeman/api-doc v1.0.3-0.20260107151524-cc15acf0fcf7/go.mod h1:EO06OJ2rIS+s3CaXoGiX6yTuyhKFeSrgHukOY7FofHw= +git.zhangdeman.cn/zhangdeman/api-doc v1.0.3-0.20260107152122-a877079712ee h1:cUrlM+87fQW8StLbGBA8EB+Ke3ZR1QslWyZqbXyD5C0= +git.zhangdeman.cn/zhangdeman/api-doc v1.0.3-0.20260107152122-a877079712ee/go.mod h1:EO06OJ2rIS+s3CaXoGiX6yTuyhKFeSrgHukOY7FofHw= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20260106015608-42e268aea545 h1:A6UeeMcSqAlHUmA2coWIuiCS/W9ySylhZMa/0HbFDcQ= +git.zhangdeman.cn/zhangdeman/consts v0.0.0-20260106015608-42e268aea545/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc= git.zhangdeman.cn/zhangdeman/dynamic-struct v0.0.0-20251207071238-9aa24be3d708 h1:yVr38JAgPwS/6JeYdHLDa8PXU3fTa2dnENL1JGdu3ns= git.zhangdeman.cn/zhangdeman/dynamic-struct v0.0.0-20251207071238-9aa24be3d708/go.mod h1:iwegDT/6mX2INyxMAYxsL9pZdpEixiu1GdpHIG2It1k= git.zhangdeman.cn/zhangdeman/easylock v0.0.0-20230731062340-983985c12eda h1:bMD6r9gjRy7cO+T4zRQVYAesgIblBdTnhzT1vN5wjvI= @@ -26,8 +32,8 @@ git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20251013044511-86c1a4a3a9dd h1:kTZ git.zhangdeman.cn/zhangdeman/serialize v0.0.0-20251013044511-86c1a4a3a9dd/go.mod h1:pLrQ63JICi81/3w2BrD26QZiu+IpddvEVfMJ6No3Xb4= git.zhangdeman.cn/zhangdeman/trace v0.0.0-20251013092356-b7b9fb5f8a76 h1:Y1GME5dg3jFmoURKvuujK07VUNMpsuRR4FkhinfBQIs= git.zhangdeman.cn/zhangdeman/trace v0.0.0-20251013092356-b7b9fb5f8a76/go.mod h1:avN/muzbNjUM2qamRZeY4fuLEmA9cmfslk1dkdtRQBE= -git.zhangdeman.cn/zhangdeman/util v0.0.0-20260103142706-1f6145405ec4 h1:x/xEkukbXS8WNN0N2A79C/q1Gh+fm3XrAdr1d/nEcXI= -git.zhangdeman.cn/zhangdeman/util v0.0.0-20260103142706-1f6145405ec4/go.mod h1:OKI+RVVfRTUf/ox9uDMydtos2YDFr+/UuU4FFZKgPYY= +git.zhangdeman.cn/zhangdeman/util v0.0.0-20260105024213-3d76b1bcde5a h1:IGUsWz204BTQlD2l4kenlwJQS4Av2RS2kfUHZ5QVrmw= +git.zhangdeman.cn/zhangdeman/util v0.0.0-20260105024213-3d76b1bcde5a/go.mod h1:OKI+RVVfRTUf/ox9uDMydtos2YDFr+/UuU4FFZKgPYY= git.zhangdeman.cn/zhangdeman/websocket v0.0.0-20251013144324-313024336a6f h1:InxNoLPHBLwCLblW0lsL2P1ZBYoUEzw9Yh+KNLt4Xmg= git.zhangdeman.cn/zhangdeman/websocket v0.0.0-20251013144324-313024336a6f/go.mod h1:Pbs7tusW6RNcqrNCVcLE2zrM8JfPaO7lBJQuRiAzzLs= git.zhangdeman.cn/zhangdeman/wrapper v0.0.0-20251225094759-09c64ba2541c h1:lTShwLIIBojEInQlQYmKvpoio5Srasn05bPyBIEXBsY= @@ -59,6 +65,8 @@ github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/r github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc= github.com/gabriel-vasile/mimetype v1.4.12 h1:e9hWvmLYvtp846tLHam2o++qitpguFiYCKbn0w9jyqw= github.com/gabriel-vasile/mimetype v1.4.12/go.mod h1:d+9Oxyo1wTzWdyVUPMmXFvp4F9tea18J8ufA774AB3s= +github.com/getkin/kin-openapi v0.133.0 h1:pJdmNohVIJ97r4AUFtEXRXwESr8b0bD721u/Tz6k8PQ= +github.com/getkin/kin-openapi v0.133.0/go.mod h1:boAciF6cXk5FhPqe/NQeBTeenbjqU4LhWBf09ILVvWE= github.com/gin-contrib/gzip v0.0.6 h1:NjcunTcGAj5CO1gn4N8jHOSIeRFHIbn51z6K+xaN4d4= github.com/gin-contrib/gzip v0.0.6/go.mod h1:QOJlmV2xmayAjkNS2Y8NQsMneuRShOU/kjovCXNuzzk= github.com/gin-contrib/pprof v1.5.3 h1:Bj5SxJ3kQDVez/s/+f9+meedJIqLS+xlkIVDe/lcvgM= @@ -71,29 +79,33 @@ github.com/gin-gonic/gin v1.11.0 h1:OW/6PLjyusp2PPXtyxKHU0RbX6I/l28FTdDlae5ueWk= github.com/gin-gonic/gin v1.11.0/go.mod h1:+iq/FyxlGzII0KHiBGjuNn4UNENUlKbGlNmc+W50Dls= github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A= github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8= -github.com/go-openapi/jsonpointer v0.22.1 h1:sHYI1He3b9NqJ4wXLoJDKmUmHkWy/L7rtEo92JUxBNk= -github.com/go-openapi/jsonpointer v0.22.1/go.mod h1:pQT9OsLkfz1yWoMgYFy4x3U5GY5nUlsOn1qSBH5MkCM= -github.com/go-openapi/jsonreference v0.21.2 h1:Wxjda4M/BBQllegefXrY/9aq1fxBA8sI5M/lFU6tSWU= -github.com/go-openapi/jsonreference v0.21.2/go.mod h1:pp3PEjIsJ9CZDGCNOyXIQxsNuroxm8FAJ/+quA0yKzQ= -github.com/go-openapi/spec v0.22.0 h1:xT/EsX4frL3U09QviRIZXvkh80yibxQmtoEvyqug0Tw= -github.com/go-openapi/spec v0.22.0/go.mod h1:K0FhKxkez8YNS94XzF8YKEMULbFrRw4m15i2YUht4L0= -github.com/go-openapi/swag v0.19.15 h1:D2NRCBzS9/pEY3gP9Nl8aDqGUcPFrwG2p+CNFrLyrCM= -github.com/go-openapi/swag/conv v0.25.1 h1:+9o8YUg6QuqqBM5X6rYL/p1dpWeZRhoIt9x7CCP+he0= -github.com/go-openapi/swag/conv v0.25.1/go.mod h1:Z1mFEGPfyIKPu0806khI3zF+/EUXde+fdeksUl2NiDs= -github.com/go-openapi/swag/jsonname v0.25.1 h1:Sgx+qbwa4ej6AomWC6pEfXrA6uP2RkaNjA9BR8a1RJU= -github.com/go-openapi/swag/jsonname v0.25.1/go.mod h1:71Tekow6UOLBD3wS7XhdT98g5J5GR13NOTQ9/6Q11Zo= -github.com/go-openapi/swag/jsonutils v0.25.1 h1:AihLHaD0brrkJoMqEZOBNzTLnk81Kg9cWr+SPtxtgl8= -github.com/go-openapi/swag/jsonutils v0.25.1/go.mod h1:JpEkAjxQXpiaHmRO04N1zE4qbUEg3b7Udll7AMGTNOo= -github.com/go-openapi/swag/jsonutils/fixtures_test v0.25.1 h1:DSQGcdB6G0N9c/KhtpYc71PzzGEIc/fZ1no35x4/XBY= -github.com/go-openapi/swag/jsonutils/fixtures_test v0.25.1/go.mod h1:kjmweouyPwRUEYMSrbAidoLMGeJ5p6zdHi9BgZiqmsg= -github.com/go-openapi/swag/loading v0.25.1 h1:6OruqzjWoJyanZOim58iG2vj934TysYVptyaoXS24kw= -github.com/go-openapi/swag/loading v0.25.1/go.mod h1:xoIe2EG32NOYYbqxvXgPzne989bWvSNoWoyQVWEZicc= -github.com/go-openapi/swag/stringutils v0.25.1 h1:Xasqgjvk30eUe8VKdmyzKtjkVjeiXx1Iz0zDfMNpPbw= -github.com/go-openapi/swag/stringutils v0.25.1/go.mod h1:JLdSAq5169HaiDUbTvArA2yQxmgn4D6h4A+4HqVvAYg= -github.com/go-openapi/swag/typeutils v0.25.1 h1:rD/9HsEQieewNt6/k+JBwkxuAHktFtH3I3ysiFZqukA= -github.com/go-openapi/swag/typeutils v0.25.1/go.mod h1:9McMC/oCdS4BKwk2shEB7x17P6HmMmA6dQRtAkSnNb8= -github.com/go-openapi/swag/yamlutils v0.25.1 h1:mry5ez8joJwzvMbaTGLhw8pXUnhDK91oSJLDPF1bmGk= -github.com/go-openapi/swag/yamlutils v0.25.1/go.mod h1:cm9ywbzncy3y6uPm/97ysW8+wZ09qsks+9RS8fLWKqg= +github.com/go-openapi/jsonpointer v0.22.4 h1:dZtK82WlNpVLDW2jlA1YCiVJFVqkED1MegOUy9kR5T4= +github.com/go-openapi/jsonpointer v0.22.4/go.mod h1:elX9+UgznpFhgBuaMQ7iu4lvvX1nvNsesQ3oxmYTw80= +github.com/go-openapi/jsonreference v0.21.4 h1:24qaE2y9bx/q3uRK/qN+TDwbok1NhbSmGjjySRCHtC8= +github.com/go-openapi/jsonreference v0.21.4/go.mod h1:rIENPTjDbLpzQmQWCj5kKj3ZlmEh+EFVbz3RTUh30/4= +github.com/go-openapi/spec v0.22.3 h1:qRSmj6Smz2rEBxMnLRBMeBWxbbOvuOoElvSvObIgwQc= +github.com/go-openapi/spec v0.22.3/go.mod h1:iIImLODL2loCh3Vnox8TY2YWYJZjMAKYyLH2Mu8lOZs= +github.com/go-openapi/swag v0.23.0 h1:vsEVJDUo2hPJ2tu0/Xc+4noaxyEffXNIs3cOULZ+GrE= +github.com/go-openapi/swag/conv v0.25.4 h1:/Dd7p0LZXczgUcC/Ikm1+YqVzkEeCc9LnOWjfkpkfe4= +github.com/go-openapi/swag/conv v0.25.4/go.mod h1:3LXfie/lwoAv0NHoEuY1hjoFAYkvlqI/Bn5EQDD3PPU= +github.com/go-openapi/swag/jsonname v0.25.4 h1:bZH0+MsS03MbnwBXYhuTttMOqk+5KcQ9869Vye1bNHI= +github.com/go-openapi/swag/jsonname v0.25.4/go.mod h1:GPVEk9CWVhNvWhZgrnvRA6utbAltopbKwDu8mXNUMag= +github.com/go-openapi/swag/jsonutils v0.25.4 h1:VSchfbGhD4UTf4vCdR2F4TLBdLwHyUDTd1/q4i+jGZA= +github.com/go-openapi/swag/jsonutils v0.25.4/go.mod h1:7OYGXpvVFPn4PpaSdPHJBtF0iGnbEaTk8AvBkoWnaAY= +github.com/go-openapi/swag/jsonutils/fixtures_test v0.25.4 h1:IACsSvBhiNJwlDix7wq39SS2Fh7lUOCJRmx/4SN4sVo= +github.com/go-openapi/swag/jsonutils/fixtures_test v0.25.4/go.mod h1:Mt0Ost9l3cUzVv4OEZG+WSeoHwjWLnarzMePNDAOBiM= +github.com/go-openapi/swag/loading v0.25.4 h1:jN4MvLj0X6yhCDduRsxDDw1aHe+ZWoLjW+9ZQWIKn2s= +github.com/go-openapi/swag/loading v0.25.4/go.mod h1:rpUM1ZiyEP9+mNLIQUdMiD7dCETXvkkC30z53i+ftTE= +github.com/go-openapi/swag/stringutils v0.25.4 h1:O6dU1Rd8bej4HPA3/CLPciNBBDwZj9HiEpdVsb8B5A8= +github.com/go-openapi/swag/stringutils v0.25.4/go.mod h1:GTsRvhJW5xM5gkgiFe0fV3PUlFm0dr8vki6/VSRaZK0= +github.com/go-openapi/swag/typeutils v0.25.4 h1:1/fbZOUN472NTc39zpa+YGHn3jzHWhv42wAJSN91wRw= +github.com/go-openapi/swag/typeutils v0.25.4/go.mod h1:Ou7g//Wx8tTLS9vG0UmzfCsjZjKhpjxayRKTHXf2pTE= +github.com/go-openapi/swag/yamlutils v0.25.4 h1:6jdaeSItEUb7ioS9lFoCZ65Cne1/RZtPBZ9A56h92Sw= +github.com/go-openapi/swag/yamlutils v0.25.4/go.mod h1:MNzq1ulQu+yd8Kl7wPOut/YHAAU/H6hL91fF+E2RFwc= +github.com/go-openapi/testify/enable/yaml/v2 v2.0.2 h1:0+Y41Pz1NkbTHz8NngxTuAXxEodtNSI1WG1c/m5Akw4= +github.com/go-openapi/testify/enable/yaml/v2 v2.0.2/go.mod h1:kme83333GCtJQHXQ8UKX3IBZu6z8T5Dvy5+CW3NLUUg= +github.com/go-openapi/testify/v2 v2.0.2 h1:X999g3jeLcoY8qctY/c/Z8iBHTbwLz7R2WXd6Ub6wls= +github.com/go-openapi/testify/v2 v2.0.2/go.mod h1:HCPmvFFnheKK2BuwSA0TbbdxJ3I16pjwMkYkP4Ywn54= github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s= github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4= github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA= @@ -104,6 +116,8 @@ github.com/go-playground/validator/v10 v10.30.1 h1:f3zDSN/zOma+w6+1Wswgd9fLkdwy0 github.com/go-playground/validator/v10 v10.30.1/go.mod h1:oSuBIQzuJxL//3MelwSLD5hc2Tu889bF0Idm9Dg26cM= github.com/go-redis/redis_rate/v10 v10.0.1 h1:calPxi7tVlxojKunJwQ72kwfozdy25RjA0bCj1h0MUo= github.com/go-redis/redis_rate/v10 v10.0.1/go.mod h1:EMiuO9+cjRkR7UvdvwMO7vbgqJkltQHtwbdIQvaBKIU= +github.com/go-test/deep v1.0.8 h1:TDsG77qcSprGbC6vTN8OuXp5g+J+b5Pcguhf7Zt61VM= +github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE= github.com/go-webtools/knife4go v1.0.4 h1:p32SApmM0sx2/Y5p0QfeaGv5KD96R1mj2CaHdyH8jy8= github.com/go-webtools/knife4go v1.0.4/go.mod h1:trOlXN1tqBJ7R44sHON3exGvzCwjbsVriIHEenry3d8= github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4= @@ -123,6 +137,8 @@ github.com/jedib0t/go-pretty/v6 v6.7.8 h1:BVYrDy5DPBA3Qn9ICT+PokP9cvCv1KaHv2i+Hc github.com/jedib0t/go-pretty/v6 v6.7.8/go.mod h1:YwC5CE4fJ1HFUDeivSV1r//AmANFHyqczZk+U6BDALU= github.com/jonboulle/clockwork v0.3.0 h1:9BSCMi8C+0qdApAp4auwX0RkLGUjs956h0EkuQymUhg= github.com/jonboulle/clockwork v0.3.0/go.mod h1:Pkfl5aHPm1nk2H9h0bjmnJD/BcgbGXUBGnn1kMkgxc8= +github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= +github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= @@ -143,6 +159,8 @@ github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible h1:Y6sqxHMyB1D2YSzWkL github.com/lestrrat-go/file-rotatelogs v2.4.0+incompatible/go.mod h1:ZQnN8lSECaebrkQytbHj4xNgtg8CR7RYXnPok8e0EHA= github.com/lestrrat-go/strftime v1.1.1 h1:zgf8QCsgj27GlKBy3SU9/8MMgegZ8UCzlCyHYrUF0QU= github.com/lestrrat-go/strftime v1.1.1/go.mod h1:YDrzHJAODYQ+xxvrn5SG01uFIQAeDTzpxNVppCz7Nmw= +github.com/mailru/easyjson v0.9.1 h1:LbtsOm5WAswyWbvTEOqhypdPeZzHavpZx96/n553mR8= +github.com/mailru/easyjson v0.9.1/go.mod h1:1+xMtQp2MRNVL/V1bOzuP3aP8VNwRW55fQUto+XFtTU= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc= @@ -156,13 +174,21 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 h1:RWengNIwukTxcDr9M+97sNutRR1RKhG96O6jWumTTnw= +github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8= github.com/mozillazg/go-pinyin v0.21.0 h1:Wo8/NT45z7P3er/9YSLHA3/kjZzbLz5hR7i+jGeIGao= github.com/mozillazg/go-pinyin v0.21.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc= github.com/mssola/user_agent v0.6.0 h1:uwPR4rtWlCHRFyyP9u2KOV0u8iQXmS7Z7feTrstQwk4= github.com/mssola/user_agent v0.6.0/go.mod h1:TTPno8LPY3wAIEKRpAtkdMT0f8SE24pLRGPahjCH4uw= github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno= +github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037 h1:G7ERwszslrBzRxj//JalHPu/3yz+De2J+4aLtSRlHiY= +github.com/oasdiff/yaml v0.0.0-20250309154309-f31be36b4037/go.mod h1:2bpvgLBZEtENV5scfDFEtB/5+1M4hkQhDQrccEJ/qGw= +github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90 h1:bQx3WeLcUWy+RletIKwUIt4x3t8n2SxavmoclizMb8c= +github.com/oasdiff/yaml3 v0.0.0-20250309153720-d2182401db90/go.mod h1:y5+oSEHCPT/DGrS++Wc/479ERge0zTFxaF8PbGKcg2o= github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4= github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY= +github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s= +github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= @@ -176,8 +202,8 @@ github.com/redis/go-redis/v9 v9.17.2/go.mod h1:u410H11HMLoB+TP67dz8rL9s6QW2j76l0 github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= -github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= -github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= +github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8= +github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4= github.com/sbabiv/xml2map v1.2.1 h1:1lT7t0hhUvXZCkdxqtq4n8/ZCnwLWGq4rDuDv5XOoFE= github.com/sbabiv/xml2map v1.2.1/go.mod h1:2TPoAfcaM7+Sd4iriPvzyntb2mx7GY+kkQpB/GQa/eo= github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY= @@ -215,6 +241,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY= github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4= +github.com/woodsbury/decimal128 v1.4.0 h1:xJATj7lLu4f2oObouMt2tgGiElE5gO6mSWUjQsBgUlc= +github.com/woodsbury/decimal128 v1.4.0/go.mod h1:BP46FUrVjVhdTbKT+XuQh2xfQaGki9LMIRJSFuh6THU= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= diff --git a/router/define.go b/router/define.go index 1a4c3b6..759fa82 100644 --- a/router/define.go +++ b/router/define.go @@ -7,6 +7,8 @@ // Date : 2024-07-20 22:57 package router +import "github.com/getkin/kin-openapi/openapi3" + const ( PrefixFuncName = "RouterPrefix" // 路由前缀函数名称 MiddlewareFuncName = "RouterMiddleware" // 路由中间件函数名称 @@ -46,3 +48,15 @@ type UriParam struct { const ( FieldNameMeta = "Meta" // 元信息字段 ) + +// DocConfig 文档配置 +type DocConfig struct { + Enable bool `json:"enable" toml:"enable" yaml:"enable" dc:"是否启用文档"` + UiTheme string `json:"ui_theme" toml:"ui_theme" yaml:"ui_theme" dc:"文档主题"` + BaseUri string `json:"base_uri" toml:"base_uri" yaml:"base_uri" dc:"文档基础Uri"` + Flag string `json:"flag" toml:"flag" yaml:"flag" dc:"文档标识"` + ServerList *openapi3.Servers `json:"server_list" toml:"server_list" yaml:"server_list" dc:"服务环境列表"` + Info *openapi3.Info `json:"info" toml:"info" yaml:"info" dc:"基础信息"` + SecuritySchemes *openapi3.SecuritySchemes `json:"security_schemes" toml:"security_schemes" yaml:"security_schemes" dc:"服务安全策略"` + CommonParameter *openapi3.ParametersMap `json:"common_parameter" toml:"common_parameter" yaml:"common_parameter" dc:"基础公共参数"` +} diff --git a/router/doc.go b/router/doc.go index acbaa25..d77507e 100644 --- a/router/doc.go +++ b/router/doc.go @@ -8,70 +8,10 @@ package router import ( - "reflect" - - apiDoc "git.zhangdeman.cn/zhangdeman/api-doc" - "git.zhangdeman.cn/zhangdeman/api-doc/define" + "git.zhangdeman.cn/zhangdeman/api-doc/openapi" + "github.com/getkin/kin-openapi/openapi3" ) -func NewDoc(info *define.Info, servers []*define.ServerItem) *Doc { - if nil == info { - info = &define.Info{ - Description: "", - Title: "", - TermsOfService: "", - Contact: &define.Contact{ - Name: "", - Url: "", - Email: "", - }, - License: nil, - Version: "", - } - } - if nil == info.Contact { - info.Contact = &define.Contact{ - Name: "", - Url: "", - Email: "", - } - } - if nil == info.License { - info.License = &define.License{ - Name: "", - Url: "", - } - } - return &Doc{ - instance: apiDoc.NewOpenapiDoc( - apiDoc.WithDocDescription(info.Description), - apiDoc.WithDocTitle(info.Title), - apiDoc.WithDocContactEmail(info.Contact.Email), - apiDoc.WithDocContactName(info.Contact.Name), - apiDoc.WithDocLicense(info.License.Name), - apiDoc.WithDocServers(servers), - ), - } -} - -type Doc struct { - instance *apiDoc.Generate -} - -// Add 增加接口文档测试 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 21:55 2025/2/14 -func (d *Doc) Add(routePrefix string, paramType reflect.Type, resultType reflect.Type) { - _ = d.instance.AddApiFromInAndOut(routePrefix, paramType, resultType) -} - -// Data 文档数据 -// -// Author : go_developer@163.com<白茶清欢> -// -// Date : 21:59 2025/2/14 -func (d *Doc) Data() *define.OpenapiDoc { - return d.instance.Doc() +func NewOpenApiDoc(docFlag string, docOption ...openapi.OptionFunc) *openapi3.T { + return openapi.DocManager.NewOpenApiDoc(docFlag, docOption...) } diff --git a/router/option.go b/router/option.go index d95697d..b79ce4a 100644 --- a/router/option.go +++ b/router/option.go @@ -8,9 +8,6 @@ package router import ( - "strings" - - apiDocDefine "git.zhangdeman.cn/zhangdeman/api-doc/define" "git.zhangdeman.cn/zhangdeman/gin/middleware" "git.zhangdeman.cn/zhangdeman/rate_limit/abstract" "github.com/gin-gonic/gin" @@ -20,18 +17,14 @@ type SetServerOptionFunc func(so *serverOption) // serverOption 获取server实例的选项 type serverOption struct { - swaggerUiTheme string // swagger 主题 - swaggerBaseUri string // swagger 基础path - globalMiddlewareList []gin.HandlerFunc // 全局中间件列表 - disableSwaggerDoc bool // 禁用swagger文档, 特定环境不想展示文档, 可通过次方式禁用 - serverInfo *apiDocDefine.Info // 服务器信息 - serverList []*apiDocDefine.ServerItem // 服务器环境列表 - enablePprof bool // 启用 pprof - enableCors bool // 启动跨域支持 - disableInitRequest bool // 禁用初始化请求 - loggerCfg *middleware.AccessConfig // 日志配置 - initContextData gin.HandlerFunc // 初始化一些请求数据 - rateLimitInstance abstract.IRateLimit // 服务流控实例 + docConfig DocConfig // 文档配置 + globalMiddlewareList []gin.HandlerFunc // 全局中间件列表 + enablePprof bool // 启用 pprof + enableCors bool // 启动跨域支持 + disableInitRequest bool // 禁用初始化请求 + loggerCfg *middleware.AccessConfig // 日志配置 + initContextData gin.HandlerFunc // 初始化一些请求数据 + rateLimitInstance abstract.IRateLimit // 服务流控实例 } // WithRateLimitInstance 设置流控实例, 配置为 nil, 代表禁用 @@ -59,14 +52,13 @@ func WithInitContextData(formatFunc func(ctx *gin.Context)) SetServerOptionFunc } } -// WithSwaggerUITheme 设置swaggerUI主题 -func WithSwaggerUITheme(uiTheme string) SetServerOptionFunc { +// WithDocConfig 设置文档配置 +func WithDocConfig(docConfig *DocConfig) SetServerOptionFunc { return func(so *serverOption) { - uiTheme = strings.TrimSpace(uiTheme) - if len(uiTheme) == 0 { + if nil == docConfig { return } - so.swaggerUiTheme = uiTheme + so.docConfig = *docConfig } } @@ -77,45 +69,6 @@ func WithGlobalMiddlewareList(middlewareList ...gin.HandlerFunc) SetServerOption } } -// WithSwaggerBaseUri ... -func WithSwaggerBaseUri(baseUri string) SetServerOptionFunc { - return func(so *serverOption) { - baseUri = strings.TrimSpace(baseUri) - if len(baseUri) == 0 { - return - } - baseUri = "/" + strings.TrimLeft(baseUri, "/") - so.swaggerBaseUri = baseUri - } -} - -// WithDisableSwaggerDoc 禁用swagger文档 -func WithDisableSwaggerDoc() SetServerOptionFunc { - return func(so *serverOption) { - so.disableSwaggerDoc = true - } -} - -// WithServerInfo 设置serverInfo -func WithServerInfo(serverInfo *apiDocDefine.Info) SetServerOptionFunc { - return func(so *serverOption) { - if nil == serverInfo { - return - } - so.serverInfo = serverInfo - } -} - -// WithServerList 设置服务器列表 -func WithServerList(serverList []*apiDocDefine.ServerItem) SetServerOptionFunc { - return func(so *serverOption) { - if len(serverList) == 0 { - return - } - so.serverList = serverList - } -} - // WithPprofEnable 启用pprof func WithPprofEnable() SetServerOptionFunc { return func(so *serverOption) { diff --git a/router/server.go b/router/server.go index 9176d7f..1a15b74 100644 --- a/router/server.go +++ b/router/server.go @@ -16,19 +16,19 @@ import ( "strings" "sync" + apiDoc "git.zhangdeman.cn/zhangdeman/api-doc" + apiDocDefine "git.zhangdeman.cn/zhangdeman/api-doc/define" + "git.zhangdeman.cn/zhangdeman/api-doc/enums" + "git.zhangdeman.cn/zhangdeman/api-doc/openapi" "git.zhangdeman.cn/zhangdeman/gin/define" "git.zhangdeman.cn/zhangdeman/graceful" "github.com/jedib0t/go-pretty/v6/table" "github.com/jedib0t/go-pretty/v6/text" - apiDoc "git.zhangdeman.cn/zhangdeman/api-doc" - "git.zhangdeman.cn/zhangdeman/consts" "git.zhangdeman.cn/zhangdeman/gin/middleware" "git.zhangdeman.cn/zhangdeman/gin/middleware/request_cors" "github.com/gin-contrib/pprof" - apiDocDefine "git.zhangdeman.cn/zhangdeman/api-doc/define" - apiDocEnum "git.zhangdeman.cn/zhangdeman/api-doc/enums" "github.com/gin-gonic/gin" ) @@ -39,32 +39,7 @@ func NewServerOption(port int, optionList ...SetServerOptionFunc) *serverOption func newServerOption(port int, optionList ...SetServerOptionFunc) *serverOption { option := &serverOption{ - swaggerUiTheme: apiDocEnum.SwaggerUIThemeRedocFree.String(), - swaggerBaseUri: "/doc/swagger", globalMiddlewareList: nil, - disableSwaggerDoc: false, - serverInfo: &apiDocDefine.Info{ - Description: "这是一个微服务,提供一些必要的的数据接口功能", - Title: "微服务接口文档", - TermsOfService: "", - Contact: &apiDocDefine.Contact{ - Name: "开发人员", - Url: "", - Email: "developer@example.com", - }, - License: &apiDocDefine.License{ - Name: consts.LicenseApache20, - Url: consts.LicenseUrlTable[consts.LicenseApache20], - }, - Version: "0.0.1", - }, - serverList: []*apiDocDefine.ServerItem{ - { - Url: fmt.Sprintf("http://127.0.0.1:%d", port), - Description: "测试服务器", - Variables: nil, - }, - }, } for _, opt := range optionList { if nil == opt { @@ -83,9 +58,28 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server { gin.SetMode(gin.ReleaseMode) option := newServerOption(port, optionList...) + // 启用文档, 初始化文档 + if option.docConfig.Enable { + if len(option.docConfig.BaseUri) == 0 { + option.docConfig.BaseUri = "/_doc/swagger" // 未指定文档基础路径, 则以默认值作为基础路径 + } + if len(option.docConfig.Flag) == 0 { + option.docConfig.Flag = fmt.Sprintf("%v", port) // 未指定文档实例标识, 则以监听端口作为实例 + } + // base_uri 拼接 文档标识, 作为最终 baseUri + option.docConfig.BaseUri = fmt.Sprintf("%v/%v", strings.TrimRight(option.docConfig.BaseUri, "/"), strings.Trim(option.docConfig.Flag, "/")) + optionFuncList := []openapi.OptionFunc{ + openapi.WithInfo(option.docConfig.Info), + openapi.WithSecurity(option.docConfig.SecuritySchemes), + openapi.WithCommonParameter(option.docConfig.CommonParameter), + } + if nil != option.docConfig.ServerList { + optionFuncList = append(optionFuncList, openapi.WithServers(*option.docConfig.ServerList)) + } + openapi.DocManager.NewOpenApiDoc(option.docConfig.Flag, optionFuncList...) + } s := &server{ router: gin.Default(), - uiInstance: apiDoc.NewSwaggerUI(option.serverInfo, option.serverList, apiDocEnum.SwaggerUITheme(option.swaggerUiTheme)), port: port, option: option, lock: &sync.RWMutex{}, @@ -108,7 +102,6 @@ func NewServer(port int, optionList ...SetServerOptionFunc) *server { type server struct { router *gin.Engine port int - uiInstance *apiDoc.SwaggerUI option *serverOption commonParam map[string]GetCommonParam // 结构体字段名, 注意, 不是TAG lock *sync.RWMutex @@ -157,10 +150,48 @@ func (s *server) getGlobalMiddlewareList(option *serverOption) { s.globalMiddlewareDescList = s.getMiddlewareDescList(s.globalMiddlewareList) } +// RegisterDocHandler 注册文档路由 +func (s *server) RegisterDocHandler() { + if !s.option.docConfig.Enable { + return + } + + s.router.GET(s.option.docConfig.BaseUri+"/*any", func(ctx *gin.Context) { + if ctx.Request.RequestURI == s.option.docConfig.BaseUri+"/doc.json" || ctx.Request.RequestURI == s.option.docConfig.BaseUri+"/knife4go/doc.json" { + // 默认swagger, 通过此接口读取文档数据 + ctx.JSON(http.StatusOK, openapi.DocManager.DocData(s.option.docConfig.Flag)) + ctx.Abort() + } + if ctx.Request.RequestURI == "/doc/swagger/openapi.json" { + // knife4go 文档通过此接口读取文档列表 + ctx.JSON(http.StatusOK, []map[string]any{ + { + "name": "服务文档", + "url": "doc.json", + "swaggerVersion": enums.SwaggerDocVersion3.String(), + }, + }) + ctx.Abort() + } + }, apiDoc.NewSwaggerUI(fmt.Sprintf("[%v]接口文档", s.option.docConfig.Flag), s.option.docConfig.BaseUri, enums.SwaggerUITheme(s.option.docConfig.UiTheme)).Handler()) + s.router.GET("/swagger-resources", func(ctx *gin.Context) { // lucky UI获取分组信息 + ctx.Writer.Header().Set("Access-Control-Allow-Origin", "*") // 允许访问所有域 + ctx.JSON(http.StatusOK, []map[string]any{ + { + "name": "服务文档", + "url": s.option.docConfig.BaseUri + "/doc.json", + "swaggerVersion": enums.SwaggerDocVersion3.String(), + }, + }) + // ctx.JSON(http.StatusOK, swaggerInstance.docInstance.Data()) + }) +} + // Start 启动服务 func (s *server) Start() { // 注册文档 - s.uiInstance.RegisterHandler(s.router, s.option.swaggerBaseUri) + s.RegisterDocHandler() + // s.uiInstance.RegisterHandler(s.router, s.option.swaggerBaseUri) gracefulServer := graceful.NewServer(fmt.Sprintf(":%d", s.port), s.Router()) defer func() { _ = gracefulServer.Close() @@ -215,7 +246,7 @@ func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, co // 设置 logic 函数描述 apiMiddlewareList = append(apiMiddlewareList, runtime.FuncForPC(itemUriCfg.ApiLogicFunc.Func.Pointer()).Name()) - _ = s.uiInstance.DocInstance().AddApiFromInAndOut(routerPrefix, itemUriCfg.FormDataType, itemUriCfg.ResultDataType) + // _ = s.uiInstance.DocInstance().AddApiFromInAndOut(routerPrefix, itemUriCfg.FormDataType, itemUriCfg.ResultDataType) // 普通 HTTP 请求 handleFunc := s.RequestHandler(itemUriCfg) if itemUriCfg.IsSse { @@ -228,6 +259,13 @@ func (s *server) Group(routerPrefix string, middlewareList []gin.HandlerFunc, co routerPrefix = "/" + strings.TrimSuffix(strings.TrimPrefix(routerPrefix, "/"), "/") } fullUriPath := routerPrefix + "/" + strings.TrimPrefix(itemUriCfg.Path, "/") + // 注册接口文档 + _ = openapi.DocManager.AddApiDoc(s.option.docConfig.Flag, apiDocDefine.UriConfig{ + Path: fullUriPath, + RequestMethod: method, + TagList: itemUriCfg.TagList, + Desc: itemUriCfg.Desc, + }, itemUriCfg.FormDataType, itemUriCfg.ResultDataType) s.uriTable[fullUriPath] = itemUriCfg // 注册路由时存储 接口路径 => 接口配置的信息 s.consoleOutput = append(s.consoleOutput, []any{ fullUriPath, diff --git a/router/server_test.go b/router/server_test.go index a75e433..ce330e2 100644 --- a/router/server_test.go +++ b/router/server_test.go @@ -19,13 +19,22 @@ type testCommon struct { } type testForm struct { - Meta `json:"-" method:"get" path:"test" rate-limit:"1/5/60"` + Meta `json:"-" method:"get" path:"test" rate-limit:"1/5/60" tag:"测试,验证" summary:"本地调试"` testCommon Name string `json:"name"` } func TestNewServer(t *testing.T) { - s := NewServer(9087, WithRateLimitInstance(rate_limit.MemoryClient)) + s := NewServer(9087, WithRateLimitInstance(rate_limit.MemoryClient), WithDocConfig(&DocConfig{ + Enable: true, + UiTheme: "ydoc-lucky-ui", + BaseUri: "", + Flag: "test-server", + ServerList: nil, + Info: nil, + SecuritySchemes: nil, + CommonParameter: nil, + })) s.AddCommonParamRule("UserID", func(ctx *gin.Context) (any, error) { return uint(123456), nil })