Skip to content

Commit

Permalink
Add kafka example
Browse files Browse the repository at this point in the history
  • Loading branch information
zalbiraw committed Nov 14, 2024
1 parent 6b47a7c commit 6e5d1ab
Show file tree
Hide file tree
Showing 10 changed files with 712 additions and 56 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
.idea
vendor
server
**/.DS_Store
22 changes: 20 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ FROM golang:1.23-alpine AS builder

LABEL maintainer="Zaid Albirawi"

RUN apk add --no-progress --no-cache gcc musl-dev

# Set the working directory inside the container
WORKDIR /go/src/github.com/zalbiraw/go-api-test-service

Expand All @@ -21,21 +23,37 @@ RUN CGO_ENABLED=0 GO111MODULE=on go build -o services/rest/rest/server
RUN CGO_ENABLED=0 GO111MODULE=on go build -o services/rest/users/server services/rest/users/server.go
RUN CGO_ENABLED=0 GO111MODULE=on go build -o services/rest/posts/server services/rest/posts/server.go
RUN CGO_ENABLED=0 GO111MODULE=on go build -o services/rest/comments/server services/rest/comments/server.go

RUN CGO_ENABLED=0 GO111MODULE=on go build -o services/graphql/users/server services/graphql/users/server.go
RUN CGO_ENABLED=0 GO111MODULE=on go build -o services/graphql/posts/server services/graphql/posts/server.go
RUN CGO_ENABLED=0 GO111MODULE=on go build -o services/graphql/comments/server services/graphql/comments/server.go
RUN CGO_ENABLED=0 GO111MODULE=on go build -o services/graphql/notifications/server services/graphql/notifications/server.go

RUN CGO_ENABLED=0 GO111MODULE=on go build -o services/graphql-subgraphs/users/server services/graphql-subgraphs/users/server.go
RUN CGO_ENABLED=0 GO111MODULE=on go build -o services/graphql-subgraphs/posts/server services/graphql-subgraphs/posts/server.go
RUN CGO_ENABLED=0 GO111MODULE=on go build -o services/graphql-subgraphs/comments/server services/graphql-subgraphs/comments/server.go
RUN CGO_ENABLED=0 GO111MODULE=on go build -o services/graphql-subgraphs/notifications/server services/graphql-subgraphs/notifications/server.go

RUN CGO_ENABLED=1 GO111MODULE=on go build -tags musl -ldflags '-extldflags "-static"' -o services/kafka/notifications/producer services/kafka/notifications/producer.go

# Stage 2: Create the final lightweight image
FROM alpine:latest

# Set the working directory
WORKDIR /go/src/github.com/zalbiraw/go-api-test-service

# Copy all the built binaries from the builder stage
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/helpers /go/src/github.com/zalbiraw/go-api-test-service/helpers
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/services /go/src/github.com/zalbiraw/go-api-test-service/services
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/helpers /go/src/github.com/zalbiraw/go-api-test-service/helpers
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/services/rest/rest/server /go/src/github.com/zalbiraw/go-api-test-service/services/rest/rest/server
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/services/rest/users/server /go/src/github.com/zalbiraw/go-api-test-service/services/rest/users/server
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/services/rest/posts/server /go/src/github.com/zalbiraw/go-api-test-service/services/rest/posts/server
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/services/rest/comments/server /go/src/github.com/zalbiraw/go-api-test-service/services/rest/comments/server
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/services/graphql/users/server /go/src/github.com/zalbiraw/go-api-test-service/services/graphql/users/server
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/services/graphql/posts/server /go/src/github.com/zalbiraw/go-api-test-service/services/graphql/posts/server
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/services/graphql/comments/server /go/src/github.com/zalbiraw/go-api-test-service/services/graphql/comments/server
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/services/graphql/notifications/server /go/src/github.com/zalbiraw/go-api-test-service/services/graphql/notifications/server
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/services/graphql-subgraphs/users/server /go/src/github.com/zalbiraw/go-api-test-service/services/graphql-subgraphs/users/server
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/services/graphql-subgraphs/posts/server /go/src/github.com/zalbiraw/go-api-test-service/services/graphql-subgraphs/posts/server
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/services/graphql-subgraphs/comments/server /go/src/github.com/zalbiraw/go-api-test-service/services/graphql-subgraphs/comments/server
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/services/graphql-subgraphs/notifications/server /go/src/github.com/zalbiraw/go-api-test-service/services/graphql-subgraphs/notifications/server
COPY --from=builder /go/src/github.com/zalbiraw/go-api-test-service/services/kafka/notifications/producer /go/src/github.com/zalbiraw/go-api-test-service/services/kafka/notifications/producer
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# go-api-test-service
This library allows you to launch different REST, GraphQL and GraphQL subgraphs to help test your APIs.
This library allows you to launch different REST, GraphQL, GraphQL subgraphs, and Kafka producers to help test your APIs.

The available services are:
- rest
Expand All @@ -14,6 +14,7 @@ The available services are:
- posts-subgraph
- comments-subgraph
- notifications-subgraph (federated subscription)
- notifications-kafka-producer

The same docker file will allow you to run the different services. You can launch the service you want by setting the entrypoint to the docker container to be `entrypoint: ./$SERVICE_NAME/server`.

Expand Down
93 changes: 68 additions & 25 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,132 +1,175 @@
services:
rest:
image: zalbiraw/go-api-test-service:v3.3.0
image: zalbiraw/go-api-test-service:v3.4.0
container_name: rest
entrypoint: ./services/rest/rest/server

environment:
- PORT=3100
PORT: 3100

ports:
- "3100:3100"

users-rest:
image: zalbiraw/go-api-test-service:v3.3.0
image: zalbiraw/go-api-test-service:v3.4.0
container_name: users-rest
entrypoint: ./services/rest/users/server

environment:
- PORT=3101
PORT: 3101

ports:
- "3101:3101"

posts-rest:
image: zalbiraw/go-api-test-service:v3.3.0
image: zalbiraw/go-api-test-service:v3.4.0
container_name: posts-rest
entrypoint: ./services/rest/posts/server

environment:
- PORT=3102
PORT: 3102

ports:
- "3102:3102"

comments-rest:
image: zalbiraw/go-api-test-service:v3.3.0
image: zalbiraw/go-api-test-service:v3.4.0
container_name: comments-rest
entrypoint: ./services/rest/comments/server

environment:
- PORT=3103
PORT: 3103

ports:
- "3103:3103"

users-graph:
image: zalbiraw/go-api-test-service:v3.3.0
image: zalbiraw/go-api-test-service:v3.4.0
container_name: users-graph
entrypoint: ./services/graphql/users/server

environment:
- PORT=4101
PORT: 4101

ports:
- "4101:4101"

posts-graph:
image: zalbiraw/go-api-test-service:v3.3.0
image: zalbiraw/go-api-test-service:v3.4.0
container_name: posts-graph
entrypoint: ./services/graphql/posts/server

environment:
- PORT=4102
PORT: 4102

ports:
- "4102:4102"

comments-graph:
image: zalbiraw/go-api-test-service:v3.3.0
image: zalbiraw/go-api-test-service:v3.4.0
container_name: comments-graph
entrypoint: ./services/graphql/comments/server

environment:
- PORT=4103
PORT: 4103

ports:
- "4103:4103"

notifications-graph:
image: zalbiraw/go-api-test-service:v3.3.0
image: zalbiraw/go-api-test-service:v3.4.0
container_name: notifications-graph
entrypoint: ./services/graphql/notifications/server

environment:
- PORT=4104
PORT: 4104

ports:
- "4104:4104"

users-subgraph:
image: zalbiraw/go-api-test-service:v3.3.0
image: zalbiraw/go-api-test-service:v3.4.0
container_name: users-subgraph
entrypoint: ./services/graphql-subgraphs/users/server

environment:
- PORT=4201
PORT: 4201

ports:
- "4201:4201"

posts-subgraph:
image: zalbiraw/go-api-test-service:v3.3.0
image: zalbiraw/go-api-test-service:v3.4.0
container_name: posts-subgraph
entrypoint: ./services/graphql-subgraphs/posts/server

environment:
- PORT=4202
PORT: 4202

ports:
- "4202:4202"

comments-subgraph:
image: zalbiraw/go-api-test-service:v3.3.0
image: zalbiraw/go-api-test-service:v3.4.0
container_name: comments-subgraph
entrypoint: ./services/graphql-subgraphs/comments/server

environment:
- PORT=4203
PORT: 4203

ports:
- "4203:4203"

notifications-subgraph:
image: zalbiraw/go-api-test-service:v3.3.0
image: zalbiraw/go-api-test-service:v3.4.0
container_name: notifications-subgraph
entrypoint: ./services/graphql-subgraphs/notifications/server

environment:
- PORT=4204
PORT: 4204

ports:
- "4204:4204"
- "4204:4204"

notifications-kafka-producer:
image: zalbiraw/go-api-test-service:v3.4.0
container_name: notifications-kafka-producer
entrypoint: ./services/kafka/notifications/producer

networks:
- kafka

environment:
KAFKA_BROKER_URL: kafka:9092
KAFKA_TOPIC: test
PRODUCE_INTERVAL: 5s

depends_on:
- kafka

kafka:
image: apache/kafka:3.9.0
container_name: kafka

networks:
- kafka

environment:
KAFKA_NODE_ID: 1
KAFKA_PROCESS_ROLES: broker,controller
KAFKA_LISTENERS: PLAINTEXT://:9092,CONTROLLER://:9093
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka:9092
KAFKA_CONTROLLER_LISTENER_NAMES: CONTROLLER
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
KAFKA_CONTROLLER_QUORUM_VOTERS: 1@localhost:9093
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_GROUP_INITIAL_REBALANCE_DELAY_MS: 0
KAFKA_NUM_PARTITIONS: 3

ports:
- "9092:9092"

networks:
kafka:
25 changes: 14 additions & 11 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ go 1.23

require (
github.com/99designs/gqlgen v0.17.13
github.com/confluentinc/confluent-kafka-go/v2 v2.6.0
github.com/go-chi/chi v1.5.4
github.com/gorilla/websocket v1.5.0
github.com/linkedin/goavro v2.1.0+incompatible
github.com/vektah/gqlparser/v2 v2.4.6
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0
go.opentelemetry.io/otel v1.29.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.29.0
go.opentelemetry.io/otel/sdk v1.29.0
gopkg.in/loremipsum.v1 v1.1.0
)

Expand All @@ -16,27 +22,24 @@ require (
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/golang/snappy v0.0.4 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/grpc-ecosystem/grpc-gateway/v2 v2.22.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/logrusorgru/aurora/v3 v3.0.0 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.4.3 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.54.0 // indirect
go.opentelemetry.io/otel v1.29.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.29.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.29.0 // indirect
go.opentelemetry.io/otel/metric v1.29.0 // indirect
go.opentelemetry.io/otel/sdk v1.29.0 // indirect
go.opentelemetry.io/otel/trace v1.29.0 // indirect
go.opentelemetry.io/proto/otlp v1.3.1 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240822170219-fc7c04adadcd // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240822170219-fc7c04adadcd // indirect
google.golang.org/grpc v1.65.0 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gopkg.in/linkedin/goavro.v1 v1.0.5 // indirect
)
Loading

0 comments on commit 6e5d1ab

Please sign in to comment.