diff --git a/common/constants.go b/common/constants.go index 9ee791df..654c1d91 100644 --- a/common/constants.go +++ b/common/constants.go @@ -91,12 +91,12 @@ var IsMasterNode = os.Getenv("NODE_TYPE") != "slave" var requestInterval, _ = strconv.Atoi(os.Getenv("POLLING_INTERVAL")) var RequestInterval = time.Duration(requestInterval) * time.Second -var SyncFrequency = GetOrDefault("SYNC_FREQUENCY", 10*60) // unit is second +var SyncFrequency = GetOrDefaultInt("SYNC_FREQUENCY", 10*60) // unit is second var BatchUpdateEnabled = false -var BatchUpdateInterval = GetOrDefault("BATCH_UPDATE_INTERVAL", 5) +var BatchUpdateInterval = GetOrDefaultInt("BATCH_UPDATE_INTERVAL", 5) -var RelayTimeout = GetOrDefault("RELAY_TIMEOUT", 0) // unit is second +var RelayTimeout = GetOrDefaultInt("RELAY_TIMEOUT", 0) // unit is second var GeminiSafetySetting = GetOrDefaultString("GEMINI_SAFETY_SETTING", "BLOCK_NONE") @@ -127,10 +127,10 @@ var ( // All duration's unit is seconds // Shouldn't larger then RateLimitKeyExpirationDuration var ( - GlobalApiRateLimitNum = GetOrDefault("GLOBAL_API_RATE_LIMIT", 180) + GlobalApiRateLimitNum = GetOrDefaultInt("GLOBAL_API_RATE_LIMIT", 180) GlobalApiRateLimitDuration int64 = 3 * 60 - GlobalWebRateLimitNum = GetOrDefault("GLOBAL_WEB_RATE_LIMIT", 60) + GlobalWebRateLimitNum = GetOrDefaultInt("GLOBAL_WEB_RATE_LIMIT", 60) GlobalWebRateLimitDuration int64 = 3 * 60 UploadRateLimitNum = 10 diff --git a/common/crypto.go b/common/crypto.go index 45228416..6b5efbd2 100644 --- a/common/crypto.go +++ b/common/crypto.go @@ -8,7 +8,7 @@ func Password2Hash(password string) (string, error) { return string(hashedPassword), err } -func ValidatePasswordAndHash(password string, hash string) bool { +func ValidatePasswordAndHash(password, hash string) bool { err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password)) return err == nil } diff --git a/common/database.go b/common/database.go index 76f2cd55..71b26208 100644 --- a/common/database.go +++ b/common/database.go @@ -4,4 +4,4 @@ var UsingSQLite = false var UsingPostgreSQL = false var SQLitePath = "one-api.db" -var SQLiteBusyTimeout = GetOrDefault("SQLITE_BUSY_TIMEOUT", 3000) +var SQLiteBusyTimeout = GetOrDefaultInt("SQLITE_BUSY_TIMEOUT", 3000) diff --git a/common/email.go b/common/email.go index b915f0f9..e7f44167 100644 --- a/common/email.go +++ b/common/email.go @@ -10,7 +10,7 @@ import ( "time" ) -func SendEmail(subject string, receiver string, content string) error { +func SendEmail(subject, receiver, content string) error { if SMTPFrom == "" { // for compatibility SMTPFrom = SMTPAccount } diff --git a/common/embed-file-system.go b/common/embed-file-system.go index 3ea02cf8..9fb88a80 100644 --- a/common/embed-file-system.go +++ b/common/embed-file-system.go @@ -13,7 +13,7 @@ type embedFileSystem struct { http.FileSystem } -func (e embedFileSystem) Exists(prefix string, path string) bool { +func (e embedFileSystem) Exists(prefix, path string) bool { _, err := e.Open(path) if err != nil { return false diff --git a/common/utils.go b/common/utils.go index 9a7038e2..744fb9e4 100644 --- a/common/utils.go +++ b/common/utils.go @@ -121,7 +121,7 @@ func UnescapeHTML(x string) interface{} { return template.HTML(x) } -func IntMax(a int, b int) int { +func IntMax(a, b int) int { if a >= b { return a } else { @@ -176,7 +176,7 @@ func GetTimeString() string { return fmt.Sprintf("%s%d", now.Format("20060102150405"), now.UnixNano()%1e9) } -func Max(a int, b int) int { +func Max(a, b int) int { if a >= b { return a } else { @@ -184,7 +184,7 @@ func Max(a int, b int) int { } } -func GetOrDefault(env string, defaultValue int) int { +func GetOrDefaultInt(env string, defaultValue int) int { if env == "" || os.Getenv(env) == "" { return defaultValue } @@ -196,14 +196,14 @@ func GetOrDefault(env string, defaultValue int) int { return num } -func GetOrDefaultString(env string, defaultValue string) string { +func GetOrDefaultString(env, defaultValue string) string { if env == "" || os.Getenv(env) == "" { return defaultValue } return os.Getenv(env) } -func MessageWithRequestId(message string, id string) string { +func MessageWithRequestId(message, id string) string { return fmt.Sprintf("%s (request id: %s)", message, id) } diff --git a/common/verification.go b/common/verification.go index d8ccd6ea..5c8050c0 100644 --- a/common/verification.go +++ b/common/verification.go @@ -31,7 +31,7 @@ func GenerateVerificationCode(length int) string { return code[:length] } -func RegisterVerificationCodeWithKey(key string, code string, purpose string) { +func RegisterVerificationCodeWithKey(key, code, purpose string) { verificationMutex.Lock() defer verificationMutex.Unlock() verificationMap[purpose+key] = verificationValue{ @@ -43,7 +43,7 @@ func RegisterVerificationCodeWithKey(key string, code string, purpose string) { } } -func VerifyCodeWithKey(key string, code string, purpose string) bool { +func VerifyCodeWithKey(key, code, purpose string) bool { verificationMutex.Lock() defer verificationMutex.Unlock() value, okay := verificationMap[purpose+key] @@ -54,7 +54,7 @@ func VerifyCodeWithKey(key string, code string, purpose string) bool { return code == value.code } -func DeleteKey(key string, purpose string) { +func DeleteKey(key, purpose string) { verificationMutex.Lock() defer verificationMutex.Unlock() delete(verificationMap, purpose+key) diff --git a/model/main.go b/model/main.go index bfd6888b..fea1b786 100644 --- a/model/main.go +++ b/model/main.go @@ -77,9 +77,9 @@ func InitDB() (err error) { if err != nil { return err } - sqlDB.SetMaxIdleConns(common.GetOrDefault("SQL_MAX_IDLE_CONNS", 100)) - sqlDB.SetMaxOpenConns(common.GetOrDefault("SQL_MAX_OPEN_CONNS", 1000)) - sqlDB.SetConnMaxLifetime(time.Second * time.Duration(common.GetOrDefault("SQL_MAX_LIFETIME", 60))) + sqlDB.SetMaxIdleConns(common.GetOrDefaultInt("SQL_MAX_IDLE_CONNS", 100)) + sqlDB.SetMaxOpenConns(common.GetOrDefaultInt("SQL_MAX_OPEN_CONNS", 1000)) + sqlDB.SetConnMaxLifetime(time.Second * time.Duration(common.GetOrDefaultInt("SQL_MAX_LIFETIME", 60))) if !common.IsMasterNode { return nil