-
Notifications
You must be signed in to change notification settings - Fork 647
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
Showing
38 changed files
with
1,288 additions
and
115 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 |
---|---|---|
|
@@ -108,11 +108,6 @@ jobs: | |
run: | | ||
go mod download | ||
- name: Create Kafka topics for test | ||
run: | | ||
docker exec ${{ job.services.kafka.id }} kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic products | ||
docker exec ${{ job.services.kafka.id }} kafka-topics.sh --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic order-logs | ||
- name: Start Zipkin | ||
run: docker run -d -p 2005:9411 openzipkin/zipkin:latest | ||
|
||
|
@@ -157,7 +152,8 @@ jobs: | |
run: | | ||
export GOFR_ENV=test | ||
go test gofr.dev/pkg/... -tags migration -v -short -coverprofile packageWithMigration.cov -coverpkg=gofr.dev/pkg/... | ||
grep -v 'gofr.dev/pkg/gofr/migration' packageWithMigration.cov > profile.cov | ||
grep -v 'gofr.dev/pkg/gofr/migration' packageWithMigration.cov > packageWithoutMigration.cov | ||
grep -v 'google/mock_interfaces\.go' packageWithoutMigration.cov > profile.cov | ||
go tool cover -func profile.cov | ||
- name: Upload Test Coverage | ||
|
@@ -221,7 +217,6 @@ jobs: | |
awk '!/^mode: / && FNR==1{print "mode: set"} {print}' ./Example-Test-Report/profile.cov > merged_profile.cov | ||
tail -n +2 ./PKG-Coverage-Report/profile.cov >> merged_profile.cov | ||
- name: Upload | ||
uses: paambaati/[email protected] | ||
env: | ||
|
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,123 @@ | ||
# gRPC | ||
We have already seen how GoFr can help ease the development of HTTP servers, but there are | ||
cases where performance is primiraliy required sacrificing flexibility. In these types of | ||
scenarios gRPC protocol comes into picture. gRPC is an open-source RPC(Remote Procedure Call) | ||
framework initially developed by Google. [Learn more](https://grpc.io/docs/what-is-grpc/introduction/) | ||
|
||
## Prerequisites | ||
- Install the `protoc` protocol buffer compilation | ||
- Linux, using `apt` or `apt-get` | ||
```shell | ||
$ apt install -y protobuf-compiler | ||
$ protoc --version # Ensure compiler version is 3+ | ||
``` | ||
- MacOS, using homebrew | ||
```shell | ||
$ brew install protobuf | ||
$ protoc --version # Ensure compiler version is 3+ | ||
``` | ||
- Install **Go Puligins** for protocol compiler: | ||
1. Install prtocol compiler plugins for Go | ||
```shell | ||
$ go install google.golang.org/protobuf/cmd/[email protected] | ||
$ go install google.golang.org/grpc/cmd/[email protected] | ||
``` | ||
2. Update `PATH` for `protoc` compiler to find the plugins: | ||
```shell | ||
$ export PATH="$PATH:$(go env GOPATH)/bin" | ||
``` | ||
|
||
## Creating protocol buffers | ||
For a detailed guide, please take a look at the [Tutorial](https://grpc.io/docs/languages/go/basics/) at official gRPC docs. | ||
|
||
Fistly, we need to create a `customer.proto` file to define our service and the rpc methods that the service provides. | ||
```protobuf | ||
// Indicates the protocol buffer version that is being used | ||
syntax = "proto3"; | ||
// Indicates the go package where the generated file will be produced | ||
option go_package = ""; | ||
service CustomerService { | ||
// ... | ||
} | ||
``` | ||
Inside the service one can define all the `rpc` methods, specifying the request and responses types. | ||
```protobuf | ||
service CustomerService { | ||
// GetCustomer is a rpc method to get customer data using specific filters | ||
rpc GetCustomer(CustomerFilter) returns(CustomerData) {} | ||
} | ||
``` | ||
The `CustomerFilter` and `CustomerData` are two types of messages that will be exchanged between server | ||
and client. Users must define those for protocol buffer to serialize them when making a remote procedure call. | ||
```protobuf | ||
syntax = "proto3"; | ||
message CustomerFilter { | ||
int64 id = 1; | ||
string name = 2; | ||
// other fields that can be passed | ||
} | ||
message CustomerData { | ||
int64 id = 1; | ||
string name = 2; | ||
string address = 3; | ||
// other customer releated fields | ||
} | ||
``` | ||
|
||
Now run the following command to gegnerate go code using the Go gRPC plugins: | ||
```shell | ||
protoc \ | ||
--go_out=. \ | ||
--go_opt=paths=source_relative \ | ||
--go-grpc_out=. \ | ||
--go-grpc_opt=paths=source_relative \ | ||
customer.proto | ||
``` | ||
Above command will generate two files `customer.pb.go` and `customer_grpc.pb.go` and these contain necessary code to perform rpc calls. | ||
In `customer.pb.go` you can find `CustomerService` interface- | ||
```go | ||
// CustomerServiceServer is the server API for CustomerService service. | ||
type CustomerServiceServer interface { | ||
GetCustomer(context.Context, *CustomerFilter) (*CustomerData, error) | ||
} | ||
``` | ||
User needs to implement this interface to serve the content to the client calling the method. | ||
```go | ||
package customer | ||
import ( | ||
"context" | ||
) | ||
type Handler struct { | ||
// required fields to get the customer data | ||
} | ||
func (h *Handler) GetCustomer(ctx context.Context, filter *CustomerFilter) (*CustomerData, error) { | ||
// get the customer data and handler error | ||
return data, nil | ||
} | ||
``` | ||
|
||
Lastly to register the gRPC service to the gofr server, user can call the `RegisterCustomerServiceServer` in `customer_grpc.pb.go` | ||
to register the service giving gofr app and the Handler struct. | ||
```go | ||
package main | ||
import ( | ||
"gofr.dev/pkg/gofr" | ||
"gofr.dev/examples/grpc-server/customer" | ||
) | ||
func main() { | ||
app := gofr.New() | ||
customer.RegisterCustomerServiceServer(app, customer.Handler{}) | ||
app.Run() | ||
} | ||
``` | ||
>Note: By default, grpc server will run on port 9000, to customize the port users can set GRPC_PORT config in the .env |
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 |
---|---|---|
|
@@ -5,6 +5,7 @@ import ( | |
"fmt" | ||
|
||
"gofr.dev/examples/using-migrations/migrations" | ||
|
||
"gofr.dev/pkg/gofr" | ||
) | ||
|
||
|
20 changes: 20 additions & 0 deletions
20
examples/using-migrations/migrations/1708322090_create_topics_for_store.go
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,20 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
|
||
"gofr.dev/pkg/gofr/migration" | ||
) | ||
|
||
func createTopicsForStore() migration.Migrate { | ||
return migration.Migrate{ | ||
UP: func(d migration.Datasource) error { | ||
err := d.PubSub.CreateTopic(context.Background(), "products") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return d.PubSub.CreateTopic(context.Background(), "order-logs") | ||
}, | ||
} | ||
} |
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,23 @@ | ||
package migrations | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
||
"gofr.dev/pkg/gofr/migration" | ||
) | ||
|
||
func TestAll(t *testing.T) { | ||
// Get the map of migrations | ||
allMigrations := All() | ||
|
||
expected := map[int64]migration.Migrate{ | ||
1708322067: createTableEmployee(), | ||
1708322089: addEmployeeInRedis(), | ||
1708322090: createTopicsForStore(), | ||
} | ||
|
||
// Check if the length of the maps match | ||
assert.Equal(t, len(expected), len(allMigrations), "TestAll Failed!") | ||
} |
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
Oops, something went wrong.