forked from mtusi2026/mtusi2026lab5
ЛР 5 — асинхронная обработка и версии pipeline
This commit is contained in:
@@ -0,0 +1,180 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Database struct {
|
||||
PostgresURL string
|
||||
RedisAddr string
|
||||
}
|
||||
|
||||
type S3 struct {
|
||||
Endpoint string
|
||||
Region string
|
||||
Bucket string
|
||||
AccessKeyID string
|
||||
SecretAccessKey string
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
JobMaxAttempts int
|
||||
HTTP struct {
|
||||
Host string
|
||||
Port string
|
||||
}
|
||||
Database Database
|
||||
|
||||
Session struct {
|
||||
TTL time.Duration
|
||||
Store string
|
||||
}
|
||||
|
||||
S3 S3
|
||||
|
||||
AppEnv string
|
||||
CacheURL string
|
||||
}
|
||||
|
||||
func Load() (*Config, error) {
|
||||
config := &Config{}
|
||||
sessionTTL, err := getSessionTTL()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
config.Session.TTL = sessionTTL
|
||||
config.JobMaxAttempts, err = EnvInt("JOB_MAX_ATTEMPTS", 5, 1, 20)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config.HTTP.Host = os.Getenv("APP_HOST")
|
||||
if config.HTTP.Host == "" {
|
||||
config.HTTP.Host = "0.0.0.0"
|
||||
}
|
||||
|
||||
config.HTTP.Port = os.Getenv("APP_PORT")
|
||||
if config.HTTP.Port == "" {
|
||||
config.HTTP.Port = "8080"
|
||||
}
|
||||
|
||||
config.AppEnv = os.Getenv("APP_ENV")
|
||||
if config.AppEnv == "" {
|
||||
config.AppEnv = "lab"
|
||||
}
|
||||
|
||||
store := os.Getenv("SESSION_STORE")
|
||||
|
||||
config.Session.Store = strings.TrimSpace(string(store))
|
||||
if config.Session.Store == "" {
|
||||
return nil, fmt.Errorf("empty session store data")
|
||||
}
|
||||
|
||||
config.Database, err = getDatabaseURL()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
config.S3, err = getS3Data()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return config, nil
|
||||
|
||||
}
|
||||
|
||||
func getSessionTTL() (time.Duration, error) {
|
||||
value, ok := os.LookupEnv("SESSION_TTL_SECONDS")
|
||||
if !ok {
|
||||
return time.Hour, nil
|
||||
}
|
||||
|
||||
seconds, err := strconv.ParseInt(value, 10, 64)
|
||||
if err != nil || seconds <= 0 || seconds > int64((1<<63-1)/int64(time.Second)) {
|
||||
return 0, fmt.Errorf("invalid SESSION_TTL_SECONDS: %q", value)
|
||||
}
|
||||
|
||||
return time.Duration(seconds) * time.Second, nil
|
||||
}
|
||||
|
||||
func LoadAttachmentCleanup() (postgresURL string, objectStorage S3, err error) {
|
||||
postgresURL, err = LoadPostgresURL()
|
||||
if err != nil {
|
||||
return "", S3{}, err
|
||||
}
|
||||
objectStorage, err = getS3Data()
|
||||
return postgresURL, objectStorage, err
|
||||
}
|
||||
|
||||
func LoadPostgresURL() (string, error) {
|
||||
postgresData, err := os.ReadFile(os.Getenv("DATABASE_URL_FILE"))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read database URL: %w", err)
|
||||
}
|
||||
postgresURL := strings.TrimSpace(string(postgresData))
|
||||
if postgresURL == "" {
|
||||
return "", fmt.Errorf("empty database URL")
|
||||
}
|
||||
return postgresURL, nil
|
||||
}
|
||||
|
||||
func getDatabaseURL() (Database, error) {
|
||||
postgresURL, err := LoadPostgresURL()
|
||||
if err != nil {
|
||||
return Database{}, err
|
||||
}
|
||||
db := Database{PostgresURL: postgresURL}
|
||||
|
||||
redisData := os.Getenv("REDIS_ADDR")
|
||||
|
||||
db.RedisAddr = strings.TrimSpace(string(redisData))
|
||||
|
||||
if db.RedisAddr == "" {
|
||||
return db, fmt.Errorf("redis addres is empty")
|
||||
}
|
||||
|
||||
return db, nil
|
||||
|
||||
}
|
||||
|
||||
func getS3Data() (S3, error) {
|
||||
s3 := S3{
|
||||
Endpoint: os.Getenv("S3_ENDPOINT"),
|
||||
Region: os.Getenv("S3_REGION"),
|
||||
Bucket: os.Getenv("S3_BUCKET"),
|
||||
}
|
||||
|
||||
if s3.Endpoint == "" {
|
||||
return s3, fmt.Errorf("empty s3 endpoint")
|
||||
}
|
||||
if s3.Region == "" {
|
||||
return s3, fmt.Errorf("empty s3 region")
|
||||
}
|
||||
if s3.Bucket == "" {
|
||||
return s3, fmt.Errorf("emtpy s3 bucket")
|
||||
}
|
||||
accessKeyIDPath := os.Getenv("S3_ACCESS_KEY_ID_FILE")
|
||||
|
||||
accessKeyID, err := os.ReadFile(accessKeyIDPath)
|
||||
if err != nil {
|
||||
return s3, fmt.Errorf("empty s3 access_key_id")
|
||||
}
|
||||
s3.AccessKeyID = strings.TrimSpace(string(accessKeyID))
|
||||
|
||||
secretAccessKeyPath := os.Getenv("S3_SECRET_ACCESS_KEY_FILE")
|
||||
secretAccessKey, err := os.ReadFile(secretAccessKeyPath)
|
||||
if err != nil {
|
||||
return s3, fmt.Errorf("empty s3 secret_access_key_file")
|
||||
}
|
||||
s3.SecretAccessKey = strings.TrimSpace(string(secretAccessKey))
|
||||
|
||||
return s3, nil
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user