update config to file
This commit is contained in:
17
README.md
17
README.md
@@ -88,6 +88,15 @@ Usage of ./openai-api-route:
|
|||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
authorization: woshimima
|
authorization: woshimima
|
||||||
|
|
||||||
|
# 使用 sqlite 作为数据库储存请求记录
|
||||||
|
dbtype: sqlite
|
||||||
|
dbaddr: ./db.sqlite
|
||||||
|
|
||||||
|
# 使用 postgres 作为数据库储存请求记录
|
||||||
|
# dbtype: postgres
|
||||||
|
# dbaddr: "host=127.0.0.1 port=5432 user=postgres dbname=openai_api_route sslmode=disable password=woshimima"
|
||||||
|
|
||||||
upstreams:
|
upstreams:
|
||||||
- sk: "secret_key_1"
|
- sk: "secret_key_1"
|
||||||
endpoint: "https://api.openai.com/v2"
|
endpoint: "https://api.openai.com/v2"
|
||||||
@@ -116,10 +125,10 @@ upstreams:
|
|||||||
|
|
||||||
服务在处理请求时会根据不同的条件设置不同的超时时间。超时时间是指服务等待上游服务器响应的最大时间。以下是超时时间的设置规则:
|
服务在处理请求时会根据不同的条件设置不同的超时时间。超时时间是指服务等待上游服务器响应的最大时间。以下是超时时间的设置规则:
|
||||||
|
|
||||||
1. **默认超时时间**:如果没有特殊条件,服务将使用默认的超时时间,即60秒。
|
1. **默认超时时间**:如果没有特殊条件,服务将使用默认的超时时间,即 60 秒。
|
||||||
|
|
||||||
2. **流式请求**:如果请求体被识别为流式(`requestBody.Stream` 为 `true`),并且请求体检查(`requestBodyOK`)没有发现问题,超时时间将被设置为5秒。这适用于那些预期会快速响应的流式请求。
|
2. **流式请求**:如果请求体被识别为流式(`requestBody.Stream` 为 `true`),并且请求体检查(`requestBodyOK`)没有发现问题,超时时间将被设置为 5 秒。这适用于那些预期会快速响应的流式请求。
|
||||||
|
|
||||||
3. **大请求体**:如果请求体的大小超过128KB(即 `len(inBody) > 1024*128`),超时时间将被设置为20秒。这考虑到了处理大型数据可能需要更长的时间。
|
3. **大请求体**:如果请求体的大小超过 128KB(即 `len(inBody) > 1024*128`),超时时间将被设置为 20 秒。这考虑到了处理大型数据可能需要更长的时间。
|
||||||
|
|
||||||
4. **上游超时配置**:如果上游服务器在配置中指定了超时时间(`upstream.Timeout` 大于0),服务将使用该值作为超时时间。这个值是以秒为单位的。
|
4. **上游超时配置**:如果上游服务器在配置中指定了超时时间(`upstream.Timeout` 大于 0),服务将使用该值作为超时时间。这个值是以秒为单位的。
|
||||||
|
|||||||
13
main.go
13
main.go
@@ -18,10 +18,7 @@ import (
|
|||||||
var config Config
|
var config Config
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
dbType := flag.String("dbtype", "sqlite", "Database type (sqlite or postgres)")
|
|
||||||
dbAddr := flag.String("database", "./db.sqlite", "Database address, if dbType is postgres, this is the DSN connection string")
|
|
||||||
configFile := flag.String("config", "./config.yaml", "Config file")
|
configFile := flag.String("config", "./config.yaml", "Config file")
|
||||||
listenAddr := flag.String("addr", ":8888", "Listening address")
|
|
||||||
listMode := flag.Bool("list", false, "List all upstream")
|
listMode := flag.Bool("list", false, "List all upstream")
|
||||||
noauth := flag.Bool("noauth", false, "Do not check incoming authorization header")
|
noauth := flag.Bool("noauth", false, "Do not check incoming authorization header")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
@@ -31,19 +28,19 @@ func main() {
|
|||||||
// connect to database
|
// connect to database
|
||||||
var db *gorm.DB
|
var db *gorm.DB
|
||||||
var err error
|
var err error
|
||||||
switch *dbType {
|
switch config.DBType {
|
||||||
case "sqlite":
|
case "sqlite":
|
||||||
db, err = gorm.Open(sqlite.Open(*dbAddr), &gorm.Config{
|
db, err = gorm.Open(sqlite.Open(config.DBAddr), &gorm.Config{
|
||||||
PrepareStmt: true,
|
PrepareStmt: true,
|
||||||
SkipDefaultTransaction: true,
|
SkipDefaultTransaction: true,
|
||||||
})
|
})
|
||||||
case "postgres":
|
case "postgres":
|
||||||
db, err = gorm.Open(postgres.Open(*dbAddr), &gorm.Config{
|
db, err = gorm.Open(postgres.Open(config.DBAddr), &gorm.Config{
|
||||||
PrepareStmt: true,
|
PrepareStmt: true,
|
||||||
SkipDefaultTransaction: true,
|
SkipDefaultTransaction: true,
|
||||||
})
|
})
|
||||||
default:
|
default:
|
||||||
log.Fatalf("Unsupported database type: %s", *dbType)
|
log.Fatalf("Unsupported database type: %s", config.DBType)
|
||||||
}
|
}
|
||||||
|
|
||||||
// load all upstreams
|
// load all upstreams
|
||||||
@@ -144,5 +141,5 @@ func main() {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
engine.Run(*listenAddr)
|
engine.Run(config.Address)
|
||||||
}
|
}
|
||||||
|
|||||||
14
structure.go
14
structure.go
@@ -8,6 +8,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Config struct {
|
type Config struct {
|
||||||
|
Address string `yaml:"address"`
|
||||||
|
DBType string `yaml:"dbtype"`
|
||||||
|
DBAddr string `yaml:"dbaddr"`
|
||||||
Authorization string `yaml:"authorization"`
|
Authorization string `yaml:"authorization"`
|
||||||
Upstreams []OPENAI_UPSTREAM `yaml:"upstreams"`
|
Upstreams []OPENAI_UPSTREAM `yaml:"upstreams"`
|
||||||
}
|
}
|
||||||
@@ -32,5 +35,16 @@ func readConfig(filepath string) Config {
|
|||||||
log.Fatalf("Error unmarshaling YAML: %s", err)
|
log.Fatalf("Error unmarshaling YAML: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// set default value
|
||||||
|
if config.Address == "" {
|
||||||
|
config.Address = ":8888"
|
||||||
|
}
|
||||||
|
if config.DBType == "" {
|
||||||
|
config.DBType = "sqlite"
|
||||||
|
}
|
||||||
|
if config.DBAddr == "" {
|
||||||
|
config.DBAddr = "./db.sqlite"
|
||||||
|
}
|
||||||
|
|
||||||
return config
|
return config
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user