|
1 |
| -# Messenger Documentation |
| 1 | +# Messenger |
2 | 2 |
|
3 | 3 | [](https://github.com/Gerfey/messenger/actions/workflows/ci.yml)
|
4 | 4 | [](https://github.com/Gerfey/messenger/actions/workflows/security.yml)
|
|
7 | 7 | [](https://pkg.go.dev/github.com/Gerfey/messenger)
|
8 | 8 | [](LICENSE)
|
9 | 9 |
|
10 |
| -> Pre-release MVP version. Requires Go 1.24+ |
| 10 | +> ⚠️ `v0.6.0` is a pre-release version — feel free to test and report issues! |
11 | 11 |
|
12 |
| -## Installation |
| 12 | +> 📚 Full documentation available in the [GitHub Wiki](https://github.com/Gerfey/messenger/wiki/Documentation) |
13 | 13 |
|
14 |
| -```bash |
15 |
| -go get github.com/gerfey/ [email protected] |
16 |
| -``` |
| 14 | +🇷🇺 [Русская версия](README.ru.md) |
17 | 15 |
|
18 |
| -## Overview |
19 |
| -- **Multiple Transports**: AMQP (RabbitMQ), In-Memory |
| 16 | +## ✨ Features |
| 17 | +- **Multiple Transports**: AMQP (RabbitMQ), In-Memory (sync) |
20 | 18 | - **Middleware Chain**: Extensible middleware system for message processing
|
21 | 19 | - **Event-Driven**: Built-in event dispatcher for lifecycle hooks
|
22 | 20 | - **Retry Mechanism**: Configurable retry strategies with exponential backoff
|
23 | 21 | - **Message Routing**: Flexible routing system for message distribution
|
24 | 22 | - **Stamps System**: Metadata attachment for message tracking
|
25 |
| -- **YAML Configuration**: Easy configuration management |
| 23 | +- **YAML Configuration**: Easy configuration management with `%env(...)%` support |
| 24 | + |
| 25 | +## 📦 Installation |
| 26 | +> Requires Go 1.24+ |
| 27 | +```bash |
| 28 | +go get github.com/gerfey/ [email protected] |
| 29 | +``` |
26 | 30 |
|
27 |
| -## Quick Start |
| 31 | +## 🚀 Quick Start |
28 | 32 |
|
29 |
| -### 1. Define Your Message |
| 33 | +### Define Your Message |
30 | 34 |
|
31 | 35 | ```go
|
| 36 | +package main |
| 37 | + |
32 | 38 | type HelloMessage struct {
|
33 | 39 | Text string
|
34 | 40 | }
|
35 | 41 | ```
|
36 | 42 |
|
37 |
| -### 2. Create a Handler |
| 43 | +### Create a Handler |
38 | 44 |
|
39 | 45 | ```go
|
| 46 | +package main |
| 47 | + |
40 | 48 | type HelloHandler struct{}
|
41 | 49 |
|
42 | 50 | func (h *HelloHandler) Handle(ctx context.Context, msg *HelloMessage) error {
|
43 |
| - fmt.Printf("Received: %s\n", msg.Text) |
| 51 | + fmt.Println("Hello:", msg.Text) |
44 | 52 | return nil
|
45 | 53 | }
|
46 | 54 | ```
|
47 | 55 |
|
48 |
| -### 3. Configure and Run |
| 56 | +### Create config file `messenger.yaml`: |
49 | 57 |
|
50 | 58 | ```yaml
|
51 |
| -# messenger.yaml |
52 | 59 | default_bus: default
|
53 | 60 |
|
54 | 61 | buses:
|
55 | 62 | default: ~
|
56 | 63 |
|
57 | 64 | transports:
|
58 |
| - amqp: |
59 |
| - dsn: "amqp://guest:guest@localhost:5672/" |
60 |
| - options: |
61 |
| - auto_setup: true |
62 |
| - exchange: |
63 |
| - name: messages |
64 |
| - type: topic |
| 65 | + sync: |
| 66 | + dsn: "in-memory://" |
65 | 67 |
|
66 | 68 | routing:
|
67 |
| - package.HelloMessage: amqp |
| 69 | + main.HelloMessage: sync |
68 | 70 | ```
|
| 71 | +> 💡 If no transport is configured for a message, it will be executed synchronously by default (inline handler execution). |
69 | 72 |
|
70 |
| -```go |
71 |
| -package main |
| 73 | +### Initialize messenger: |
72 | 74 |
|
73 |
| -import ( |
74 |
| - "context" |
75 |
| - "log/slog" |
76 |
| - |
77 |
| - "github.com/Gerfey/messenger/builder" |
78 |
| - "github.com/Gerfey/messenger/config" |
79 |
| -) |
80 |
| - |
81 |
| -func main() { |
82 |
| - ctx := context.Background() |
83 |
| - logger := slog.Default() |
84 |
| - |
85 |
| - // Load configuration |
86 |
| - cfg, err := config.LoadConfig("messenger.yaml") |
87 |
| - if err != nil { |
88 |
| - logger.Error("failed to load config", "error", err) |
89 |
| - return |
90 |
| - } |
91 |
| - |
92 |
| - // Build messenger |
93 |
| - b := builder.NewBuilder(cfg, logger) |
94 |
| - b.RegisterHandler(&HelloHandler{}) |
95 |
| - |
96 |
| - messenger, err := b.Build() |
97 |
| - if err != nil { |
98 |
| - logger.Error("failed to build messenger", "error", err) |
99 |
| - return |
100 |
| - } |
101 |
| - |
102 |
| - // Start consuming |
103 |
| - go func() { |
104 |
| - if err := messenger.Run(ctx); err != nil { |
105 |
| - logger.Error("messenger failed", "error", err) |
106 |
| - } |
107 |
| - }() |
108 |
| - |
109 |
| - // Send message |
110 |
| - bus, _ := messenger.GetDefaultBus() |
111 |
| - bus.Dispatch(ctx, &HelloMessage{Text: "Hello, World!"}) |
| 75 | +```go |
| 76 | +cfg, errConfig := config.LoadConfig("messenger.yaml") |
| 77 | +if errConfig != nil { |
| 78 | + l.Error("ERROR load config", "error", errConfig) |
| 79 | + return |
112 | 80 | }
|
| 81 | + |
| 82 | +b := builder.NewBuilder(cfg, slog.Default()) |
| 83 | +b.RegisterHandler(&HelloHandler{}) |
| 84 | +m, _ := b.Build() |
| 85 | + |
| 86 | +ctx := context.Background() |
| 87 | +go m.Run(ctx) |
| 88 | + |
| 89 | +bus, _ := m.GetDefaultBus() |
| 90 | +_, _ = bus.Dispatch(ctx, &HelloMessage{Text: "World"}) |
| 91 | + |
| 92 | +time.Sleep(5 * time.Second) |
113 | 93 | ```
|
114 | 94 |
|
115 |
| -## Contributing |
| 95 | +## 🔍 More Examples |
| 96 | + |
| 97 | +* ✅ Commands with void return |
| 98 | +* ✅ Queries with return value access |
| 99 | +* ✅ Retry and Dead Letter Queue |
| 100 | +* ✅ Custom Middleware and Transports |
| 101 | +* ✅ Event Listeners and Lifecycle Hooks |
| 102 | + |
| 103 | +> See [Usage Scenarios](https://github.com/Gerfey/messenger/wiki/Usage-Scenarios) for commands, queries, return values and advanced use-cases. |
| 104 | +
|
| 105 | +## 🤝 Contributing |
116 | 106 |
|
117 | 107 | 1. Fork the repository
|
118 | 108 | 2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
119 | 109 | 3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
120 | 110 | 4. Push to the branch (`git push origin feature/amazing-feature`)
|
121 | 111 | 5. Open a Pull Request
|
122 | 112 |
|
123 |
| -## License |
| 113 | +## ⚖️ License |
124 | 114 |
|
125 | 115 | This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
126 | 116 |
|
127 |
| -## Acknowledgments |
| 117 | +## ⭐️ Support |
| 118 | + |
| 119 | +If you find this project useful, please consider starring ⭐️ it and sharing with others! |
| 120 | + |
| 121 | +## 🙏 Acknowledgments |
128 | 122 |
|
129 | 123 | - Inspired by [Symfony Messenger](https://symfony.com/doc/current/messenger.html)
|
130 |
| -- Built with ❤️ for the Go community |
| 124 | +- Built with ❤️ for the Go community |
0 commit comments