From 346a4a53c96744b1bc969d760d27ac1c8932a1b5 Mon Sep 17 00:00:00 2001 From: igophper <34326532+igophper@users.noreply.github.com> Date: Sun, 30 Jun 2024 16:13:43 +0800 Subject: [PATCH] refactor: replace hardcoded string with ctxkey constant (#1579) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: 江杭辉 --- common/ctxkey/key.go | 1 + common/gin.go | 7 +++---- controller/relay.go | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/common/ctxkey/key.go b/common/ctxkey/key.go index 6c6408706f..90556b3af6 100644 --- a/common/ctxkey/key.go +++ b/common/ctxkey/key.go @@ -19,4 +19,5 @@ const ( TokenName = "token_name" BaseURL = "base_url" AvailableModels = "available_models" + KeyRequestBody = "key_request_body" ) diff --git a/common/gin.go b/common/gin.go index b6ef96a6e9..549d3279c9 100644 --- a/common/gin.go +++ b/common/gin.go @@ -4,14 +4,13 @@ import ( "bytes" "encoding/json" "github.com/gin-gonic/gin" + "github.com/songquanpeng/one-api/common/ctxkey" "io" "strings" ) -const KeyRequestBody = "key_request_body" - func GetRequestBody(c *gin.Context) ([]byte, error) { - requestBody, _ := c.Get(KeyRequestBody) + requestBody, _ := c.Get(ctxkey.KeyRequestBody) if requestBody != nil { return requestBody.([]byte), nil } @@ -20,7 +19,7 @@ func GetRequestBody(c *gin.Context) ([]byte, error) { return nil, err } _ = c.Request.Body.Close() - c.Set(KeyRequestBody, requestBody) + c.Set(ctxkey.KeyRequestBody, requestBody) return requestBody.([]byte), nil } diff --git a/controller/relay.go b/controller/relay.go index 5d8ac69039..932e023b41 100644 --- a/controller/relay.go +++ b/controller/relay.go @@ -48,7 +48,7 @@ func Relay(c *gin.Context) { logger.Debugf(ctx, "request body: %s", string(requestBody)) } channelId := c.GetInt(ctxkey.ChannelId) - userId := c.GetInt("id") + userId := c.GetInt(ctxkey.Id) bizErr := relayHelper(c, relayMode) if bizErr == nil { monitor.Emit(channelId, true)