Skip to content

Commit

Permalink
feat: add Cros middleware (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tohrusky authored Jul 14, 2024
1 parent 6ddf47f commit 7352264
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 12 deletions.
2 changes: 2 additions & 0 deletions conf/nuxbt.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ server:
allowRegister: true
useInvitationCode: false
requestLimit: 50 # 50 times per minute
cros:
- https://114514.com

jwt:
timeout: 600 # minute
Expand Down
26 changes: 26 additions & 0 deletions internal/middleware/cros/cros.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cros

import (
"github.com/gin-gonic/gin"
)

// CorsByRules 按照白名单规则设置跨域
func CorsByRules(whiteList []string) gin.HandlerFunc {
return func(c *gin.Context) {
origin := c.GetHeader("origin")
for _, v := range whiteList {
if v == origin {
c.Header("Access-Control-Allow-Origin", origin)
c.Header("Access-Control-Allow-Headers",
"Content-Type, AccessToken, X-CSRF-Token, Authorization, Token, X-Token, X-User-Id")
c.Header("Access-Control-Allow-Methods", "POST, GET")
c.Header("Access-Control-Expose-Headers",
"Content-Length, Access-Control-Allow-Origin, "+
"Access-Control-Allow-Headers, Content-Type, New-Token, New-Expires-At")
c.Header("Access-Control-Allow-Credentials", "true")
break
}
}
c.Next()
}
}
12 changes: 6 additions & 6 deletions internal/middleware/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ import (
"github.com/gin-gonic/gin"
)

// LogLayout 日志layout
// LogLayout 日志 layout
type LogLayout struct {
RequestMethod string // 请求方法字段
StatusCode int // 状态码
Time time.Time
Metadata map[string]interface{} // 存储自定义原数据
Path string // 访问路径
Query string // 携带query
Body string // 携带body数据
IP string // ip地址
Query string // 携带 query
Body string // 携带 body 数据
IP string // ip 地址
UserAgent string // 代理
Error string // 错误
Cost time.Duration // 花费时间
Expand All @@ -31,7 +31,7 @@ type LogLayout struct {
type Logger struct {
// Filter 用户自定义过滤
Filter func(c *gin.Context) bool
// FilterKeyword 关键字过滤(key)
// FilterKeyword 关键字过滤 (key)
FilterKeyword func(layout *LogLayout) bool
// AuthProcess 鉴权处理
AuthProcess func(c *gin.Context, layout *LogLayout)
Expand Down Expand Up @@ -76,7 +76,7 @@ func (l Logger) SetLoggerMiddleware() gin.HandlerFunc {
l.AuthProcess(c, &layout)
}
if l.FilterKeyword != nil {
// 自行判断key/value 脱敏等
// 自行判断 key/value 脱敏等
l.FilterKeyword(&layout)
}
// 自行处理日志
Expand Down
4 changes: 3 additions & 1 deletion internal/router/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

middleware_cache "github.com/TensoRaws/NuxBT-Backend/internal/middleware/cache"
"github.com/TensoRaws/NuxBT-Backend/internal/middleware/cros"
"github.com/TensoRaws/NuxBT-Backend/internal/middleware/jwt"
"github.com/TensoRaws/NuxBT-Backend/internal/middleware/logger"
user_service "github.com/TensoRaws/NuxBT-Backend/internal/service/user"
Expand All @@ -15,7 +16,8 @@ import (

func NewAPI() *gin.Engine {
r := gin.New()
r.Use(logger.DefaultLogger(), gin.Recovery()) // 日志中间件
r.Use(cros.CorsByRules(config.ServerConfig.Cros)) // 跨域中间件
r.Use(logger.DefaultLogger(), gin.Recovery()) // 日志中间件
r.Use(middleware_cache.NewRateLimiter(cache.Clients[cache.IPLimit],
config.ServerConfig.RequestLimit, 60*time.Second)) // 限流中间件

Expand Down
1 change: 1 addition & 0 deletions module/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func initialize() {
"allowRegister": true,
"useInvitationCode": false,
"requestLimit": 50,
"cros": []string{},
})

config.SetDefault("jwt", map[string]interface{}{
Expand Down
11 changes: 6 additions & 5 deletions module/config/type.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package config

type Server struct {
Port int `yaml:"port"`
Mode string `yaml:"mode"`
AllowResgister bool `yaml:"allowResgister"`
UseInvitationCode bool `yaml:"useInvitationCode"`
RequestLimit int `yaml:"requestLimit"`
Port int `yaml:"port"`
Mode string `yaml:"mode"`
AllowResgister bool `yaml:"allowResgister"`
UseInvitationCode bool `yaml:"useInvitationCode"`
RequestLimit int `yaml:"requestLimit"`
Cros []string `yaml:"cros"`
}

type Jwt struct {
Expand Down

0 comments on commit 7352264

Please sign in to comment.