From 8342e47e4b26240fd353f9f899ed0ec4ced78fb9 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Thu, 28 Sep 2023 15:42:22 +0800 Subject: [PATCH] asis & readme --- README.md | 82 ++----------------------------------------------------- auth.go | 7 ++++- main.go | 5 ++-- record.go | 17 ++++++------ 4 files changed, 21 insertions(+), 90 deletions(-) diff --git a/README.md b/README.md index fc511da..6cd9fac 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ 这将会编译代码并生成可执行文件。 -5. 编译成功后,您可以直接运行以下命令来启动负载均衡和能力 API: +5. 编译成功后,您可以直接运行以下命令来启动负载均衡 API: ``` ./openai-api-route @@ -85,6 +85,7 @@ Usage of ./openai-api-route: 您也可以使用 `/admin/upstreams` 的 HTTP 接口进行控制。 另外,您还可以直接编辑数据库中的 `openai_upstreams` 表。 + ## 身份验证 ### 身份验证中间件流程 @@ -99,81 +100,4 @@ Usage of ./openai-api-route: ## 上游管理 -### 获取所有上游 - -- URL: `/admin/upstreams` -- 方法: GET -- 权限要求: 需要进行身份验证 -- 返回数据类型: JSON -- 请求示例: - ```bash - curl -X GET -H "Authorization: Bearer access_token" http://localhost:8080/admin/upstreams - ``` -- 返回示例: - ```json - [ - { - "ID": 1, - "SK": "sk_value", - "Endpoint": "endpoint_value" - }, - { - "ID": 2, - "SK": "sk_value", - "Endpoint": "endpoint_value" - } - ] - ``` - -### 创建新的上游 - -- URL: `/admin/upstreams` -- 方法: POST -- 权限要求: 需要进行身份验证 -- 请求数据类型: JSON -- 请求示例: - ```bash - curl -X POST -H "Authorization: Bearer access_token" -H "Content-Type: application/json" -d '{"SK": "sk_value", "Endpoint": "endpoint_value"}' http://localhost:8080/admin/upstreams - ``` -- 返回数据类型: JSON -- 返回示例: - ```json - { - "message": "success" - } - ``` - -### 删除指定 ID 的上游 - -- URL: `/admin/upstreams/:id` -- 方法: DELETE -- 权限要求: 需要进行身份验证 -- 返回数据类型: JSON -- 请求示例: - ```bash - curl -X DELETE -H "Authorization: Bearer access_token" http://localhost:8080/admin/upstreams/1 - ``` -- 返回示例: - ```json - { - "message": "success" - } - ``` - -### 更新指定 ID 的上游 - -- URL: `/admin/upstreams/:id` -- 方法: PUT -- 权限要求: 需要进行身份验证 -- 请求数据类型: JSON -- 请求示例: - ```bash - curl -X PUT -H "Authorization: Bearer access_token" -H "Content-Type: application/json" -d '{"SK": "sk_value", "Endpoint": "endpoint_value"}' http://localhost:8080/admin/upstreams/1 - ``` -- 返回数据类型: JSON -- 返回示例: - ```json - { - "message": "success" - } - ``` +没什么好说的,直接操作数据库 `openai_upstreams` 表,改动立即生效 diff --git a/auth.go b/auth.go index d2d5761..3815e21 100644 --- a/auth.go +++ b/auth.go @@ -21,7 +21,12 @@ func handleAuth(c *gin.Context) error { authorization = strings.Trim(authorization[len("Bearer"):], " ") log.Println("Received authorization", authorization) - if authConfig.Value != "asis" && authorization != authConfig.Value { + if authConfig.Value == "asis" { + log.Println("Authorization is asis, skipping") + return nil + } + + if authorization != authConfig.Value { err = errors.New("wrong authorization header") c.AbortWithError(403, err) return err diff --git a/main.go b/main.go index 531cd86..a76d20f 100644 --- a/main.go +++ b/main.go @@ -103,8 +103,9 @@ func main() { engine.POST("/v1/*any", func(c *gin.Context) { record := Record{ - IP: c.ClientIP(), - CreatedAt: time.Now(), + IP: c.ClientIP(), + CreatedAt: time.Now(), + Authorization: c.Request.Header.Get("Authorization"), } defer func() { if err := recover(); err != nil { diff --git a/record.go b/record.go index 5ff89ca..801c055 100644 --- a/record.go +++ b/record.go @@ -11,14 +11,15 @@ import ( ) type Record struct { - ID int64 `gorm:"primaryKey,autoIncrement"` - CreatedAt time.Time - IP string - Body string `gorm:"serializer:json"` - Response string - ElapsedTime time.Duration - Status int - UpstreamID uint + ID int64 `gorm:"primaryKey,autoIncrement"` + CreatedAt time.Time + IP string + Body string `gorm:"serializer:json"` + Response string + ElapsedTime time.Duration + Status int + UpstreamID uint + Authorization string } type StreamModeChunk struct {