forked from mtusi2026/mtusi2026lab6
Реализация на 5
This commit is contained in:
@@ -0,0 +1,110 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
const (
|
||||
uri = "postgres://%s:%s@%s:%d/%s?sslmode=disable"
|
||||
)
|
||||
|
||||
type Postgres struct {
|
||||
Host string
|
||||
Port int
|
||||
User string
|
||||
Password string
|
||||
Database string
|
||||
VersionTable string
|
||||
RunMigrations bool
|
||||
MaxConnections int
|
||||
ConnectTimeout time.Duration
|
||||
QueryTimeout time.Duration
|
||||
}
|
||||
|
||||
func (c *Postgres) Validate() error {
|
||||
switch {
|
||||
case c.Host == "":
|
||||
return wrapValidationError("postgres", errors.New("host cannot be empty"))
|
||||
case c.Port == 0:
|
||||
return wrapValidationError("postgres", errors.New("port cannot be empty"))
|
||||
case c.User == "":
|
||||
return wrapValidationError("postgres", errors.New("user cannot be empty"))
|
||||
case c.Password == "":
|
||||
return wrapValidationError("postgres", errors.New("password cannot be empty"))
|
||||
case c.Database == "":
|
||||
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 || 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 ""
|
||||
}
|
||||
return c.VersionTable
|
||||
}
|
||||
|
||||
func (c *Postgres) GetURI() string {
|
||||
if c == nil {
|
||||
return ""
|
||||
}
|
||||
return fmt.Sprintf(uri, c.User, c.Password, c.Host, c.Port, c.Database)
|
||||
}
|
||||
|
||||
func (c *Postgres) GetRunMigrations() bool {
|
||||
if c == nil {
|
||||
return false
|
||||
}
|
||||
return c.RunMigrations
|
||||
}
|
||||
|
||||
func (c *Postgres) loadConfig(v *viper.Viper) error {
|
||||
v.SetEnvPrefix("POSTGRES")
|
||||
c.Host = v.GetString("HOST")
|
||||
c.Port = v.GetInt("PORT")
|
||||
c.User = v.GetString("USER")
|
||||
c.Password = v.GetString("PASSWORD")
|
||||
c.Database = v.GetString("DATABASE")
|
||||
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
|
||||
}
|
||||
|
||||
func (c *Postgres) copy() *Postgres {
|
||||
if c == nil {
|
||||
return nil
|
||||
}
|
||||
return &Postgres{
|
||||
Host: c.Host,
|
||||
Port: c.Port,
|
||||
User: c.User,
|
||||
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