forked from mtusi2026/mtusi2026lab2
Проверка и завершение требований уровней «3» и «4»
This commit is contained in:
@@ -3,6 +3,8 @@ package config
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
@@ -20,6 +22,8 @@ type Postgres struct {
|
||||
VersionTable string
|
||||
RunMigrations bool
|
||||
MaxConnections int
|
||||
ConnectTimeout time.Duration
|
||||
QueryTimeout time.Duration
|
||||
}
|
||||
|
||||
func (c *Postgres) Validate() error {
|
||||
@@ -36,13 +40,21 @@ func (c *Postgres) Validate() error {
|
||||
return wrapValidationError("postgres", errors.New("database cannot be empty"))
|
||||
case c.VersionTable == "":
|
||||
return wrapValidationError("postgres", errors.New("version_table name cannot be empty"))
|
||||
case c.MaxConnections == 0:
|
||||
return wrapValidationError("postgres", errors.New("max_connections cannot be empty"))
|
||||
case c.MaxConnections <= 0 || int64(c.MaxConnections) > math.MaxInt32:
|
||||
return wrapValidationError("postgres", errors.New("max_connections must be between 1 and 2147483647"))
|
||||
case c.ConnectTimeout <= 0:
|
||||
return wrapValidationError("postgres", errors.New("connect_timeout must be positive"))
|
||||
case c.QueryTimeout < time.Millisecond:
|
||||
return wrapValidationError("postgres", errors.New("query_timeout must be at least 1ms"))
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Postgres) GetMaxConnections() int32 { return int32(c.MaxConnections) }
|
||||
func (c *Postgres) GetConnectTimeout() time.Duration { return c.ConnectTimeout }
|
||||
func (c *Postgres) GetQueryTimeout() time.Duration { return c.QueryTimeout }
|
||||
|
||||
func (c *Postgres) GetVersionTableName() string {
|
||||
if c == nil {
|
||||
return ""
|
||||
@@ -74,6 +86,8 @@ func (c *Postgres) loadConfig(v *viper.Viper) error {
|
||||
c.VersionTable = v.GetString("VERSION_TABLE")
|
||||
c.RunMigrations = v.GetBool("RUN_MIGRATIONS")
|
||||
c.MaxConnections = v.GetInt("MAX_CONNECTIONS")
|
||||
c.ConnectTimeout = v.GetDuration("CONNECT_TIMEOUT")
|
||||
c.QueryTimeout = v.GetDuration("QUERY_TIMEOUT")
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -88,6 +102,8 @@ func (c *Postgres) copy() *Postgres {
|
||||
Password: c.Password,
|
||||
Database: c.Database,
|
||||
MaxConnections: c.MaxConnections,
|
||||
ConnectTimeout: c.ConnectTimeout,
|
||||
QueryTimeout: c.QueryTimeout,
|
||||
RunMigrations: c.RunMigrations,
|
||||
VersionTable: c.VersionTable,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user