优化结构

This commit is contained in:
Yvan
2026-08-21 08:54:29 +08:00
parent c968f9f24e
commit 77e192b6c0
167 changed files with 3657 additions and 1528 deletions
@@ -0,0 +1,27 @@
package httpx
import (
"net/http"
"strings"
"time"
"github.com/gin-gonic/gin"
)
func requestUsesHTTPS(request *http.Request) bool {
if request.TLS != nil {
return true
}
forwarded := strings.SplitN(request.Header.Get("X-Forwarded-Proto"), ",", 2)[0]
return strings.EqualFold(strings.TrimSpace(forwarded), "https")
}
// SetTokenCookie also marks cookies Secure behind TLS or a TLS-terminating
// reverse proxy.
func SetTokenCookie(c *gin.Context, value string, maxAge int) {
cookie := &http.Cookie{Name: "x-token", Value: value, Path: "/", MaxAge: maxAge, Secure: requestUsesHTTPS(c.Request), HttpOnly: true, SameSite: http.SameSiteStrictMode}
if maxAge < 0 {
cookie.Expires = time.Unix(1, 0)
}
http.SetCookie(c.Writer, cookie)
}
@@ -0,0 +1,29 @@
package httpx
import (
"github.com/gin-gonic/gin"
shared "kra/pkg/httpx"
)
const (
CodeSuccess = shared.CodeSuccess
CodeError = shared.CodeError
CodePasswordChangeRequired = shared.CodePasswordChangeRequired
)
type Response = shared.Response
type PageResult = shared.PageResult
func Write(c *gin.Context, code int, data any, message string) {
shared.Write(c, code, data, message)
}
func OK(c *gin.Context) { shared.OK(c) }
func OKWithData(c *gin.Context, data any) { shared.OKWithData(c, data) }
func Fail(c *gin.Context, message string) { shared.Fail(c, message) }
func NoAuth(c *gin.Context, message string) {
shared.NoAuth(c, message)
}