Skip to content

Commit

Permalink
feat: added protobuff
Browse files Browse the repository at this point in the history
  • Loading branch information
igh9410 committed Jul 14, 2024
1 parent ef18c22 commit a6b4bfc
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ shutdown:
@echo "Shutting down the Kubernetes cluster..."

@kubectl scale --replicas=0 deployment blabber-hive
@kubectl scale --replicas=0 statefulset broker
# @kubectl scale --replicas=0 statefulset broker
@kubectl scale --replicas=0 deployment fastapi
@kubectl scale --replicas=0 deployment grafana
@kubectl scale --replicas=0 deployment nginx
@kubectl scale --replicas=0 statefulset postgres
@kubectl scale --replicas=0 deployment prometheus
@kubectl scale --replicas=0 deployment redis
@kubectl scale --replicas=0 deployment zookeeper
# @kubectl scale --replicas=0 deployment zookeeper

# @echo "Shutting down the stateful infrastructures..."
# @docker compose -f ./docker-compose-k8s.yml down
Expand Down
24 changes: 23 additions & 1 deletion backend/Makefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
include .env
export

PROTO_PATH=./proto
GOOGLEAPIS_PATH=./proto/third_party/googleapis

# Makefile`
.PHONY: all run docker-push docker-run linter create-migration goose-version migrate-up migrate-down test

all: generate-docs generate-server generate-client
# Run the application
run:
@echo "Running the application..."
Expand Down Expand Up @@ -47,3 +51,21 @@ migrate-down:
test:
@echo "Running the tests..."
@go test ./... -v -cover -coverprofile=coverage.out

# Generate OpenAPI documentation from proto files
generate-docs:
@echo "Generating OpenAPI documentation..."
@protoc --proto_path=${PROTO_PATH} --proto_path=${GOOGLEAPIS_PATH} --openapi_out=api --openapi_opt=enum_type=string ${PROTO_PATH}/*.proto
# Generate server code from OpenAPI specification
generate-server:
@echo "Generating server code from OpenAPI specification..."
@oapi-codegen --generate=gin-server,strict-server,embedded-spec --package=api -o internal/api/server.gen.go docs/openapi.yaml

# Generate client code from OpenAPI specification
generate-client:
@echo "Generating client code from OpenAPI specification..."
@oapi-codegen --generate=client -o internal/client/client.gen.go --package=client docs/openapi.yaml




80 changes: 80 additions & 0 deletions backend/api/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Generated with protoc-gen-openapi
# https://github.com/google/gnostic/tree/master/cmd/protoc-gen-openapi

openapi: 3.0.3
info:
title: ChatService API
version: 0.0.1
paths:
/api/chats:
post:
tags:
- ChatService
operationId: ChatService_CreateChatRoom
requestBody:
content:
application/json:
schema:
$ref: '#/components/schemas/CreateChatRoomRequest'
required: true
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/CreateChatRoomResponse'
default:
description: Default error response
content:
application/json:
schema:
$ref: '#/components/schemas/Status'
components:
schemas:
ChatRoom:
type: object
properties:
id:
type: string
name:
type: string
createdAt:
type: string
format: date-time
CreateChatRoomRequest:
type: object
properties:
name:
type: string
CreateChatRoomResponse:
type: object
properties:
chatRoom:
$ref: '#/components/schemas/ChatRoom'
GoogleProtobufAny:
type: object
properties:
'@type':
type: string
description: The type of the serialized message.
additionalProperties: true
description: Contains an arbitrary serialized message along with a @type that describes the type of the serialized message.
Status:
type: object
properties:
code:
type: integer
description: The status code, which should be an enum value of [google.rpc.Code][google.rpc.Code].
format: int32
message:
type: string
description: A developer-facing error message, which should be in English. Any user-facing error message should be localized and sent in the [google.rpc.Status.details][google.rpc.Status.details] field, or localized by the client.
details:
type: array
items:
$ref: '#/components/schemas/GoogleProtobufAny'
description: A list of messages that carry the error details. There is a common set of message types for APIs to use.
description: 'The `Status` type defines a logical error model that is suitable for different programming environments, including REST APIs and RPC APIs. It is used by [gRPC](https://github.com/grpc). Each `Status` message contains three pieces of data: error code, error message, and error details. You can find out more about this error model and how to work with it in the [API Design Guide](https://cloud.google.com/apis/design/errors).'
tags:
- name: ChatService
18 changes: 5 additions & 13 deletions backend/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ services:
environment:
KAFKA_BROKER_ID: 1
KAFKA_ZOOKEEPER_CONNECT: "zookeeper:2181"
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT,PLAINTEXT_INTERNAL:PLAINTEXT
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092,PLAINTEXT_INTERNAL://0.0.0.0:29092
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://broker:9092,PLAINTEXT_INTERNAL://broker:29092
KAFKA_LISTENER_SECURITY_PROTOCOL_MAP: PLAINTEXT:PLAINTEXT
KAFKA_LISTENERS: PLAINTEXT://0.0.0.0:9092
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092
KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR: 1
KAFKA_TRANSACTION_STATE_LOG_MIN_ISR: 1
KAFKA_TRANSACTION_STATE_LOG_REPLICATION_FACTOR: 1
Expand All @@ -60,6 +60,8 @@ services:
environment:
POSTGRES_USERNAME: ${POSTGRES_USERNAME}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD}
volumes:
- ./postgres-data:/var/lib/postgresql/data

redis: # Redis Container
image: redis:7.2.3-alpine3.18
Expand All @@ -68,16 +70,6 @@ services:
- "6379:6379"
networks:
- blabber-hive
# kafka-setup:
# image: confluentinc/cp-kafka:latest
# container_name: blabber-hive-kafka-setup
# depends_on:
# - broker
# volumes:
# - ./create-kafka-topics.sh:/tmp/create-kafka-topics.sh
# command: "/tmp/create-kafka-topics.sh"
# networks:
# - blabber-hive
prometheus:
image: prom/prometheus
container_name: blabber-hive-prometheus
Expand Down
1 change: 1 addition & 0 deletions backend/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/googleapis/googleapis v0.0.0-20240712205114-b3f4e52d6c3b // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.0 // indirect
github.com/jackc/pgio v1.0.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions backend/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,8 @@ github.com/googleapis/gnostic v0.5.1/go.mod h1:6U4PtQXGIEt/Z3h5MAT7FNofLnw9vXk2c
github.com/googleapis/gnostic v0.5.5/go.mod h1:7+EbHbldMins07ALC74bsA81Ovc97DwqyJO1AENw9kA=
github.com/googleapis/go-type-adapters v1.0.0/go.mod h1:zHW75FOG2aur7gAO2B+MLby+cLsWGBF62rFAi7WjWO4=
github.com/googleapis/google-cloud-go-testing v0.0.0-20200911160855-bcd43fbb19e8/go.mod h1:dvDLG8qkwmyD9a/MJJN3XJcT3xFxOKAvTZGvuZmac9g=
github.com/googleapis/googleapis v0.0.0-20240712205114-b3f4e52d6c3b h1:tieOH6AGdrcSnR5NCS8HHFegJ3DdI6CXrp4WjHzvANw=
github.com/googleapis/googleapis v0.0.0-20240712205114-b3f4e52d6c3b/go.mod h1:XrPm4xpez/lHHyE+8/G+NqQRcB4lg42HF9zQVTvxtXw=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/handlers v0.0.0-20150720190736-60c7bfde3e33/go.mod h1:Qkdc/uu4tH4g6mTK6auzZ766c4CA0Ng8+o/OAirnOIQ=
github.com/gorilla/mux v1.7.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
Expand Down
51 changes: 51 additions & 0 deletions backend/infra/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package kafka

import (
"backend/internal/chat"
"context"
"encoding/json"
"fmt"

"log"
"os"
Expand Down Expand Up @@ -47,6 +49,16 @@ func NewKafkaClient() (*confluentKafka.AdminClient, error) {
return nil, err
}

// Check and create the topic if it does not exist
topicName := "messages"
numPartitions := 1
replicationFactor := 1

if err := CreateTopicIfNotExists(adminClient, topicName, numPartitions, replicationFactor); err != nil {
log.Printf("Failed to create topic if not exists: %s\n", err)
return nil, err
}

return adminClient, nil
}

Expand Down Expand Up @@ -141,3 +153,42 @@ func KafkaConsumer(batchProcessor *BatchProcessor) (*confluentKafka.Consumer, er

return consumer, nil
}

func CreateTopicIfNotExists(adminClient *confluentKafka.AdminClient, topic string, numPartitions int, replicationFactor int) error {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Check if the topic already exists
metadata, err := adminClient.GetMetadata(nil, false, 10000)
if err != nil {
return fmt.Errorf("failed to get metadata: %w", err)
}

for _, t := range metadata.Topics {
if t.Topic == topic {
log.Printf("Topic %s already exists", topic)
return nil
}
}

// Topic does not exist, create it
topicConfig := confluentKafka.TopicSpecification{
Topic: topic,
NumPartitions: numPartitions,
ReplicationFactor: replicationFactor,
}

results, err := adminClient.CreateTopics(ctx, []confluentKafka.TopicSpecification{topicConfig})
if err != nil {
return fmt.Errorf("failed to create topic: %w", err)
}

for _, result := range results {
if result.Error.Code() != confluentKafka.ErrNoError {
return fmt.Errorf("failed to create topic %s: %s", topic, result.Error.String())
}
}

log.Printf("Topic %s created successfully", topic)
return nil
}
38 changes: 38 additions & 0 deletions backend/internal/api/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions backend/proto/chat.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
syntax = "proto3";
package chat;

import "google/protobuf/timestamp.proto";
import "google/api/annotations.proto";

option go_package = "internal/chat";

message ChatRoom {
string id = 1;
string name = 2;
google.protobuf.Timestamp created_at = 3;
}

message CreateChatRoomRequest {
string name = 1;
}

message CreateChatRoomResponse {
ChatRoom chat_room = 1;
}

service ChatService {
rpc CreateChatRoom(CreateChatRoomRequest) returns (CreateChatRoomResponse) {
option (google.api.http) = {
post: "/api/chats"
body: "*"
};
}
}
1 change: 1 addition & 0 deletions backend/proto/third_party/googleapis
Submodule googleapis added at b3f4e5
2 changes: 0 additions & 2 deletions k8s/kafka/broker-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ spec:
value: "1"
- name: KAFKA_ZOOKEEPER_CONNECT
value: zookeeper:2181
- name: KAFKA_LOG4J_ROOT_LOGLEVEL
value: DEBUG
image: confluentinc/cp-kafka:7.4.3
name: blabber-hive-broker
ports:
Expand Down

0 comments on commit a6b4bfc

Please sign in to comment.