feat(config): SESSION_STORE and CACHE_URL

This commit is contained in:
Голиков Maximillian Антон
2026-10-02 13:08:24 +03:00
parent 42859d90a3
commit 5a3f6114a4
3 changed files with 45 additions and 12 deletions
+3
View File
@@ -15,6 +15,7 @@ require (
github.com/bytedance/gopkg v0.1.3 // indirect
github.com/bytedance/sonic v1.15.0 // indirect
github.com/bytedance/sonic/loader v0.5.0 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/cloudwego/base64x v0.1.6 // indirect
github.com/gabriel-vasile/mimetype v1.4.12 // indirect
github.com/gin-contrib/sse v1.1.0 // indirect
@@ -36,10 +37,12 @@ require (
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
github.com/quic-go/qpack v0.6.0 // indirect
github.com/quic-go/quic-go v0.59.0 // indirect
github.com/redis/go-redis/v9 v9.22.0 // indirect
github.com/sethvargo/go-retry v0.3.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.3.1 // indirect
go.mongodb.org/mongo-driver/v2 v2.5.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/arch v0.22.0 // indirect
golang.org/x/net v0.58.0 // indirect
+6
View File
@@ -4,6 +4,8 @@ github.com/bytedance/sonic v1.15.0 h1:/PXeWFaR5ElNcVE84U0dOHjiMHQOwNIx3K4ymzh/uS
github.com/bytedance/sonic v1.15.0/go.mod h1:tFkWrPz0/CUCLEF4ri4UkHekCIcdnkqXw9VduqpJh0k=
github.com/bytedance/sonic/loader v0.5.0 h1:gXH3KVnatgY7loH5/TkeVyXPfESoqSBSBEiDd5VjlgE=
github.com/bytedance/sonic/loader v0.5.0/go.mod h1:AR4NYCk5DdzZizZ5djGqQ92eEhCCcdf5x77udYiSJRo=
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/cloudwego/base64x v0.1.6 h1:t11wG9AECkCDk5fMSoxmufanudBtJ+/HemLstXDLI2M=
github.com/cloudwego/base64x v0.1.6/go.mod h1:OFcloc187FXDaYHvrNIjxSe8ncn0OOM8gEHfghB2IPU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -73,6 +75,8 @@ github.com/quic-go/qpack v0.6.0 h1:g7W+BMYynC1LbYLSqRt8PBg5Tgwxn214ZZR34VIOjz8=
github.com/quic-go/qpack v0.6.0/go.mod h1:lUpLKChi8njB4ty2bFLX2x4gzDqXwUpaO1DP9qMDZII=
github.com/quic-go/quic-go v0.59.0 h1:OLJkp1Mlm/aS7dpKgTc6cnpynnD2Xg7C1pwL6vy/SAw=
github.com/quic-go/quic-go v0.59.0/go.mod h1:upnsH4Ju1YkqpLXC305eW3yDZ4NfnNbmQRCMWS58IKU=
github.com/redis/go-redis/v9 v9.22.0 h1:laDvpYXTJtZLloinw1fA5Kqd6HAEH2XKxOkG/PDq2F0=
github.com/redis/go-redis/v9 v9.22.0/go.mod h1:y2g0Wj8rQvuK0ELM+oxSudcLtC09JScs98I/X9gRWY4=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
@@ -95,6 +99,8 @@ github.com/ugorji/go/codec v1.3.1 h1:waO7eEiFDwidsBN6agj1vJQ4AG7lh2yqXyOXqhgQuyY
github.com/ugorji/go/codec v1.3.1/go.mod h1:pRBVtBSKl77K30Bv8R2P+cLSGaTtex6fsA2Wjqmfxj4=
go.mongodb.org/mongo-driver/v2 v2.5.0 h1:yXUhImUjjAInNcpTcAlPHiT7bIXhshCTL3jVBkF3xaE=
go.mongodb.org/mongo-driver/v2 v2.5.0/go.mod h1:yOI9kBsufol30iFsl1slpdq1I0eHPzybRWdyYUs8K/0=
go.uber.org/atomic v1.11.0 h1:ZvwS0R+56ePWxUNi+Atn9dWONBPp/AUETXlHW0DxSjE=
go.uber.org/atomic v1.11.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y=
go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
+36 -12
View File
@@ -9,10 +9,12 @@ import (
)
type Config struct {
Host string
Port string
DatabaseURL string
SessionTTL time.Duration
Host string
Port string
DatabaseURL string
SessionStore string
CacheURL string
SessionTTL time.Duration
}
func Load() (*Config, error) {
@@ -21,6 +23,11 @@ func Load() (*Config, error) {
return nil, fmt.Errorf("database url: %w", err)
}
cacheURL, err := readSecretOptional("CACHE_URL_FILE", "CACHE_URL")
if err != nil {
return nil, fmt.Errorf("cache url: %w", err)
}
ttlSeconds, err := readInt("SESSION_TTL_SECONDS", 3600)
if err != nil {
return nil, fmt.Errorf("SESSION_TTL_SECONDS: %w", err)
@@ -29,11 +36,20 @@ func Load() (*Config, error) {
return nil, fmt.Errorf("SESSION_TTL_SECONDS должен быть положительным")
}
store := strings.ToLower(getenv("SESSION_STORE", "postgres"))
switch store {
case "postgres", "redis", "valkey":
default:
return nil, fmt.Errorf("SESSION_STORE=%q не поддерживается", store)
}
return &Config{
Host: getenv("APP_HOST", "0.0.0.0"),
Port: getenv("APP_PORT", "8080"),
DatabaseURL: dsn,
SessionTTL: time.Duration(ttlSeconds) * time.Second,
Host: getenv("APP_HOST", "0.0.0.0"),
Port: getenv("APP_PORT", "8080"),
DatabaseURL: dsn,
SessionStore: store,
CacheURL: cacheURL,
SessionTTL: time.Duration(ttlSeconds) * time.Second,
}, nil
}
@@ -57,6 +73,17 @@ func readInt(key string, def int) (int, error) {
}
func readSecret(fileEnv, envKey string) (string, error) {
v, err := readSecretOptional(fileEnv, envKey)
if err != nil {
return "", err
}
if v == "" {
return "", fmt.Errorf("не задан %s или %s", fileEnv, envKey)
}
return v, nil
}
func readSecretOptional(fileEnv, envKey string) (string, error) {
if path := os.Getenv(fileEnv); path != "" {
b, err := os.ReadFile(path)
if err != nil {
@@ -68,8 +95,5 @@ func readSecret(fileEnv, envKey string) (string, error) {
}
return s, nil
}
if v := os.Getenv(envKey); v != "" {
return v, nil
}
return "", fmt.Errorf("не задан %s или %s", fileEnv, envKey)
return os.Getenv(envKey), nil
}