-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
93d259d
commit bb7c050
Showing
15 changed files
with
229 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,14 @@ | ||
test: | ||
go test ./... -v --tags=integration | ||
|
||
build: | ||
docker compose up -d | ||
|
||
clean: | ||
docker compose down --rmi all --volumes | ||
|
||
code-gen: | ||
go generate ./... | ||
|
||
schema-gen: | ||
go run script/avsc2json/main.go schema/avro/expense.avsc > docker/schema/expense.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"schema": "{ \"type\": \"record\", \"namespace\": \"com.expense\", \"name\": \"Expense\", \"fields\": [ { \"name\": \"expense_id\", \"type\": \"string\" }, { \"name\": \"user_id\", \"type\": \"string\" }, { \"name\": \"category\", \"type\": \"string\" }, { \"name\": \"amount\", \"type\": \"double\" }, { \"name\": \"currency\", \"type\": \"string\" }, { \"name\": \"timestamp\", \"type\": \"long\", \"logicalType\": \"timestamp-millis\" }, { \"name\": \"description\", \"type\": [\"null\", \"string\"], \"default\": null }, { \"name\": \"receipt\", \"type\": [\"null\", \"string\"], \"default\": null } ]}" | ||
} | ||
"schema": "{\"name\":\"com.expense.Expense\",\"type\":\"record\",\"fields\":[{\"name\":\"expense_id\",\"type\":\"string\"},{\"name\":\"user_id\",\"type\":\"string\"},{\"name\":\"category\",\"type\":\"string\"},{\"name\":\"amount\",\"type\":\"double\"},{\"name\":\"currency\",\"type\":\"string\"},{\"name\":\"timestamp\",\"type\":\"long\"},{\"name\":\"description\",\"type\":[\"null\",\"string\"]},{\"name\":\"receipt\",\"type\":[\"null\",\"string\"]}]}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package handler | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
"time" | ||
|
||
"github.com/dipjyotimetia/event-stream/gen" | ||
"github.com/dipjyotimetia/event-stream/pkg/config" | ||
"github.com/dipjyotimetia/event-stream/pkg/events" | ||
"github.com/gofiber/fiber/v2" | ||
) | ||
|
||
func PaymentHandler(ctx context.Context, client *events.KafkaClient, cfg *config.Config) fiber.Handler { | ||
return func(c *fiber.Ctx) error { | ||
var payment gen.Payment | ||
|
||
if err := c.BodyParser(&payment); err != nil { | ||
c.Status(http.StatusBadRequest) | ||
return err | ||
} | ||
|
||
// Set the Timestamp field to current time if it's not already set | ||
if payment.Timestamp == 0 { | ||
payment.Timestamp = time.Now().UnixNano() / int64(time.Millisecond) | ||
} | ||
|
||
record, err := client.SetRecord(cfg, payment, "payment-topic", gen.Payment{}) | ||
if err != nil { | ||
c.Status(http.StatusInternalServerError) | ||
} | ||
|
||
err = client.Producer(ctx, record) | ||
if err != nil { | ||
c.Status(http.StatusInternalServerError) | ||
} | ||
c.SendStatus(http.StatusOK) //nolint:errcheck | ||
return c.Send([]byte("expense created successfully")) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/hamba/avro/v2" | ||
) | ||
|
||
func main() { | ||
if len(os.Args) != 2 { | ||
fmt.Println("Usage: avsc_to_json <input.avsc>") | ||
return | ||
} | ||
|
||
avscFilename := os.Args[1] | ||
|
||
avscBytes, err := os.ReadFile(avscFilename) | ||
if err != nil { | ||
fmt.Println("Error reading AVSC file:", err) | ||
return | ||
} | ||
|
||
// Load the schema from a file or some other source | ||
schema, err := avro.Parse(string(avscBytes)) | ||
if err != nil { | ||
fmt.Println("Error parsing schema:", err) | ||
return | ||
} | ||
output := map[string]interface{}{ | ||
"schema": schema.String(), | ||
} | ||
|
||
jsonBytes, err := json.MarshalIndent(output, "", " ") | ||
if err != nil { | ||
fmt.Println("Error marshalling to JSON:", err) | ||
return | ||
} | ||
fmt.Println(string(jsonBytes)) | ||
|
||
// jsonFilename := strings.TrimSuffix(avscFilename, ".avsc") + ".json" | ||
// err = os.WriteFile(jsonFilename, jsonBytes, 0644) //nolint:gosec | ||
// if err != nil { | ||
// fmt.Println("Error writing JSON file:", err) | ||
// return | ||
// } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# AVSC to JSON Converter | ||
This script is a simple utility written in Go that converts Avro schema files (.avsc) to JSON format. | ||
|
||
### Usage | ||
To use this script, you need to pass the .avsc file as a command-line argument: | ||
|
||
Replace <input.avsc> with the path to your Avro schema file. | ||
|
||
### How It Works | ||
The script reads the Avro schema file specified as a command-line argument. It then converts the schema to JSON format and writes the output to a new file with the same name as the input file but with a .json extension. | ||
|
||
For example, if you run go run main.go example.avsc, the script will create a new file named example.json with the JSON representation of the Avro schema. | ||
|
||
### Error Handling | ||
If the script encounters an error while reading the Avro schema file or converting it to JSON, it will print an error message and exit. | ||
|
||
### Contributing | ||
Contributions are welcome. Please submit a pull request if you have any improvements or bug fixes. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package main | ||
package tests | ||
|
||
import ( | ||
"bytes" | ||
|
Oops, something went wrong.