From f0e7cc905d7e5a1bea71a24574c4392d0e07b5c3 Mon Sep 17 00:00:00 2001 From: Repinoid Date: Sat, 10 Oct 2026 13:44:22 +0300 Subject: [PATCH] =?UTF-8?q?feat(queue-url):=20ExternalURLBase=20=D0=B4?= =?UTF-8?q?=D0=BB=D1=8F=20=D0=BF=D1=83=D0=B1=D0=BB=D0=B8=D1=87=D0=BD=D0=BE?= =?UTF-8?q?=D0=B3=D0=BE=20QueueUrl=20(+=20env=20SHARED=5FSQS=5FEXTERNAL=5F?= =?UTF-8?q?URL=5FBASE),=20=D1=82=D0=B5=D1=81=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/conf/config.go | 8 ++++++++ app/conf/goaws.yaml | 1 + app/gosqs/tenant_helpers.go | 5 +++++ app/gosqs/tenant_helpers_test.go | 35 ++++++++++++++++++++++++++++++++ app/models/configuration.go | 1 + 5 files changed, 50 insertions(+) create mode 100644 app/gosqs/tenant_helpers_test.go diff --git a/app/conf/config.go b/app/conf/config.go index 8fea77c..af9fcb3 100644 --- a/app/conf/config.go +++ b/app/conf/config.go @@ -67,6 +67,11 @@ func LoadYamlConfig(filename string, env string) []string { 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 != "" { ports = []string{envs[env].Port} } @@ -90,6 +95,9 @@ func LoadYamlConfig(filename string, env string) []string { queueUrl = "http://" + models.CurrentEnvironment.Region + "." + models.CurrentEnvironment.Host + ":" + 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 if queue.ReceiveMessageWaitTimeSeconds == 0 { diff --git a/app/conf/goaws.yaml b/app/conf/goaws.yaml index 24cd6a6..7db2f08 100755 --- a/app/conf/goaws.yaml +++ b/app/conf/goaws.yaml @@ -4,6 +4,7 @@ Local: # Environment name that can be passed on the Port: 4100 # port to listen on. Region: us-east-1 AccountId: "100010001000" + ExternalURLBase: "" # публичный адрес очередей (напр. https://sqs.sinefo.ru); пусто → http://{region}.{host}:{port} LogToFile: false # Log messages (true/false) LogFile: .st/goaws_messages.log # Log filename (for message logging EnableDuplicates: false # Enable or not deduplication based on messageDeduplicationId diff --git a/app/gosqs/tenant_helpers.go b/app/gosqs/tenant_helpers.go index 8aa422f..623f574 100644 --- a/app/gosqs/tenant_helpers.go +++ b/app/gosqs/tenant_helpers.go @@ -5,6 +5,7 @@ package gosqs import ( "net/http" + "strings" "shared-sqs/app/auth" "shared-sqs/app/models" @@ -27,6 +28,10 @@ return t // tenantQueueURL — формирует URL очереди для тенанта. // Ловушка #10: QueueUrl ОБЯЗАН содержать tenantID в пути, иначе AWS SDK не сможет send/receive. 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 port := models.CurrentEnvironment.Port region := models.CurrentEnvironment.Region diff --git a/app/gosqs/tenant_helpers_test.go b/app/gosqs/tenant_helpers_test.go new file mode 100644 index 0000000..fd30068 --- /dev/null +++ b/app/gosqs/tenant_helpers_test.go @@ -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) + } +} diff --git a/app/models/configuration.go b/app/models/configuration.go index 4053973..7427a9a 100644 --- a/app/models/configuration.go +++ b/app/models/configuration.go @@ -23,6 +23,7 @@ Port string SqsPort string Region string AccountID string +ExternalURLBase string LogToFile bool LogFile string EnableDuplicates bool