feat: add openapi

This commit is contained in:
engigu
2026-03-05 20:19:35 +08:00
parent a8a24a410c
commit 9fec4ecdb5
15 changed files with 3447 additions and 22 deletions
+23
View File
@@ -2,6 +2,7 @@ package middleware
import (
"encoding/json"
"net/http"
"time"
"github.com/engigu/baihu-panel/internal/constant"
@@ -115,3 +116,25 @@ func SetAuthCookie(c *gin.Context, token string, expireDays int) {
func ClearAuthCookie(c *gin.Context) {
c.SetCookie(constant.CookieName, "", -1, "/", "", false, true)
}
// SwaggerAuth Swagger 认证中间件 (Basic Auth)
func SwaggerAuth() gin.HandlerFunc {
return func(c *gin.Context) {
cfg := services.GetConfig()
if !cfg.Swagger.Enabled {
c.Status(404)
c.Abort()
return
}
user, password, hasAuth := c.Request.BasicAuth()
if hasAuth && user == cfg.Swagger.User && password == cfg.Swagger.Password {
c.Next()
return
}
c.Header("WWW-Authenticate", `Basic realm="restricted"`)
c.Status(http.StatusUnauthorized)
c.Abort()
}
}