Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .env-example
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
SLACK_WEBHOOK_URL=
defaultBindAddr=:8080
defaultMetricsBindAddr=:8081
defaultGrafanaURL=http://grafana:3000
defaultGrafanaCredentials=admin:secret
defaultAMQPURI=amqp://guest:guest@rabbitmq:5672/
defaultAMQPQueue=deployments
defaultKafkaBrokers=kafka:9092
defaultKafkaTopic=events
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Event-Driven application example
# Event Driven Architecture with Golang (Example)

This is an example Event-Driven application written in Go, using [Watermill](https://github.com/ThreeDotsLabs/watermill).

Expand All @@ -8,7 +8,7 @@ demonstrate working with multiple event streams.

![](https://threedots.tech/media/event-driven-applications/diagram.png)

An example result can look like this:
An example result can look like this:

![](https://threedots.tech/media/event-driven-applications/grafana.png)

Expand All @@ -21,10 +21,10 @@ webhook URL in `SLACK_WEBHOOK_URL` variable.

In addition to the application, the docker-compose environment consists of:

* **Kafka** and **ZooKeeper**
* **RabbitMQ**
* **Grafana**
* **Prometheus**
- **Kafka** and **ZooKeeper**
- **RabbitMQ**
- **Grafana**
- **Prometheus**

The whole environment can be run with:

Expand Down
18 changes: 18 additions & 0 deletions app/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Step 1: Modules caching
FROM golang:1.21.4-alpine3.18 as modules
COPY go.mod go.sum /modules/
WORKDIR /modules
RUN go mod download

# Step 2: Builder
FROM golang:1.21.4-alpine3.18 as builder
COPY --from=modules /go/pkg /go/pkg
COPY . /app
WORKDIR /app
RUN CGO_ENABLED=0 GOOS=linux GOARCH=arm64 \
go build -tags migrate -o /bin/app ./cmd/app

# Step 3: Final
FROM scratch
COPY --from=builder /bin/app /app
CMD ["/app"]
29 changes: 22 additions & 7 deletions cmd/main.go → app/cmd/app/main.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,52 @@
package main

import (
"github.com/ThreeDotsLabs/event-driven-example/pkg"
"context"

"github.com/ThreeDotsLabs/event-driven-example/config"
"github.com/ThreeDotsLabs/event-driven-example/internal"
"github.com/ThreeDotsLabs/watermill"
"github.com/ThreeDotsLabs/watermill/components/metrics"
"github.com/ThreeDotsLabs/watermill/message"
"github.com/ThreeDotsLabs/watermill/message/router/middleware"
)

func main() {
// Create a new context and add a cancel function to it.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Create a new logger.
logger := watermill.NewStdLogger(false, false)
config := pkg.LoadConfig()
c := config.LoadConfig()

// Create a new router.
router, err := message.NewRouter(message.RouterConfig{}, logger)
if err != nil {
panic(err)
}
router.AddMiddleware(middleware.Recoverer)

// Metrics setup
prometheusRegistry, closeMetricsServer := metrics.CreateRegistryAndServeHTTP(config.MetricsBindAddr)
// Create a new Prometheus registry and serve it on the given address.
prometheusRegistry, closeMetricsServer := metrics.CreateRegistryAndServeHTTP(c.MetricsBindAddr)
defer closeMetricsServer()
metricsBuilder := metrics.NewPrometheusMetricsBuilder(prometheusRegistry, "", "")
metricsBuilder.AddPrometheusRouterMetrics(router)

err = pkg.SetupRouter(router, config, logger)
// Setup the router.
r := &internal.Router{
Router: router,
Logger: logger,
Config: c,
}
err = r.SetupRouter()
if err != nil {
panic(err)
}

err = router.Run()
if err != nil {
// Run the router.
if err := router.Run(ctx); err != nil {
panic(err)
}

}
8 changes: 4 additions & 4 deletions pkg/config.go → app/config/config.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package pkg
package config

import (
"os"
Expand All @@ -23,15 +23,15 @@ type Config struct {
BindAddr string
MetricsBindAddr string

GrafanaURL string
GrafanaCredentials string

AMQPURI string
AMQPQueue string

KafkaBrokers []string
KafkaTopic string

GrafanaURL string
GrafanaCredentials string

SlackWebhookURL string
}

Expand Down
57 changes: 57 additions & 0 deletions app/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module github.com/ThreeDotsLabs/event-driven-example

go 1.21

require (
github.com/ThreeDotsLabs/watermill v1.3.5
github.com/ThreeDotsLabs/watermill-amqp v1.1.4
github.com/ThreeDotsLabs/watermill-http v1.1.4
github.com/ThreeDotsLabs/watermill-kafka/v2 v2.5.0
)

require (
github.com/Shopify/sarama v1.38.0 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cenkalti/backoff/v3 v3.2.2 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/eapache/go-resiliency v1.3.0 // indirect
github.com/eapache/go-xerial-snappy v0.0.0-20180814174437-776d5712da21 // indirect
github.com/eapache/queue v1.1.0 // indirect
github.com/go-chi/chi v4.0.2+incompatible // indirect
github.com/go-chi/chi/v5 v5.0.8 // indirect
github.com/go-chi/render v1.0.1 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.3.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
github.com/jcmturner/gofork v1.7.6 // indirect
github.com/jcmturner/gokrb5/v8 v8.4.3 // indirect
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/lithammer/shortuuid/v3 v3.0.7 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pierrec/lz4/v4 v4.1.17 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/client_golang v1.14.0 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.39.0 // indirect
github.com/prometheus/procfs v0.9.0 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/sony/gobreaker v0.5.0 // indirect
github.com/streadway/amqp v1.0.0 // indirect
go.opentelemetry.io/contrib/instrumentation/github.com/Shopify/sarama/otelsarama v0.31.0 // indirect
go.opentelemetry.io/otel v1.6.1 // indirect
go.opentelemetry.io/otel/trace v1.6.1 // indirect
golang.org/x/crypto v0.0.0-20220722155217-630584e8d5aa // indirect
golang.org/x/net v0.4.0 // indirect
golang.org/x/sys v0.4.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
)
Loading