lab4: S3-вложения, presigned upload и проверки

This commit is contained in:
Vladislav Kunetskiy
2026-10-21 22:42:48 +03:00
parent d342b2a227
commit 49c2fe3071
79 changed files with 12266 additions and 910 deletions
+47
View File
@@ -36,6 +36,27 @@ type Config struct {
Cache Cache `mapstructure:"cache"`
Session Session `mapstructure:"session"`
Idempotency Idempotency `mapstructure:"idempotency"`
S3 S3 `mapstructure:"s3"`
}
// S3 describes the object store and attachment limits. Access keys are checked
// by the object-store constructor rather than the global validator: cmd/migrate
// and unit tests load this config without any S3 credentials.
type S3 struct {
Endpoint string `mapstructure:"endpoint" validate:"required,url"`
PublicEndpoint string `mapstructure:"public_endpoint" validate:"omitempty,url"`
Region string `mapstructure:"region" validate:"required"`
Bucket string `mapstructure:"bucket" validate:"required"`
AccessKeyID string `mapstructure:"access_key_id"`
SecretAccessKey string `mapstructure:"secret_access_key"`
UsePathStyle bool `mapstructure:"use_path_style"`
Timeout time.Duration `mapstructure:"timeout" validate:"required,gt=0"`
MaxUploadBytes int64 `mapstructure:"max_upload_bytes" validate:"required,gt=0,lte=10485760"`
MaxImagePixels int64 `mapstructure:"max_image_pixels" validate:"required,gt=0,lte=40000000"`
PresignTTL time.Duration `mapstructure:"presign_ttl" validate:"required,gt=0"`
SweepInterval time.Duration `mapstructure:"sweep_interval" validate:"required,gt=0"`
SweepGrace time.Duration `mapstructure:"sweep_grace" validate:"required,gt=0"`
}
type Session struct {
@@ -72,6 +93,7 @@ type Server struct {
Host string `mapstructure:"host" validate:"required,hostname|ip"`
Port int `mapstructure:"port" validate:"required,min=1,max=65535"`
ReadHeaderTimeout time.Duration `mapstructure:"read_header_timeout" validate:"required,gt=0"`
ReadTimeout time.Duration `mapstructure:"read_timeout" validate:"required,gt=0"`
ShutdownTimeout time.Duration `mapstructure:"shutdown_timeout" validate:"required,gt=0"`
// MaxBodyBytes bounds a request body before the transport decodes it; uploads
// of a later lab raise it explicitly (contract 4: 10 MiB attachments).
@@ -123,8 +145,22 @@ var defaults = map[string]any{
"server.host": "0.0.0.0",
"server.port": 8080,
"server.read_header_timeout": "10s",
"server.read_timeout": "60s",
"server.shutdown_timeout": "10s",
"server.max_body_bytes": 262144,
"s3.endpoint": "http://localhost:8333",
"s3.public_endpoint": "http://localhost:8333",
"s3.region": "us-east-1",
"s3.bucket": "messenger-attachments",
"s3.access_key_id": "",
"s3.secret_access_key": "",
"s3.use_path_style": true,
"s3.timeout": "30s",
"s3.max_upload_bytes": 10485760,
"s3.max_image_pixels": 40000000,
"s3.presign_ttl": "5m",
"s3.sweep_interval": "1m",
"s3.sweep_grace": "10m",
"log.level": "info",
"log.format": "json",
"session.ttl_seconds": 3600,
@@ -161,6 +197,17 @@ func Load() (Config, error) {
}
}
// The skeleton suggests MAX_UPLOAD_BYTES / MAX_IMAGE_PIXELS. Keep those
// names while storing the values under the S3 section.
for key, env := range map[string]string{
"s3.max_upload_bytes": "MAX_UPLOAD_BYTES",
"s3.max_image_pixels": "MAX_IMAGE_PIXELS",
} {
if err := v.BindEnv(key, env); err != nil {
return Config{}, fmt.Errorf("bind env %q: %w", env, err)
}
}
var cfg Config
if err := v.Unmarshal(&cfg); err != nil {
return Config{}, fmt.Errorf("unmarshal config: %w", err)