feat(shared-sqs): seed demo data on startup (SHARED_SQS_SEED_DEMO=true, v0.1.8)
This commit is contained in:
@@ -6,7 +6,7 @@ WORKDIR /build
|
||||
COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
COPY . .
|
||||
RUN CGO_ENABLED=0 go build -o shared-sqs app/cmd/goaws.go
|
||||
RUN CGO_ENABLED=0 go build -o shared-sqs ./app/cmd/
|
||||
|
||||
FROM alpine:3.19
|
||||
RUN apk --no-cache add ca-certificates
|
||||
|
||||
@@ -77,7 +77,10 @@ log.Infof("Failed to log to file: %s, using default stdout", filename)
|
||||
|
||||
// Инициализация in-memory TenantStore
|
||||
tenantStore := tenant.NewTenantStore()
|
||||
|
||||
// Автосид демо-данных при SHARED_SQS_SEED_DEMO=true
|
||||
if os.Getenv("SHARED_SQS_SEED_DEMO") == "true" {
|
||||
seedDemoData(tenantStore)
|
||||
}
|
||||
// Роутер с tenant auth и admin API
|
||||
r := router.New(tenantStore, adminToken)
|
||||
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
// app/cmd/seed.go
|
||||
// Created: 2026-04-09
|
||||
// Автосид демо-данных при старте через SHARED_SQS_SEED_DEMO=true.
|
||||
// Создаёт тенанта demo-service с 5 очередями и демо-сообщениями.
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/md5" //nolint:gosec — MD5 используется для SQS-совместимости, не для безопасности
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
log "github.com/sirupsen/logrus"
|
||||
|
||||
"shared-sqs/app/models"
|
||||
"shared-sqs/app/tenant"
|
||||
)
|
||||
|
||||
// seedDemoData создаёт тенанта demo-service с очередями и сообщениями.
|
||||
// Вызывается при SHARED_SQS_SEED_DEMO=true при старте сервера.
|
||||
func seedDemoData(store *tenant.TenantStore) {
|
||||
t, err := store.Create("demo-service", 10)
|
||||
if err != nil {
|
||||
log.Warnf("seed: не удалось создать demo-tenant: %v", err)
|
||||
return
|
||||
}
|
||||
log.Infof("seed: создан тенант %s (AccessKey=%s)", t.ID, t.AccessKey)
|
||||
|
||||
// Демо-очереди с набором сообщений
|
||||
queues := []struct {
|
||||
name string
|
||||
msgs []string
|
||||
}{
|
||||
{
|
||||
"orders",
|
||||
[]string{
|
||||
`{"order_id":"1001","amount":99.99,"status":"pending"}`,
|
||||
`{"order_id":"1002","amount":14.50,"status":"completed"}`,
|
||||
`{"order_id":"1003","amount":299.00,"status":"processing"}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
"notifications",
|
||||
[]string{
|
||||
`{"to":"user@example.com","text":"Welcome to the service!"}`,
|
||||
`{"to":"admin@example.com","text":"New user signed up"}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
"emails",
|
||||
[]string{
|
||||
`{"subject":"Invoice #42","body":"See attachment","to":"billing@example.com"}`,
|
||||
},
|
||||
},
|
||||
{
|
||||
"uploads",
|
||||
nil,
|
||||
},
|
||||
{
|
||||
"user-events",
|
||||
[]string{
|
||||
`{"event":"login","user_id":"u-123","ts":1744000000}`,
|
||||
`{"event":"logout","user_id":"u-123","ts":1744003600}`,
|
||||
`{"event":"purchase","user_id":"u-456","item_id":"prod-7","ts":1744005000}`,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, q := range queues {
|
||||
key := t.AccessKey + ":" + q.name
|
||||
|
||||
msgs := make([]models.SqsMessage, 0, len(q.msgs))
|
||||
for _, body := range q.msgs {
|
||||
//nolint:gosec — MD5 здесь для совместимости с AWS SQS протоколом
|
||||
sum := md5.Sum([]byte(body)) //nolint:gosec
|
||||
msgs = append(msgs, models.SqsMessage{
|
||||
MessageBody: body,
|
||||
Uuid: uuid.NewString(),
|
||||
MD5OfMessageBody: fmt.Sprintf("%x", sum),
|
||||
SentTime: time.Now(),
|
||||
})
|
||||
}
|
||||
|
||||
models.SyncQueues.Lock()
|
||||
models.SyncQueues.Queues[key] = &models.Queue{
|
||||
Name: q.name,
|
||||
VisibilityTimeout: 30,
|
||||
MaximumMessageSize: 262144,
|
||||
MessageRetentionPeriod: 345600,
|
||||
Messages: msgs,
|
||||
Duplicates: make(map[string]time.Time),
|
||||
}
|
||||
models.SyncQueues.Unlock()
|
||||
|
||||
log.Infof("seed: очередь %s (%d сообщений)", q.name, len(q.msgs))
|
||||
}
|
||||
|
||||
log.Infof("seed: демо-данные готовы — тенант %s, 5 очередей", t.Name)
|
||||
}
|
||||
@@ -22,7 +22,7 @@ spec:
|
||||
spec:
|
||||
containers:
|
||||
- name: shared-sqs
|
||||
image: naeel/shared-sqs:v0.1.6
|
||||
image: naeel/shared-sqs:v0.1.8
|
||||
ports:
|
||||
- containerPort: 4100
|
||||
name: http
|
||||
@@ -32,6 +32,8 @@ spec:
|
||||
secretKeyRef:
|
||||
name: shared-sqs-admin
|
||||
key: token
|
||||
- name: SHARED_SQS_SEED_DEMO
|
||||
value: "true"
|
||||
resources:
|
||||
requests:
|
||||
memory: "64Mi"
|
||||
|
||||
Reference in New Issue
Block a user