feat: Add Swagger UI support and update Go module dependencies

This commit is contained in:
HynoR 2025-11-24 14:22:53 +08:00
parent a853d8c869
commit 2f09afec69
3 changed files with 28 additions and 11 deletions

View file

@ -36,8 +36,7 @@ require (
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
github.com/spf13/cobra v1.8.1 github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0 github.com/spf13/viper v1.19.0
github.com/swaggo/files v1.0.1 github.com/swaggo/files/v2 v2.0.2
github.com/swaggo/gin-swagger v1.6.0
github.com/swaggo/swag v1.16.3 github.com/swaggo/swag v1.16.3
github.com/tencentyun/cos-go-sdk-v5 v0.7.54 github.com/tencentyun/cos-go-sdk-v5 v0.7.54
github.com/upyun/go-sdk v2.1.0+incompatible github.com/upyun/go-sdk v2.1.0+incompatible

View file

@ -468,10 +468,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/swaggo/files v1.0.1 h1:J1bVJ4XHZNq0I46UU90611i9/YzdrF7x92oX1ig5IdE= github.com/swaggo/files/v2 v2.0.2 h1:Bq4tgS/yxLB/3nwOMcul5oLEUKa877Ykgz3CJMVbQKU=
github.com/swaggo/files v1.0.1/go.mod h1:0qXmMNH6sXNf+73t65aKeB+ApmgxdnkQzVTAj2uaMUg= github.com/swaggo/files/v2 v2.0.2/go.mod h1:TVqetIzZsO9OhHX1Am9sRf9LdrFZqoK49N37KON/jr0=
github.com/swaggo/gin-swagger v1.6.0 h1:y8sxvQ3E20/RCyrXeFfg60r6H0Z+SwpTjMYsMm+zy8M=
github.com/swaggo/gin-swagger v1.6.0/go.mod h1:BG00cCEy294xtVpyIAHG6+e2Qzj/xKlRdOqDkvq0uzo=
github.com/swaggo/swag v1.16.3 h1:PnCYjPCah8FK4I26l2F/KQ4yz3sILcVUN3cTlBFA9Pg= github.com/swaggo/swag v1.16.3 h1:PnCYjPCah8FK4I26l2F/KQ4yz3sILcVUN3cTlBFA9Pg=
github.com/swaggo/swag v1.16.3/go.mod h1:DImHIuOFXKpMFAQjcC7FG4m3Dg4+QuUgUzJmKjI/gRk= github.com/swaggo/swag v1.16.3/go.mod h1:DImHIuOFXKpMFAQjcC7FG4m3Dg4+QuUgUzJmKjI/gRk=
github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.563/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y= github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/common v1.0.563/go.mod h1:7sCQWVkxcsR38nffDW057DRGk8mUjK1Ing/EFOK8s8Y=

View file

@ -3,6 +3,7 @@ package router
import ( import (
"encoding/base64" "encoding/base64"
"io" "io"
"io/fs"
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
@ -18,14 +19,31 @@ import (
"github.com/1Panel-dev/1Panel/core/utils/security" "github.com/1Panel-dev/1Panel/core/utils/security"
"github.com/gin-contrib/gzip" "github.com/gin-contrib/gzip"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
swaggerfiles "github.com/swaggo/files" swaggerfiles "github.com/swaggo/files/v2"
ginSwagger "github.com/swaggo/gin-swagger"
) )
var ( var (
Router *gin.Engine Router *gin.Engine
) )
func swaggerHandler() gin.HandlerFunc {
subFS, _ := fs.Sub(swaggerfiles.FS, "dist")
fileServer := http.StripPrefix("/1panel/swagger/", http.FileServer(http.FS(subFS)))
return func(c *gin.Context) {
path := c.Param("any")
switch path {
case "/", "/index.html", "":
c.Redirect(http.StatusMovedPermanently, "/1panel/swagger/index.html")
case "/doc.json":
c.Header("Content-Type", "application/json; charset=utf-8")
c.String(http.StatusOK, docs.SwaggerInfo.ReadDoc())
default:
fileServer.ServeHTTP(c.Writer, c.Request)
}
}
}
func setWebStatic(rootRouter *gin.RouterGroup) { func setWebStatic(rootRouter *gin.RouterGroup) {
rootRouter.StaticFS("/public", http.FS(web.Favicon)) rootRouter.StaticFS("/public", http.FS(web.Favicon))
rootRouter.StaticFS("/favicon.ico", http.FS(web.Favicon)) rootRouter.StaticFS("/favicon.ico", http.FS(web.Favicon))
@ -72,9 +90,11 @@ func Routers() *gin.Engine {
Router.Use(middleware.WhiteAllow()) Router.Use(middleware.WhiteAllow())
Router.Use(middleware.BindDomain()) Router.Use(middleware.BindDomain())
if false { // test
swaggerRouter := Router.Group("1panel") swaggerRouter := Router.Group("1panel")
docs.SwaggerInfo.BasePath = "/api/v2" docs.SwaggerInfo.BasePath = "/api/v2"
swaggerRouter.Use(middleware.SessionAuth()).GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler)) swaggerRouter.Use(middleware.SessionAuth()).GET("/swagger/*any", swaggerHandler())
}
PublicGroup := Router.Group("") PublicGroup := Router.Group("")
{ {
PublicGroup.Use(gzip.Gzip(gzip.DefaultCompression)) PublicGroup.Use(gzip.Gzip(gzip.DefaultCompression))