This repository has been archived on 2026-01-07. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Game/Server/gateway/internal/net/http_gateway/middleward.go

68 lines
1.3 KiB
Go

package http_gateway
import (
"common/net/http/http_resp"
"common/utils"
"fmt"
"gateway/config"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"strconv"
"strings"
"time"
)
func corsConfig() cors.Config {
return cors.Config{
AllowMethods: []string{"GET", "POST", "OPTIONS"},
AllowHeaders: []string{"Content-Type", "Authorization"},
AllowCredentials: false,
AllowAllOrigins: true,
MaxAge: 12 * time.Hour,
}
}
func ginLogger(logger *zap.SugaredLogger) gin.HandlerFunc {
return func(c *gin.Context) {
start := time.Now()
path := c.Request.URL.Path
c.Next()
cost := time.Since(start)
logger.Infof(fmt.Sprintf(
"HTTP Method:%v Code:%v Time:%v IP:%v Path:%v",
c.Request.Method,
c.Writer.Status(),
cost,
c.ClientIP(),
path),
)
}
}
func authJwt() gin.HandlerFunc {
return func(c *gin.Context) {
authHeader := c.GetHeader("Authorization")
if authHeader == "" {
http_resp.AbortUnauthorized(c)
return
}
parts := strings.Split(authHeader, " ")
if len(parts) != 2 || strings.ToLower(parts[0]) != "bearer" {
http_resp.AbortUnauthorized(c)
return
}
claims, err := utils.ParseToken(parts[1], config.Get().Auth.Secret)
if err != nil {
http_resp.AbortUnauthorized(c)
return
}
c.Request.Header.Set("X-Usn", strconv.Itoa(int(claims.USN)))
c.Next()
}
}