https://github.com/andriykalashnykov/rk-demo-proto
Middleware & bootstrapper for gRPC and grpc-gateway
https://github.com/andriykalashnykov/rk-demo-proto
go golang grpc grpc-gateway protobuf
Last synced: 8 months ago
JSON representation
Middleware & bootstrapper for gRPC and grpc-gateway
- Host: GitHub
- URL: https://github.com/andriykalashnykov/rk-demo-proto
- Owner: AndriyKalashnykov
- Created: 2023-10-10T14:09:37.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-02-08T04:55:48.000Z (8 months ago)
- Last Synced: 2025-02-24T06:49:53.594Z (8 months ago)
- Topics: go, golang, grpc, grpc-gateway, protobuf
- Language: Go
- Homepage:
- Size: 212 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Example
Middleware & bootstrapper for [gRPC](https://grpc.io/docs/languages/go/) and [grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway).## Documentation
- [Github](https://github.com/rookie-ninja/rk-grpc)
- [Official Docs](https://docs.rkdev.info)
- [GRPC](https://grpc.io/docs/languages/go/quickstart/)## Installation
- rk-boot: Bootstrapper base
- rk-grpc: Bootstrapper for [gRPC](https://grpc.io/docs/languages/go/) & [grpc-gateway](https://github.com/grpc-ecosystem/grpc-gateway)
- [buf CLI](https://buf.build/docs/installation)```shell
go get github.com/rookie-ninja/rk-boot/v2
go get github.com/rookie-ninja/rk-grpc/v2go install google.golang.org/protobuf/cmd/protoc-gen-go
go install github.com/grpc-ecosystem/grpc-gateway/v2/protoc-gen-openapiv2
export PATH="$PATH:$(go env GOPATH)/bin"
```## Quick start
### 1.Prepare .proto files
- api/v1/greeter.proto```protobuf
syntax = "proto3";package api.v1;
option go_package = "api/v1/hello";
service Greeter {
rpc Hello (HelloRequest) returns (HelloResponse) {}
}message HelloRequest {}
message HelloResponse {
string Message = 1;
}
```- api/v1/gw_mapping.yaml
```yaml
type: google.api.Service
config_version: 3# Please refer google.api.Http in third-party/googleapis/google/api/http.proto file for details.
http:
rules:
- selector: api.v1.Greeter.Hello
get: /v1/hello
```- buf.yaml
```yaml
version: v1beta1
name: github.com/rk-dev/rk-boot
build:
roots:
- api
- third-party/googleapis
```- buf.gen.yaml
```yaml
version: v1beta1
plugins:
- name: go
out: api/gen
opt:
- paths=source_relative
- name: go-grpc
out: api/gen
opt:
- paths=source_relative
- require_unimplemented_servers=false
- name: grpc-gateway
out: api/gen
opt:
- paths=source_relative
- grpc_api_configuration=api/v1/gw_mapping.yaml
- allow_repeated_fields_in_body=true
- generate_unbound_methods=true
- name: openapiv2
out: api/gen
opt:
- grpc_api_configuration=api/v1/gw_mapping.yaml
- allow_repeated_fields_in_body=true
```### 2.Generate .pb.go files with [buf](https://docs.buf.build/introduction)
```
$ buf generate --path api/v1
```### 4.Create boot.yaml
Important note: rk-boot will bind grpc and grpc-gateway in the same port which we think is a convenient way.As a result, grpc-gateway will automatically be started.
```yaml
---
grpc:
- name: rk-demo
port: 8080
enabled: true
commonService:
enabled: true
sw:
enabled: true
docs:
enabled: true
prom:
enabled: true
middleware:
logging:
enabled: true
prom:
enabled: true
```### 5.Create main.go
```go
// Copyright (c) 2021 rookie-ninja
//
// Use of this source code is governed by an Apache-style
// license that can be found in the LICENSE file.package main
import (
"context"
"github.com/rookie-ninja/rk-boot/v2"
"github.com/rookie-ninja/rk-demo/api/gen/v1"
"github.com/rookie-ninja/rk-grpc/v2/boot"
"google.golang.org/grpc"
)func main() {
boot := rkboot.NewBoot()// register grpc
entry := rkgrpc.GetGrpcEntry("rk-demo")
entry.AddRegFuncGrpc(registerGreeter)
entry.AddRegFuncGw(greeter.RegisterGreeterHandlerFromEndpoint)// Bootstrap
boot.Bootstrap(context.TODO())// Wait for shutdown sig
boot.WaitForShutdownSig(context.TODO())
}func registerGreeter(server *grpc.Server) {
greeter.RegisterGreeterServer(server, &GreeterServer{})
}//GreeterServer GreeterServer struct
type GreeterServer struct{}// Hello response with hello message
func (server *GreeterServer) Hello(_ context.Context, _ *greeter.HelloRequest) (*greeter.HelloResponse, error) {
return &greeter.HelloResponse{
Message: "hello!",
}, nil
}
```### 6.Start server
```go
$ go run main.go
```### 7.Validation
- Call API:```shell script
$ curl -X GET localhost:8080/v1/hello
{"Message":"hello!"}$ curl -X GET localhost:8080/rk/v1/ready
{
"ready": true
}$ curl -X GET localhost:8080/rk/v1/alive
{
"alive": true
}
```- Swagger UI: [http://localhost:8080/sw](http://localhost:8080/sw)
- Docs UI via: [http://localhost:8080/docs](http://localhost:8080/docs)
- Prometheus client: [http://localhost:8080/metrics](http://localhost:8080/metrics)