fix: 修复 golangci-lint 检查错误

- SA1029: 创建 ctxkey 包定义类型安全的 context key
- ST1005: 错误字符串首字母改小写
- errcheck: 显式忽略 bytes.Buffer.Write 返回值
- 修复单元测试中 GatewayService 缺少 cfg 字段的问题
This commit is contained in:
song
2025-12-29 17:46:52 +08:00
parent eec8b4c91e
commit 21a04332ec
9 changed files with 101 additions and 73 deletions

View File

@@ -3,6 +3,7 @@ package middleware
import (
"context"
"github.com/Wei-Shaw/sub2api/internal/pkg/ctxkey"
"github.com/gin-gonic/gin"
)
@@ -22,16 +23,12 @@ const (
ContextKeyForcePlatform ContextKey = "force_platform"
)
// ctxKeyForcePlatformStr 用于 request.Context 的字符串 key供 Service 读取)
// 注意service 包中也需要使用相同的字符串 "ctx_force_platform"
const ctxKeyForcePlatformStr = "ctx_force_platform"
// ForcePlatform 返回设置强制平台的中间件
// 同时设置 request.Context供 Service 使用)和 gin.Context供 Handler 快速检查)
func ForcePlatform(platform string) gin.HandlerFunc {
return func(c *gin.Context) {
// 设置到 request.Context使用字符串 key 供 Service 层读取
ctx := context.WithValue(c.Request.Context(), ctxKeyForcePlatformStr, platform)
// 设置到 request.Context使用 ctxkey.ForcePlatform 供 Service 层读取
ctx := context.WithValue(c.Request.Context(), ctxkey.ForcePlatform, platform)
c.Request = c.Request.WithContext(ctx)
// 同时设置到 gin.Context供 Handler 快速检查
c.Set(string(ContextKeyForcePlatform), platform)