feat(queue-url): ExternalURLBase для публичного QueueUrl (+ env SHARED_SQS_EXTERNAL_URL_BASE), тест

This commit is contained in:
Repinoid
2026-10-10 13:44:22 +03:00
parent ee5503f9ac
commit f0e7cc905d
5 changed files with 50 additions and 0 deletions
+8
View File
@@ -67,6 +67,11 @@ func LoadYamlConfig(filename string, env string) []string {
models.CurrentEnvironment = envs[env] models.CurrentEnvironment = envs[env]
// Публичный адрес очередей (напр. https://sqs.sinefo.ru) — override через env.
if v := os.Getenv("SHARED_SQS_EXTERNAL_URL_BASE"); v != "" {
models.CurrentEnvironment.ExternalURLBase = v
}
if envs[env].Port != "" { if envs[env].Port != "" {
ports = []string{envs[env].Port} ports = []string{envs[env].Port}
} }
@@ -90,6 +95,9 @@ func LoadYamlConfig(filename string, env string) []string {
queueUrl = "http://" + models.CurrentEnvironment.Region + "." + models.CurrentEnvironment.Host + ":" + queueUrl = "http://" + models.CurrentEnvironment.Region + "." + models.CurrentEnvironment.Host + ":" +
models.CurrentEnvironment.Port + "/" + models.CurrentEnvironment.AccountID + "/" + queue.Name models.CurrentEnvironment.Port + "/" + models.CurrentEnvironment.AccountID + "/" + queue.Name
} }
if base := strings.TrimRight(models.CurrentEnvironment.ExternalURLBase, "/"); base != "" {
queueUrl = base + "/" + models.CurrentEnvironment.AccountID + "/" + queue.Name
}
queueArn := "arn:aws:sqs:" + models.CurrentEnvironment.Region + ":" + models.CurrentEnvironment.AccountID + ":" + queue.Name queueArn := "arn:aws:sqs:" + models.CurrentEnvironment.Region + ":" + models.CurrentEnvironment.AccountID + ":" + queue.Name
if queue.ReceiveMessageWaitTimeSeconds == 0 { if queue.ReceiveMessageWaitTimeSeconds == 0 {
+1
View File
@@ -4,6 +4,7 @@ Local: # Environment name that can be passed on the
Port: 4100 # port to listen on. Port: 4100 # port to listen on.
Region: us-east-1 Region: us-east-1
AccountId: "100010001000" AccountId: "100010001000"
ExternalURLBase: "" # публичный адрес очередей (напр. https://sqs.sinefo.ru); пусто → http://{region}.{host}:{port}
LogToFile: false # Log messages (true/false) LogToFile: false # Log messages (true/false)
LogFile: .st/goaws_messages.log # Log filename (for message logging LogFile: .st/goaws_messages.log # Log filename (for message logging
EnableDuplicates: false # Enable or not deduplication based on messageDeduplicationId EnableDuplicates: false # Enable or not deduplication based on messageDeduplicationId
+5
View File
@@ -5,6 +5,7 @@ package gosqs
import ( import (
"net/http" "net/http"
"strings"
"shared-sqs/app/auth" "shared-sqs/app/auth"
"shared-sqs/app/models" "shared-sqs/app/models"
@@ -27,6 +28,10 @@ return t
// tenantQueueURL — формирует URL очереди для тенанта. // tenantQueueURL — формирует URL очереди для тенанта.
// Ловушка #10: QueueUrl ОБЯЗАН содержать tenantID в пути, иначе AWS SDK не сможет send/receive. // Ловушка #10: QueueUrl ОБЯЗАН содержать tenantID в пути, иначе AWS SDK не сможет send/receive.
func tenantQueueURL(t *tenant.Tenant, queueName string) string { func tenantQueueURL(t *tenant.Tenant, queueName string) string {
// Публичный адрес (напр. https://sqs.sinefo.ru) — приоритетнее локального формата.
if base := strings.TrimRight(models.CurrentEnvironment.ExternalURLBase, "/"); base != "" {
return base + "/" + t.ID + "/" + queueName
}
host := models.CurrentEnvironment.Host host := models.CurrentEnvironment.Host
port := models.CurrentEnvironment.Port port := models.CurrentEnvironment.Port
region := models.CurrentEnvironment.Region region := models.CurrentEnvironment.Region
+35
View File
@@ -0,0 +1,35 @@
package gosqs
import (
"testing"
"shared-sqs/app/models"
"shared-sqs/app/tenant"
)
// TestTenantQueueURL — проверяет формирование QueueUrl: публичный ExternalURLBase
// приоритетнее локального формата, пустое значение даёт старый формат.
func TestTenantQueueURL(t *testing.T) {
old := models.CurrentEnvironment
defer func() { models.CurrentEnvironment = old }()
tt := &tenant.Tenant{ID: "t-abc123"}
// Публичный адрес; хвостовой слеш обрезается.
models.CurrentEnvironment = models.Environment{ExternalURLBase: "https://sqs.sinefo.ru/"}
if got, want := tenantQueueURL(tt, "orders"), "https://sqs.sinefo.ru/t-abc123/orders"; got != want {
t.Fatalf("external base: got %q, want %q", got, want)
}
// Пусто → старый формат с region.
models.CurrentEnvironment = models.Environment{Host: "goaws.com", Port: "4100", Region: "us-east-1"}
if got, want := tenantQueueURL(tt, "orders"), "http://us-east-1.goaws.com:4100/t-abc123/orders"; got != want {
t.Fatalf("local format (region): got %q, want %q", got, want)
}
// Пусто, без region.
models.CurrentEnvironment = models.Environment{Host: "localhost", Port: "4100"}
if got, want := tenantQueueURL(tt, "orders"), "http://localhost:4100/t-abc123/orders"; got != want {
t.Fatalf("local format (no region): got %q, want %q", got, want)
}
}
+1
View File
@@ -23,6 +23,7 @@ Port string
SqsPort string SqsPort string
Region string Region string
AccountID string AccountID string
ExternalURLBase string
LogToFile bool LogToFile bool
LogFile string LogFile string
EnableDuplicates bool EnableDuplicates bool