forked from mtusi2026/mtusi2026lab7
127 lines
3.5 KiB
Go
127 lines
3.5 KiB
Go
package config
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"gitea.privcloud.tech/mtusi2026/internal/pkg/transportsecurity"
|
|
"math"
|
|
"net/url"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/spf13/viper"
|
|
)
|
|
|
|
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 ""
|
|
}
|
|
u := &url.URL{Scheme: "postgres", User: url.UserPassword(c.User, c.Password), Host: fmt.Sprintf("%s:%d", c.Host, c.Port), Path: "/" + c.Database}
|
|
q := url.Values{}
|
|
if transportsecurity.Enabled() {
|
|
q.Set("sslmode", "verify-full")
|
|
q.Set("sslrootcert", transportsecurity.Directory()+"/ca.crt")
|
|
} else {
|
|
q.Set("sslmode", "disable")
|
|
}
|
|
u.RawQuery = q.Encode()
|
|
return u.String()
|
|
}
|
|
|
|
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")
|
|
if path := v.GetString("PASSWORD_FILE"); path != "" {
|
|
b, e := os.ReadFile(path)
|
|
if e != nil {
|
|
return fmt.Errorf("read database credential: %w", e)
|
|
}
|
|
c.Password = strings.TrimSpace(string(b))
|
|
}
|
|
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,
|
|
}
|
|
}
|