mirror of
https://github.com/1Panel-dev/1Panel.git
synced 2025-12-16 12:09:13 +08:00
swag
This commit is contained in:
parent
e360de89f2
commit
648511fc11
2 changed files with 65 additions and 20 deletions
|
|
@ -13,36 +13,18 @@ import (
|
|||
"github.com/1Panel-dev/1Panel/core/cmd/server/web"
|
||||
"github.com/1Panel-dev/1Panel/core/global"
|
||||
"github.com/1Panel-dev/1Panel/core/i18n"
|
||||
"github.com/1Panel-dev/1Panel/core/init/swagger"
|
||||
"github.com/1Panel-dev/1Panel/core/middleware"
|
||||
rou "github.com/1Panel-dev/1Panel/core/router"
|
||||
"github.com/1Panel-dev/1Panel/core/utils/security"
|
||||
"github.com/gin-contrib/gzip"
|
||||
"github.com/gin-gonic/gin"
|
||||
swaggerfiles "github.com/swaggo/files/v2"
|
||||
)
|
||||
|
||||
var (
|
||||
Router *gin.Engine
|
||||
)
|
||||
|
||||
func swaggerHandler() gin.HandlerFunc {
|
||||
fileServer := http.StripPrefix("/1panel/swagger/", http.FileServer(http.FS(swaggerfiles.FS)))
|
||||
|
||||
return func(c *gin.Context) {
|
||||
path := c.Request.URL.Path
|
||||
path = strings.TrimPrefix(path, "/1panel/swagger")
|
||||
switch path {
|
||||
case "/doc.json":
|
||||
c.Header("Content-Type", "application/json; charset=utf-8")
|
||||
c.Header("Cache-Control", "private, max-age=600")
|
||||
c.String(http.StatusOK, docs.SwaggerInfo.ReadDoc())
|
||||
default:
|
||||
c.Header("Cache-Control", "private, max-age=600")
|
||||
fileServer.ServeHTTP(c.Writer, c.Request)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func setWebStatic(rootRouter *gin.RouterGroup) {
|
||||
rootRouter.StaticFS("/public", http.FS(web.Favicon))
|
||||
rootRouter.StaticFS("/favicon.ico", http.FS(web.Favicon))
|
||||
|
|
@ -91,7 +73,7 @@ func Routers() *gin.Engine {
|
|||
|
||||
swaggerRouter := Router.Group("1panel")
|
||||
docs.SwaggerInfo.BasePath = "/api/v2"
|
||||
swaggerRouter.Use(middleware.SessionAuth()).GET("/swagger/*any", swaggerHandler())
|
||||
swaggerRouter.Use(middleware.SessionAuth()).GET("/swagger/*any", swagger.SwaggerHandler())
|
||||
|
||||
PublicGroup := Router.Group("")
|
||||
{
|
||||
|
|
|
|||
63
core/init/swagger/swag.go
Normal file
63
core/init/swagger/swag.go
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
package swagger
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/1Panel-dev/1Panel/core/cmd/server/docs"
|
||||
"github.com/gin-gonic/gin"
|
||||
swaggerfiles "github.com/swaggo/files/v2"
|
||||
)
|
||||
|
||||
var matcher = regexp.MustCompile(`(.*)(index\.html|index\.css|swagger-initializer\.js|doc\.json|favicon-16x16\.png|...`)
|
||||
|
||||
const SwaggerDocFile = "doc.json"
|
||||
|
||||
const CustomSwaggerInitializerJS = `window.onload = function() {
|
||||
//<editor-fold desc="Changeable Configuration Block">
|
||||
window.ui = SwaggerUIBundle({
|
||||
url: "{{.URL}}",
|
||||
dom_id: '#swagger-ui',
|
||||
deepLinking: true,
|
||||
presets: [
|
||||
SwaggerUIBundle.presets.apis,
|
||||
SwaggerUIStandalonePreset
|
||||
],
|
||||
plugins: [
|
||||
SwaggerUIBundle.plugins.DownloadUrl
|
||||
],
|
||||
layout: "StandaloneLayout"
|
||||
});
|
||||
|
||||
//</editor-fold>
|
||||
};
|
||||
`
|
||||
|
||||
func SwaggerHandler() gin.HandlerFunc {
|
||||
fileServer := http.StripPrefix("/1panel/swagger/", http.FileServer(http.FS(swaggerfiles.FS)))
|
||||
|
||||
return func(c *gin.Context) {
|
||||
path := c.Request.URL.Path
|
||||
path = strings.TrimPrefix(path, "/1panel/swagger")
|
||||
if !matcher.MatchString(path) {
|
||||
// 404
|
||||
c.AbortWithStatus(http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
switch path {
|
||||
case "/doc.json":
|
||||
c.Header("Content-Type", "application/json; charset=utf-8")
|
||||
c.Header("Cache-Control", "private, max-age=600")
|
||||
c.String(http.StatusOK, docs.SwaggerInfo.ReadDoc())
|
||||
case "/swagger-initializer.js":
|
||||
c.Header("Content-Type", "application/javascript; charset=utf-8")
|
||||
c.Header("Cache-Control", "private, max-age=600")
|
||||
c.String(http.StatusOK, strings.ReplaceAll(CustomSwaggerInitializerJS, "{{.URL}}", "/api/v2/"+SwaggerDocFile))
|
||||
default:
|
||||
|
||||
c.Header("Cache-Control", "private, max-age=600")
|
||||
fileServer.ServeHTTP(c.Writer, c.Request)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue