This project demonstrates an event-driven architecture using Go. It includes a simple example of creating an order, processing order payment, updating stock movement, and sending an order email.
- Go 1.16 or higher
- RabbitMQ
-
Clone the repository:
git clone https://github.com/AdiPP/go-event-driven.git cd go-event-driven
-
Install dependencies:
go mod tidy
-
Ensure RabbitMQ is running locally:
docker compose up -d
The project is structured as follows:
main.go
: The entry point of the application.internal/application/controller
: Contains the HTTP controllers.internal/application/usecase
: Contains the use cases for the application.internal/domain/event
: Contains the domain events.internal/infra/queue
: Contains the queue adapter implementation (RabbitMQ in this case).
- The client sends a POST request to create an order.
- The
OrderController
handles the request and triggers theCreateOrderUseCase
. - The
CreateOrderUseCase
publishes anOrderCreatedEvent
to the RabbitMQ queue. - The application listens for the
OrderCreatedEvent
and processes it through various handlers:ProcessOrderPayment
,StockMovement
, andSendOrderEmail
.
-
Start the application:
go run main.go
-
The server will start and listen on port 8080.
- Create Order:
POST /create-order
- This endpoint is used to create a new order.
curl -X POST http://localhost:8080/create-order -H "Accept: application/json" -H "Content-Type: application/json" -d '{"items":[{"product_id":"1","qtd":10}]}'
Contributions are welcome! Please fork this repository and submit a pull request.
This project is licensed under the MIT License.