Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
dipjyotimetia committed Sep 16, 2023
1 parent ae5f407 commit 2ce027b
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 25 deletions.
Empty file added README.md
Empty file.
10 changes: 5 additions & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"log"
"os"
Expand All @@ -16,7 +17,6 @@ import (
"github.com/gofiber/fiber/v2/middleware/cors"
"github.com/gofiber/fiber/v2/middleware/helmet"
"github.com/gofiber/fiber/v2/middleware/logger"
"github.com/gofiber/fiber/v2/middleware/monitor"
)

const idleTimeout = 5 * time.Second
Expand All @@ -34,7 +34,6 @@ func main() {
TimeFormat: "02-Jan-2006",
TimeZone: "UTC",
}))
app.Get("/metrics", monitor.New(monitor.Config{Title: "MyService Metrics Page"}))
app.Get("/health", func(ctx *fiber.Ctx) error {
return ctx.Send([]byte("healthy"))
})
Expand All @@ -44,9 +43,10 @@ func main() {
log.Fatalf("error loading config")
}

ctx := context.Background()
cc := events.NewKafkaClient(cfg)
api := app.Group("/api")
router.ExpenseRouter(api, cc, cfg)
router.ExpenseRouter(api, ctx, cc, cfg)
// Listen from a different goroutine
go func() {
if err := app.Listen(":8083"); err != nil {
Expand All @@ -60,7 +60,7 @@ func main() {
fmt.Println("Gracefully shutting down...")
_ = app.Shutdown()

fmt.Println("Running cleanup tasks...")
log.Println("Running cleanup tasks...")
cc.Close()
fmt.Println("Fiber was successful shutdown.")
log.Println("Fiber was successful shutdown.")
}
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ services:
environment:
BROKERS: kafka:9092
TOPICS: expense-topic
SCHEMAREGISTRY: kafka:8081
SCHEMAREGISTRY: kafka:8081

kafka:
image: docker.vectorized.io/vectorized/redpanda:latest
Expand Down Expand Up @@ -46,7 +46,7 @@ services:
context: ./docker
dockerfile: Dockerfile
depends_on:
- kafka
- kafka

console:
image: docker.redpanda.com/redpandadata/console:latest
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ go 1.21
require (
github.com/caarlos0/env/v8 v8.0.0
github.com/goccy/go-json v0.10.2
github.com/gofiber/fiber/v2 v2.49.1
github.com/hamba/avro/v2 v2.15.1
github.com/gofiber/fiber/v2 v2.49.2
github.com/hamba/avro/v2 v2.16.0
github.com/linkedin/goavro/v2 v2.12.0
github.com/twmb/franz-go v1.14.4
github.com/twmb/franz-go/pkg/sr v0.0.0-20230905214301-01651affd204
Expand All @@ -25,10 +25,10 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pierrec/lz4/v4 v4.1.18 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/twmb/franz-go/pkg/kmsg v1.6.1 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.49.0 // indirect
github.com/valyala/fasthttp v1.50.0 // indirect
github.com/valyala/tcplisten v1.0.0 // indirect
golang.org/x/sys v0.11.0 // indirect
golang.org/x/sys v0.12.0 // indirect
)
19 changes: 10 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/gofiber/fiber/v2 v2.49.1 h1:0W2DRWevSirc8pJl4o8r8QejDR8TV6ZUCawHxwbIdOk=
github.com/gofiber/fiber/v2 v2.49.1/go.mod h1:nPUeEBUeeYGgwbDm59Gp7vS8MDyScL6ezr/Np9A13WU=
github.com/gofiber/fiber/v2 v2.49.2 h1:ONEN3/Vc+dUCxxDgZZwpqvhISgHqb+bu+isBiEyKEQs=
github.com/gofiber/fiber/v2 v2.49.2/go.mod h1:gNsKnyrmfEWFpJxQAV0qvW6l70K1dZGno12oLtukcts=
github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hamba/avro/v2 v2.15.1 h1:vIeEMKARCNXLa52NurXOF3X4DUpH8AdjAlDI/Z9j4dg=
github.com/hamba/avro/v2 v2.15.1/go.mod h1:Q9YK+qxAhtVrNqOhwlZTATLgLA8qxG2vtvkhK8fJ7Jo=
github.com/hamba/avro/v2 v2.16.0 h1:0XhyP65Hs8iMLtdSR0v7ZrwRjsbIZdvr7KzYgmx1Mbo=
github.com/hamba/avro/v2 v2.16.0/go.mod h1:Q9YK+qxAhtVrNqOhwlZTATLgLA8qxG2vtvkhK8fJ7Jo=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.16.7 h1:2mk3MPGNzKyxErAw8YaohYh69+pa4sIQSC0fPGCFR9I=
Expand All @@ -41,8 +41,9 @@ github.com/pierrec/lz4/v4 v4.1.18 h1:xaKrnTkyoqfh1YItXl56+6KJNVYWlEEPuAQW9xsplYQ
github.com/pierrec/lz4/v4 v4.1.18/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis=
github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
Expand All @@ -57,14 +58,14 @@ github.com/twmb/franz-go/pkg/sr v0.0.0-20230905214301-01651affd204 h1:RO2BkMdqAT
github.com/twmb/franz-go/pkg/sr v0.0.0-20230905214301-01651affd204/go.mod h1:egX+kicq83hpztv3PRCXKLNO132Ol9JTAJOCRZcqUxI=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasthttp v1.49.0 h1:9FdvCpmxB74LH4dPb7IJ1cOSsluR07XG3I1txXWwJpE=
github.com/valyala/fasthttp v1.49.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
github.com/valyala/fasthttp v1.50.0 h1:H7fweIlBm0rXLs2q0XbalvJ6r0CUPFWK3/bB4N13e9M=
github.com/valyala/fasthttp v1.50.0/go.mod h1:k2zXd82h/7UZc3VOdJ2WaUqt1uZ/XpXAfE9i+HBC3lA=
github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8=
github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.11.0 h1:eG7RXZHdqOJ1i+0lgLgCpSXAp6M3LYlAo6osgSi0xOM=
golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0 h1:CM0HF96J0hcLAwsHPJZjfdNzs0gftsLfgKt57wWHJ0o=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
4 changes: 2 additions & 2 deletions pkg/handler/expenseHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
// sets the Timestamp field to the current time if it's not already set,
// creates a Kafka record with the expense data, and sends it to the Kafka topic.
// Finally, it returns a success response.
func ExpenseHandler(client *events.KafkaClient, cfg *config.Config) fiber.Handler {
func ExpenseHandler(ctx context.Context, client *events.KafkaClient, cfg *config.Config) fiber.Handler {
// Parse the JSON request body into an Expense struct
return func(c *fiber.Ctx) error {
var expense gen.Expense
Expand All @@ -38,7 +38,7 @@ func ExpenseHandler(client *events.KafkaClient, cfg *config.Config) fiber.Handle
record := client.SetExpenseRecord(cfg, expense)

// Send the Kafka record to the Kafka topic
err := client.Producer(context.Background(), record)
err := client.Producer(ctx, record)
if err != nil {
c.Status(http.StatusInternalServerError)
}
Expand Down
6 changes: 4 additions & 2 deletions pkg/router/expenseRouter.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package router

import (
"context"

"github.com/dipjyotimetia/event-stream/pkg/config"
"github.com/dipjyotimetia/event-stream/pkg/events"
"github.com/dipjyotimetia/event-stream/pkg/handler"
"github.com/gofiber/fiber/v2"
)

// ExpenseRouter is the Router for GoFiber App
func ExpenseRouter(app fiber.Router, client *events.KafkaClient, cfg *config.Config) {
app.Post("/expense", handler.ExpenseHandler(client, cfg))
func ExpenseRouter(app fiber.Router,ctx context.Context, client *events.KafkaClient, cfg *config.Config) {
app.Post("/expense", handler.ExpenseHandler(ctx,client, cfg))
}

0 comments on commit 2ce027b

Please sign in to comment.