Skip to content

berkayakcay/toy-go-grpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

toy-go-grpc

Getting Started

gRPC.io > GO > Quick start

brew install protoc-gen-go
brew install protoc-gen-go-grpc

gRPC vs REST

gRPC REST
Protocol Buffers JSON
HTTP 2 HTTP 1
Streaming Unary
Bi directional Client->Server
Free design GET/POST/UPDATE/DELETE/...

gRPC Unary

rpc Greet (GreetRequest) returns (GreetResponse);

gRPC Server Streaming

rpc GreetManyTimes (GreetRequest) returns (stream GreetResponse);

gRPC Client Streaming

rpc LongGreet (stream GreetRequest) returns (GreetResponse);

gRPC Bi-Directional Streaming

rpc GreetEveryone (stream GreetRequest) returns (stream GreetResponse);

gRPC SSL Options

Generators are in this folder: SSL

NewClientTLSFromFile

// .......

	tls := true // change that to false if needed
	opts := []grpc.DialOption{}

	if tls {
		certFile := "ssl/ca.crt"
		creds, err := credentials.NewClientTLSFromFile(certFile, "localhost")

		if err != nil {
			log.Fatalf("Failed loading certificated: %v\n", err)
		}

		opts = append(opts, grpc.WithTransportCredentials(creds))
	}
	conn, err := grpc.Dial(addr, opts...)

// .......

NewServerTLSFromFile

// .......

	opts := []grpc.ServerOption{}
	tls := true // change that to false if needed

	if tls {
		certFile := "ssl/server.crt"
		keyFile := "ssl/server.pem"
		creds, err := credentials.NewServerTLSFromFile(certFile, keyFile)

		if err != nil {
			log.Fatalf("Failed loading certificated: %v\n", err)
		}

		opts = append(opts, grpc.Creds(creds))
	}

	log.Printf("Listening on %s\n", addr)

	s := grpc.NewServer(opts...)

// .......

Notes

Makefile (Windows)

Install Chocolatey (https://chocolatey.org/install)

choco install make

You should then be able to use make command in the directory that contains the Makefile.

This is optional, since you can still build the .proto files by hand by running the following commands:

protoc -I${PROJECT}/proto --go_opt=module=${YOUR_MODULE} --go_out=. ${PROJECT}/proto/*.proto where ${YOUR_MODULE} is the name of your go module (you can find that in your go.mod file) and ${PROJECT} is one of the projects name (greet, calculator, blog).

gRPC force to secure connection

https://grpc.io/docs/guides/auth/

Failed to connect: grpc: no transport security set (use grpc.WithTransportCredentials(insecure.NewCredentials()) explicitly or set credentials) 

vscode-proto3 extension configuration

Go into settings > Extensions > vscode-proto3 configuration and then click Edit in settings.json. (you can just edit .vscode/settings.json too.)

After that, give --proto_path options like below codes.

{
    "protoc": {
        "options": [
            "--proto_path=<path of your proto files>"
        ]
    }
}

gRPC evans client

installation

  • Manually gRPC API inspection
  • To automate some tasks by scripting
// .......

	s := grpc.NewServer()
	pb.RegisterCalculatorServiceServer(s, &Server{})
	reflection.Register(s)

// .......

evans --host localhost --post 50051 --reflection

Resources

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published