asis & readme

This commit is contained in:
2023-09-28 15:42:22 +08:00
parent 043fb3db85
commit 8342e47e4b
4 changed files with 21 additions and 90 deletions

View File

@@ -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` 表,改动立即生效

View File

@@ -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

View File

@@ -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 {

View File

@@ -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 {