Skip to content

Commit

Permalink
2024 ver
Browse files Browse the repository at this point in the history
  • Loading branch information
cuongnd9 committed Mar 13, 2024
1 parent 67d78a9 commit 860a7d9
Show file tree
Hide file tree
Showing 12 changed files with 81 additions and 110 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Cuong Tran
Copyright (c) 2020 Cuong Nguyen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
.PHONY: mysql gen-proto run-server run-client

mysql:
docker run -it --name database -p 3306:3306 -e MYSQL_ROOT_PASSWORD=cuongnguyenpo -e MYSQL_DATABASE=cuongnguyenpo mysql:latest

gen-proto:
protoc --proto_path=proto \
--go_out=pkg/pb --go_opt=paths=source_relative \
--go-grpc_out=pkg/pb --go-grpc_opt=paths=source_relative \
proto/*.proto

run-server:
go run cmd/server.go

run-client:
go run cmd/client.go
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,29 @@

Go 🤝 gRPC

## Preparation
### 1. Local Machine

```shell script
brew install protobuf
protoc --version
go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2
```

#### .zshrc or.bashrc

```shell script
export PATH="$PATH:$(go env GOPATH)/bin"
```

## How to use?
### 2. Project dependencies

```shell script
go mod tidy
go mod vendor
```

### Start MySQL
### 3. MySQL

```shell script
docker run -it --name database -p 3306:3306 -e MYSQL_ROOT_PASSWORD=cuongnguyenpo -e MYSQL_DATABASE=cuongnguyenpo mysql:latest
Expand All @@ -33,7 +43,7 @@ CREATE TABLE `todo` (
);
```

### Generate Go's protobuf code
### 4. Generate Go's protobuf code

```shell script
protoc --proto_path=proto \
Expand All @@ -42,18 +52,18 @@ protoc --proto_path=proto \
proto/*.proto
```

### gRPC server
### 5. Run gRPC server

```shell script
cd cmd/server && go run main.go
```

### gRPC client
### 6. Run gRPC client

```shell script
cd cmd/client && go run main.go
```

## license
### license

MIT © [Cuong Nguyen](https://github.com/cuongnd9/) 2024
23 changes: 10 additions & 13 deletions cmd/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package main

import (
"context"
"github.com/golang/protobuf/ptypes"
pb "github.com/cuongnd9/go-grpc/pkg/pb"
"google.golang.org/grpc"
"log"
"syreclabs.com/go/faker"
"time"
)

const apiVersion = "v1"
const apiVersion = "pb"

func main() {
conn, err := grpc.Dial(":50000", grpc.WithInsecure())
Expand All @@ -18,22 +18,20 @@ func main() {
}
defer conn.Close()

client := v1.NewToDoServiceClient(conn)
client := pb.NewToDoServiceClient(conn)

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

t := time.Now().In(time.UTC)
reminder, _ := ptypes.TimestampProto(t)
t.Format(time.RFC3339Nano)

// Create
req1 := v1.CreateRequest{
req1 := pb.CreateRequest{
Api: apiVersion,
ToDo: &v1.ToDo{
ToDo: &pb.ToDo{
Title: faker.Name().Title(),
Description: faker.Name().String(),
Reminder: reminder,
},
}
res1, err := client.Create(ctx, &req1)
Expand All @@ -45,7 +43,7 @@ func main() {
id := res1.Id

// Read
req2 := v1.ReadRequest{
req2 := pb.ReadRequest{
Api: apiVersion,
Id: id,
}
Expand All @@ -56,13 +54,12 @@ func main() {
log.Printf("Read result: <%+v>\n\n", res2)

// Update
req3 := v1.UpdateRequest{
req3 := pb.UpdateRequest{
Api: apiVersion,
ToDo: &v1.ToDo{
ToDo: &pb.ToDo{
Id: res2.ToDo.Id,
Title: faker.Name().Title(),
Description: faker.Name().String(),
Reminder: res2.ToDo.Reminder,
},
}
res3, err := client.Update(ctx, &req3)
Expand All @@ -72,7 +69,7 @@ func main() {
log.Printf("Update result: <%+v>\n\n", res3)

// ReadAll
req4 := v1.ReadAllRequest{
req4 := pb.ReadAllRequest{
Api: apiVersion,
}
res4, err := client.ReadAll(ctx, &req4)
Expand All @@ -82,7 +79,7 @@ func main() {
log.Printf("ReadAll result: <%+v>\n\n", res4)

// Delete
req5 := v1.DeleteRequest{
req5 := pb.DeleteRequest{
Api: apiVersion,
Id: id,
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package main

import (
"fmt"
"github.com/cuongnd9/go-grpc/pkg/cmd/server"
"github.com/cuongnd9/go-grpc/pkg"
"os"
)

func main() {
if err := cmd.RunServer(); err != nil {
if err := pkg.RunServer(); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/103cuong/go_grpc
module github.com/cuongnd9/go-grpc

go 1.14

Expand All @@ -7,5 +7,5 @@ require (
github.com/golang/protobuf v1.4.2
google.golang.org/grpc v1.27.0
google.golang.org/protobuf v1.25.0
syreclabs.com/go/faker v1.2.2 // indirect
syreclabs.com/go/faker v1.2.2
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZi
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
google.golang.org/grpc v1.27.0 h1:rRYRFMVgRv6E0D70Skyfsr28tDXIuuPZyWGMPdMcnXg=
google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8abTk=
google.golang.org/grpc v1.31.1 h1:SfXqXS5hkufcdZ/mHtYCh53P2b+92WQq/DZcKLgsFRs=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
google.golang.org/protobuf v1.20.1-0.20200309200217-e05f789c0967/go.mod h1:A+miEFZTKqfCUM6K7xSMQL9OKL/b6hQv+e19PK+JZNE=
google.golang.org/protobuf v1.21.0/go.mod h1:47Nbq4nVaFHyn7ilMalzfO3qCViNmqZ2kzikPIcrTAo=
google.golang.org/protobuf v1.22.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.23.0 h1:4MY060fB1DLGMB/7MBTLnwQUY6+F09GEiz6SsrNqyzM=
google.golang.org/protobuf v1.23.0/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.23.1-0.20200526195155-81db48ad09cc/go.mod h1:EGpADcykh3NcUnDUJcl1+ZksZNG86OlYog2l/sGQquU=
google.golang.org/protobuf v1.25.0 h1:Ejskq+SyPohKW+1uil0JJMtmHCgJPJ/qWTxr8qp+R4c=
Expand Down
13 changes: 9 additions & 4 deletions pkg/protocol/grpc.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
package grpc
package protocol

import (
"context"
v1 "github.com/cuongnd9/go-grpc/pkg/pb"
"database/sql"
pb "github.com/cuongnd9/go-grpc/pkg/pb"
"github.com/cuongnd9/go-grpc/pkg/service"
"google.golang.org/grpc"
"log"
"net"
"os"
"os/signal"
)

func RunServer(ctx context.Context, v1API v1.ToDoServiceServer, port string) error {
func RunGRPC(ctx context.Context, db *sql.DB, port string) error {
listen, err := net.Listen("tcp", ":"+port)
if err != nil {
return err
}

// register service
server := grpc.NewServer()
v1.RegisterToDoServiceServer(server, v1API)

todoService := service.NewToDoServiceServer(db)

pb.RegisterToDoServiceServer(server, todoService)

// graceful shutdown
c := make(chan os.Signal, 1)
Expand Down
2 changes: 1 addition & 1 deletion pkg/server.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package pkg

import (
"context"
Expand Down
Loading

0 comments on commit 860a7d9

Please sign in to comment.