Проверка и завершение требований уровней «3» и «4»

This commit is contained in:
Grbisba
2026-11-06 13:58:50 +03:00
parent 711754bbcd
commit 08ee3fe0b8
30 changed files with 1752 additions and 92 deletions
+3 -1
View File
@@ -29,6 +29,8 @@ var defaultConfig = &Config{
VersionTable: "versions",
RunMigrations: false,
MaxConnections: 10,
ConnectTimeout: 500 * time.Millisecond,
QueryTimeout: 2 * time.Second,
},
Redis: &Redis{
Host: "localhost",
@@ -39,7 +41,7 @@ var defaultConfig = &Config{
},
JWT: &JWT{
AccessTokenLifetime: 4 * time.Hour,
RefreshTokenLifetime: 24 * time.Hour,
RefreshTokenLifetime: time.Hour,
PublicKeyPath: "certs/private.pem",
PrivateKeyPath: "certs/public.pem",
},
+18 -3
View File
@@ -2,6 +2,8 @@ package config
import (
"errors"
"os"
"strconv"
"time"
"github.com/spf13/viper"
@@ -16,9 +18,9 @@ type JWT struct {
func (c *JWT) Validate() error {
switch {
case c.AccessTokenLifetime == 0:
case c.AccessTokenLifetime <= 0:
return wrapValidationError("jwt", errors.New("access_token_lifetime cannot be empty"))
case c.RefreshTokenLifetime == 0:
case c.RefreshTokenLifetime <= 0:
return wrapValidationError("jwt", errors.New("refresh_token_lifetime cannot be empty"))
case c.PrivateKeyPath == "":
return wrapValidationError("jwt", errors.New("private_key_path cannot be empty"))
@@ -32,7 +34,20 @@ func (c *JWT) Validate() error {
func (c *JWT) loadConfig(v *viper.Viper) error {
v.SetEnvPrefix("JWT")
c.AccessTokenLifetime = v.GetDuration("ACCESS_TOKEN_LIFETIME")
c.RefreshTokenLifetime = v.GetDuration("REFRESH_TOKEN_LIFETIME")
c.RefreshTokenLifetime = time.Hour
if value := os.Getenv("SESSION_TTL_SECONDS"); value != "" {
seconds, err := strconv.ParseInt(value, 10, 64)
if err != nil || seconds <= 0 || seconds > int64((1<<63-1)/time.Second) {
return wrapValidationError("session", errors.New("SESSION_TTL_SECONDS must be a positive integer duration in seconds"))
}
c.RefreshTokenLifetime = time.Duration(seconds) * time.Second
} else if legacy := v.GetString("REFRESH_TOKEN_LIFETIME"); legacy != "" {
duration, err := time.ParseDuration(legacy)
if err != nil {
return wrapValidationError("jwt", errors.New("invalid refresh_token_lifetime"))
}
c.RefreshTokenLifetime = duration
}
c.PublicKeyPath = v.GetString("PUBLIC_KEY_PATH")
c.PrivateKeyPath = v.GetString("PRIVATE_KEY_PATH")
return nil
+18 -2
View File
@@ -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,
}