Restructure the monorepo into clear top-level directories: - backend/: Go module root (cmd, internal, pkg, configs, migrations, docs/swagger, scripts, tests, go.mod, Makefile, .air.toml) - deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd - docs/: project documentation + reports/ (moved from repo root) - AGENTS.md: new AI coding-agent guide at repo root Update all references to the new layout: - Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root) - docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile, env_file ../../.env, volume mounts ../../backend:/app - deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile - CI: working-directory: backend for go commands, file deploy/docker/Dockerfile, coverage path backend/coverage.out, health_check backend/scripts/ - backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../ - README: architecture tree, quickstart, config paths updated Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh, gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to preserve history. Build, vet, SQLite tests, and docker compose config verified.
59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
package pubsub
|
|
|
|
import "context"
|
|
|
|
// Event represents a real-time event to be published.
|
|
type Event struct {
|
|
Type string `json:"type"` // message_created, conversation_updated, etc
|
|
AccountID uint `json:"account_id"`
|
|
Payload map[string]interface{} `json:"payload"`
|
|
}
|
|
|
|
// PubSub defines the interface for publishing and subscribing to real-time events.
|
|
// Implementations can use Redis pub/sub, NATS, or in-process channels.
|
|
type PubSub interface {
|
|
// Publish sends an event to all subscribers of the given topic.
|
|
Publish(ctx context.Context, topic string, event Event) error
|
|
|
|
// Subscribe registers a handler for events on the given topic.
|
|
Subscribe(ctx context.Context, topic string, handler EventHandler) error
|
|
|
|
// Unsubscribe removes a handler from the given topic.
|
|
Unsubscribe(ctx context.Context, topic string) error
|
|
}
|
|
|
|
// EventHandler is a callback function that processes received events.
|
|
type EventHandler func(event Event)
|
|
|
|
// InMemoryPubSub is a simple in-process pub/sub for development/testing.
|
|
type InMemoryPubSub struct {
|
|
subscribers map[string][]EventHandler
|
|
}
|
|
|
|
// NewInMemoryPubSub creates a new in-memory pub/sub instance.
|
|
func NewInMemoryPubSub() *InMemoryPubSub {
|
|
return &InMemoryPubSub{
|
|
subscribers: make(map[string][]EventHandler),
|
|
}
|
|
}
|
|
|
|
func (ps *InMemoryPubSub) Publish(ctx context.Context, topic string, event Event) error {
|
|
handlers, ok := ps.subscribers[topic]
|
|
if !ok {
|
|
return nil
|
|
}
|
|
for _, handler := range handlers {
|
|
handler(event)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (ps *InMemoryPubSub) Subscribe(ctx context.Context, topic string, handler EventHandler) error {
|
|
ps.subscribers[topic] = append(ps.subscribers[topic], handler)
|
|
return nil
|
|
}
|
|
|
|
func (ps *InMemoryPubSub) Unsubscribe(ctx context.Context, topic string) error {
|
|
delete(ps.subscribers, topic)
|
|
return nil
|
|
} |