Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rozturac/go-mediator
π Go-Mediator
https://github.com/rozturac/go-mediator
async-await asynchronous go golang mediator mediator-pattern
Last synced: 18 days ago
JSON representation
π Go-Mediator
- Host: GitHub
- URL: https://github.com/rozturac/go-mediator
- Owner: rozturac
- License: mit
- Created: 2021-12-23T10:58:16.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2022-01-12T12:01:27.000Z (almost 3 years ago)
- Last Synced: 2024-10-21T15:03:21.856Z (2 months ago)
- Topics: async-await, asynchronous, go, golang, mediator, mediator-pattern
- Language: Go
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# π Go-Mediator
[![GitHub license](https://img.shields.io/github/license/rozturac/go-mediator.svg?color=24B898&style=for-the-badge&logo=go&logoColor=ffffff)](https://github.com/rozturac/go-mediator/blob/main/LICENSE)
[![Documentation](https://img.shields.io/badge/godoc-reference-blue.svg?color=24B898&style=for-the-badge&logo=go&logoColor=ffffff)](https://pkg.go.dev/github.com/rozturac/go-mediator)
[![Release](https://img.shields.io/github/tag/rozturac/go-mediator.svg?label=release&color=24B898&logo=github&style=for-the-badge)](https://github.com/rozturac/go-mediator/releases/latest)
[![Go Report Card](https://img.shields.io/badge/go%20report-A%2B-green?style=for-the-badge)](https://goreportcard.com/report/github.com/rozturac/go-mediator)## Installation
Via go packages:
```go get github.com/rozturac/go-mediator```## Usage
### Register Command
```go
package mainimport (
"context"
"github.com/rozturac/go-mediator"
"log"
)type TestCommand struct {
name string
}func TestCommandHandler(ctx context.Context, command mediator.Command) (interface{}, error) {
return "Success", nil
}func main() {
mediator := mediator.Create()
if err := mediator.RegisterCommand(&TestCommand{}, TestCommandHandler); err != nil {
log.Fatal(err)
}
}```
### Register Behavior
```go
func LoggingBehavior(ctx context.Context, command mediator.Command, next mediator.CommandHandler) (interface{}, error) {
log.Println(fmt.Sprintf("Request INFO - Command: %v, CorrelationId: %v", command, ctx.Value("CorrelationId")))
result, err := next(ctx, command)
log.Println(fmt.Sprintf("Response INFO - Result: %v, Error: %v", result, err))
return result, err
}mediator.WithBehavior(LoggingBehavior)
```### Send Command
```go
command := &TestCommand{name: "Test Command"}
if result, err := mediator.Send(context.Background(), command); err != nil {
log.Fatal(err)
} else {
fmt.Println(fmt.Sprintf("Response: %v", result))
}
```### Send Command As Async
```go
command := &TestCommand{name: "Test Command"}
async := mediator.SendAsync(context.Background(), command)
//We can do something here in the same time..
if result, err := async.Await(); err != nil {
log.Fatal(err)
} else {
fmt.Println(fmt.Sprintf("Response: %v", result))
}
```## License
MIT LicenseCopyright (c) 2021 RΔ±dvan ΓZTURAΓ
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.