From 7b74818676acd7afcad4cced604640d339ab3691 Mon Sep 17 00:00:00 2001 From: heimoshuiyu Date: Wed, 29 Nov 2023 14:31:11 +0800 Subject: [PATCH] update config to file --- README.md | 17 +++++++++++++---- main.go | 13 +++++-------- structure.go | 14 ++++++++++++++ 3 files changed, 32 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index e4b3dc7..f1ea269 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,15 @@ Usage of ./openai-api-route: ```yaml 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: - sk: "secret_key_1" 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),服务将使用该值作为超时时间。这个值是以秒为单位的。 diff --git a/main.go b/main.go index cccac6a..725f2d8 100644 --- a/main.go +++ b/main.go @@ -18,10 +18,7 @@ import ( var config Config 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") - listenAddr := flag.String("addr", ":8888", "Listening address") listMode := flag.Bool("list", false, "List all upstream") noauth := flag.Bool("noauth", false, "Do not check incoming authorization header") flag.Parse() @@ -31,19 +28,19 @@ func main() { // connect to database var db *gorm.DB var err error - switch *dbType { + switch config.DBType { case "sqlite": - db, err = gorm.Open(sqlite.Open(*dbAddr), &gorm.Config{ + db, err = gorm.Open(sqlite.Open(config.DBAddr), &gorm.Config{ PrepareStmt: true, SkipDefaultTransaction: true, }) case "postgres": - db, err = gorm.Open(postgres.Open(*dbAddr), &gorm.Config{ + db, err = gorm.Open(postgres.Open(config.DBAddr), &gorm.Config{ PrepareStmt: true, SkipDefaultTransaction: true, }) default: - log.Fatalf("Unsupported database type: %s", *dbType) + log.Fatalf("Unsupported database type: %s", config.DBType) } // load all upstreams @@ -144,5 +141,5 @@ func main() { } }) - engine.Run(*listenAddr) + engine.Run(config.Address) } diff --git a/structure.go b/structure.go index d2ea040..9b4dbf1 100644 --- a/structure.go +++ b/structure.go @@ -8,6 +8,9 @@ import ( ) type Config struct { + Address string `yaml:"address"` + DBType string `yaml:"dbtype"` + DBAddr string `yaml:"dbaddr"` Authorization string `yaml:"authorization"` Upstreams []OPENAI_UPSTREAM `yaml:"upstreams"` } @@ -32,5 +35,16 @@ func readConfig(filepath string) Config { 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 }