initial commit
parent
2b002c9f54
commit
cd18efc5d6
|
@ -0,0 +1,36 @@
|
|||
BUF_VERSION:=v1.29.0
|
||||
|
||||
install-tools:
|
||||
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
|
||||
go install github.com/bufbuild/buf/cmd/buf@$(BUF_VERSION)
|
||||
|
||||
build: generate
|
||||
buf build
|
||||
go build -o bin/server .
|
||||
|
||||
generate:
|
||||
buf generate
|
||||
go generate ./...
|
||||
|
||||
lint:
|
||||
buf lint
|
||||
|
||||
fmt:
|
||||
buf format
|
||||
go fmt ./...
|
||||
|
||||
go.mod: FORCE
|
||||
go mod tidy
|
||||
go mod verify
|
||||
go.sum: go.mod
|
||||
|
||||
test:
|
||||
$(GOTEST) --format testname --junitfile unit-tests.xml -- -mod=readonly -race -coverprofile=./c.out -covermode=atomic -coverpkg=.,./... ./...
|
||||
|
||||
coverage: test
|
||||
$(GOTOOL) cover -func=cover.out
|
||||
|
||||
mocks:
|
||||
rm -rf mocks/*
|
||||
mockery --all
|
|
@ -0,0 +1,19 @@
|
|||
# grpc-api-example
|
||||
|
||||
This repository demonstrates how to use gRPC with Go and various plugins. It's a simple API to manage notes. It uses grpc-gateway to handle HTTP requests and openapi to define API specs.
|
||||
|
||||
## Installation
|
||||
|
||||
This project uses go1.20.
|
||||
|
||||
```
|
||||
$ make install-tools
|
||||
$ make generate
|
||||
```
|
||||
|
||||
### Run the server
|
||||
|
||||
```
|
||||
$ make build
|
||||
$ ./bin/server
|
||||
```
|
Binary file not shown.
|
@ -0,0 +1,13 @@
|
|||
version: v1
|
||||
managed:
|
||||
enabled: true
|
||||
go_package_prefix:
|
||||
default: github.com/sundowndev/grpc-api-example/proto
|
||||
- buf.build/googleapis/googleapis
|
||||
plugins:
|
||||
- plugin: buf.build/protocolbuffers/go:v1.30.0
|
||||
out: proto
|
||||
opt: paths=source_relative
|
||||
- plugin: buf.build/grpc/go:v1.3.0
|
||||
out: proto
|
||||
opt: paths=source_relative,require_unimplemented_servers=false
|
|
@ -0,0 +1,3 @@
|
|||
version: v1
|
||||
directories:
|
||||
- proto
|
|
@ -0,0 +1,21 @@
|
|||
module github.com/sundowndev/grpc-api-example
|
||||
|
||||
go 1.20
|
||||
|
||||
require (
|
||||
github.com/gofrs/uuid v4.4.0+incompatible
|
||||
github.com/stretchr/testify v1.8.4
|
||||
google.golang.org/grpc v1.62.0
|
||||
google.golang.org/protobuf v1.32.0
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/davecgh/go-spew v1.1.1 // indirect
|
||||
github.com/golang/protobuf v1.5.3 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
golang.org/x/net v0.20.0 // indirect
|
||||
golang.org/x/sys v0.16.0 // indirect
|
||||
golang.org/x/text v0.14.0 // indirect
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
)
|
|
@ -0,0 +1,32 @@
|
|||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gofrs/uuid v4.4.0+incompatible h1:3qXRTX8/NbyulANqlc0lchS1gqAVxRgsuW1YrTJupqA=
|
||||
github.com/gofrs/uuid v4.4.0+incompatible/go.mod h1:b2aQJv3Z4Fp6yNu3cdSllBxTCLRxnplIgP/c0N/04lM=
|
||||
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
|
||||
github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
|
||||
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
|
||||
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
|
||||
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
|
||||
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
|
||||
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80 h1:AjyfHzEPEFp/NpvfN5g+KDla3EMojjhRVZc1i7cj+oM=
|
||||
google.golang.org/genproto/googleapis/rpc v0.0.0-20240123012728-ef4313101c80/go.mod h1:PAREbraiVEVGVdTZsVWjSbbTtSyGbAgIIvni8a8CD5s=
|
||||
google.golang.org/grpc v1.62.0 h1:HQKZ/fa1bXkX1oFOvSjmZEUL8wLSaZTjCcLAlmZRtdk=
|
||||
google.golang.org/grpc v1.62.0/go.mod h1:IWTG0VlJLCh1SkC58F7np9ka9mx/WNkjl4PGJaiq+QE=
|
||||
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
|
||||
google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc=
|
||||
google.golang.org/protobuf v1.32.0 h1:pPC6BG5ex8PDFnkbrGU3EixyhKcQ2aDuBS36lqK/C7I=
|
||||
google.golang.org/protobuf v1.32.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
|
@ -0,0 +1,42 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/sundowndev/grpc-api-example/server"
|
||||
"google.golang.org/grpc/grpclog"
|
||||
"io"
|
||||
"net"
|
||||
"os"
|
||||
"os/signal"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
func main() {
|
||||
// Adds gRPC internal logs. This is quite verbose, so adjust as desired!
|
||||
log := grpclog.NewLoggerV2(os.Stdout, io.Discard, io.Discard)
|
||||
grpclog.SetLoggerV2(log)
|
||||
|
||||
addr := "0.0.0.0:10000"
|
||||
srv := server.NewServer()
|
||||
|
||||
// Serve gRPC Server
|
||||
log.Info("Serving gRPC on https://", addr)
|
||||
go func() {
|
||||
if err := srv.Listen(addr); err != nil && !errors.Is(err, net.ErrClosed) {
|
||||
log.Fatalf("listen: %s\n", err)
|
||||
}
|
||||
}()
|
||||
|
||||
// Wait for interrupt signal to gracefully shut down the server with
|
||||
// a timeout of 5 seconds.
|
||||
quit := make(chan os.Signal)
|
||||
// kill (no param) default send syscanll.SIGTERM
|
||||
// kill -2 is syscall.SIGINT
|
||||
// kill -9 is syscall. SIGKILL but can't be caught, so don't need to add it
|
||||
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
|
||||
<-quit
|
||||
|
||||
if err := srv.Close(); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
|
@ -0,0 +1,7 @@
|
|||
version: v1
|
||||
name: buf.build/sundowndev/grpc-api-example
|
||||
deps:
|
||||
- buf.build/googleapis/googleapis
|
||||
lint:
|
||||
use:
|
||||
- DEFAULT
|
|
@ -0,0 +1,429 @@
|
|||
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||
// versions:
|
||||
// protoc-gen-go v1.30.0
|
||||
// protoc (unknown)
|
||||
// source: notes/v1/notes.proto
|
||||
|
||||
package notesv1
|
||||
|
||||
import (
|
||||
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
|
||||
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
|
||||
reflect "reflect"
|
||||
sync "sync"
|
||||
)
|
||||
|
||||
const (
|
||||
// Verify that this generated code is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
|
||||
// Verify that runtime/protoimpl is sufficiently up-to-date.
|
||||
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
|
||||
)
|
||||
|
||||
type ListNotesRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
}
|
||||
|
||||
func (x *ListNotesRequest) Reset() {
|
||||
*x = ListNotesRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_notes_v1_notes_proto_msgTypes[0]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *ListNotesRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*ListNotesRequest) ProtoMessage() {}
|
||||
|
||||
func (x *ListNotesRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_notes_v1_notes_proto_msgTypes[0]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use ListNotesRequest.ProtoReflect.Descriptor instead.
|
||||
func (*ListNotesRequest) Descriptor() ([]byte, []int) {
|
||||
return file_notes_v1_notes_proto_rawDescGZIP(), []int{0}
|
||||
}
|
||||
|
||||
type ListNotesResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Note *Note `protobuf:"bytes,1,opt,name=note,proto3" json:"note,omitempty"`
|
||||
}
|
||||
|
||||
func (x *ListNotesResponse) Reset() {
|
||||
*x = ListNotesResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_notes_v1_notes_proto_msgTypes[1]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *ListNotesResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*ListNotesResponse) ProtoMessage() {}
|
||||
|
||||
func (x *ListNotesResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_notes_v1_notes_proto_msgTypes[1]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use ListNotesResponse.ProtoReflect.Descriptor instead.
|
||||
func (*ListNotesResponse) Descriptor() ([]byte, []int) {
|
||||
return file_notes_v1_notes_proto_rawDescGZIP(), []int{1}
|
||||
}
|
||||
|
||||
func (x *ListNotesResponse) GetNote() *Note {
|
||||
if x != nil {
|
||||
return x.Note
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type AddNoteRequest struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Title string `protobuf:"bytes,1,opt,name=title,proto3" json:"title,omitempty"`
|
||||
}
|
||||
|
||||
func (x *AddNoteRequest) Reset() {
|
||||
*x = AddNoteRequest{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_notes_v1_notes_proto_msgTypes[2]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *AddNoteRequest) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*AddNoteRequest) ProtoMessage() {}
|
||||
|
||||
func (x *AddNoteRequest) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_notes_v1_notes_proto_msgTypes[2]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use AddNoteRequest.ProtoReflect.Descriptor instead.
|
||||
func (*AddNoteRequest) Descriptor() ([]byte, []int) {
|
||||
return file_notes_v1_notes_proto_rawDescGZIP(), []int{2}
|
||||
}
|
||||
|
||||
func (x *AddNoteRequest) GetTitle() string {
|
||||
if x != nil {
|
||||
return x.Title
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
type AddNoteResponse struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Note *Note `protobuf:"bytes,1,opt,name=note,proto3" json:"note,omitempty"`
|
||||
}
|
||||
|
||||
func (x *AddNoteResponse) Reset() {
|
||||
*x = AddNoteResponse{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_notes_v1_notes_proto_msgTypes[3]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *AddNoteResponse) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*AddNoteResponse) ProtoMessage() {}
|
||||
|
||||
func (x *AddNoteResponse) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_notes_v1_notes_proto_msgTypes[3]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use AddNoteResponse.ProtoReflect.Descriptor instead.
|
||||
func (*AddNoteResponse) Descriptor() ([]byte, []int) {
|
||||
return file_notes_v1_notes_proto_rawDescGZIP(), []int{3}
|
||||
}
|
||||
|
||||
func (x *AddNoteResponse) GetNote() *Note {
|
||||
if x != nil {
|
||||
return x.Note
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type Note struct {
|
||||
state protoimpl.MessageState
|
||||
sizeCache protoimpl.SizeCache
|
||||
unknownFields protoimpl.UnknownFields
|
||||
|
||||
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
|
||||
Title string `protobuf:"bytes,2,opt,name=title,proto3" json:"title,omitempty"`
|
||||
Archived bool `protobuf:"varint,3,opt,name=archived,proto3" json:"archived,omitempty"`
|
||||
}
|
||||
|
||||
func (x *Note) Reset() {
|
||||
*x = Note{}
|
||||
if protoimpl.UnsafeEnabled {
|
||||
mi := &file_notes_v1_notes_proto_msgTypes[4]
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
}
|
||||
|
||||
func (x *Note) String() string {
|
||||
return protoimpl.X.MessageStringOf(x)
|
||||
}
|
||||
|
||||
func (*Note) ProtoMessage() {}
|
||||
|
||||
func (x *Note) ProtoReflect() protoreflect.Message {
|
||||
mi := &file_notes_v1_notes_proto_msgTypes[4]
|
||||
if protoimpl.UnsafeEnabled && x != nil {
|
||||
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
|
||||
if ms.LoadMessageInfo() == nil {
|
||||
ms.StoreMessageInfo(mi)
|
||||
}
|
||||
return ms
|
||||
}
|
||||
return mi.MessageOf(x)
|
||||
}
|
||||
|
||||
// Deprecated: Use Note.ProtoReflect.Descriptor instead.
|
||||
func (*Note) Descriptor() ([]byte, []int) {
|
||||
return file_notes_v1_notes_proto_rawDescGZIP(), []int{4}
|
||||
}
|
||||
|
||||
func (x *Note) GetId() string {
|
||||
if x != nil {
|
||||
return x.Id
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *Note) GetTitle() string {
|
||||
if x != nil {
|
||||
return x.Title
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (x *Note) GetArchived() bool {
|
||||
if x != nil {
|
||||
return x.Archived
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var File_notes_v1_notes_proto protoreflect.FileDescriptor
|
||||
|
||||
var file_notes_v1_notes_proto_rawDesc = []byte{
|
||||
0x0a, 0x14, 0x6e, 0x6f, 0x74, 0x65, 0x73, 0x2f, 0x76, 0x31, 0x2f, 0x6e, 0x6f, 0x74, 0x65, 0x73,
|
||||
0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x12, 0x08, 0x6e, 0x6f, 0x74, 0x65, 0x73, 0x2e, 0x76, 0x31,
|
||||
0x22, 0x12, 0x0a, 0x10, 0x4c, 0x69, 0x73, 0x74, 0x4e, 0x6f, 0x74, 0x65, 0x73, 0x52, 0x65, 0x71,
|
||||
0x75, 0x65, 0x73, 0x74, 0x22, 0x37, 0x0a, 0x11, 0x4c, 0x69, 0x73, 0x74, 0x4e, 0x6f, 0x74, 0x65,
|
||||
0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x22, 0x0a, 0x04, 0x6e, 0x6f, 0x74,
|
||||
0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x6e, 0x6f, 0x74, 0x65, 0x73, 0x2e,
|
||||
0x76, 0x31, 0x2e, 0x4e, 0x6f, 0x74, 0x65, 0x52, 0x04, 0x6e, 0x6f, 0x74, 0x65, 0x22, 0x26, 0x0a,
|
||||
0x0e, 0x41, 0x64, 0x64, 0x4e, 0x6f, 0x74, 0x65, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12,
|
||||
0x14, 0x0a, 0x05, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x05,
|
||||
0x74, 0x69, 0x74, 0x6c, 0x65, 0x22, 0x35, 0x0a, 0x0f, 0x41, 0x64, 0x64, 0x4e, 0x6f, 0x74, 0x65,
|
||||
0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x22, 0x0a, 0x04, 0x6e, 0x6f, 0x74, 0x65,
|
||||
0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x0e, 0x2e, 0x6e, 0x6f, 0x74, 0x65, 0x73, 0x2e, 0x76,
|
||||
0x31, 0x2e, 0x4e, 0x6f, 0x74, 0x65, 0x52, 0x04, 0x6e, 0x6f, 0x74, 0x65, 0x22, 0x48, 0x0a, 0x04,
|
||||
0x4e, 0x6f, 0x74, 0x65, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09,
|
||||
0x52, 0x02, 0x69, 0x64, 0x12, 0x14, 0x0a, 0x05, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x18, 0x02, 0x20,
|
||||
0x01, 0x28, 0x09, 0x52, 0x05, 0x74, 0x69, 0x74, 0x6c, 0x65, 0x12, 0x1a, 0x0a, 0x08, 0x61, 0x72,
|
||||
0x63, 0x68, 0x69, 0x76, 0x65, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x08, 0x52, 0x08, 0x61, 0x72,
|
||||
0x63, 0x68, 0x69, 0x76, 0x65, 0x64, 0x32, 0x9a, 0x01, 0x0a, 0x0c, 0x4e, 0x6f, 0x74, 0x65, 0x73,
|
||||
0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x48, 0x0a, 0x09, 0x4c, 0x69, 0x73, 0x74, 0x4e,
|
||||
0x6f, 0x74, 0x65, 0x73, 0x12, 0x1a, 0x2e, 0x6e, 0x6f, 0x74, 0x65, 0x73, 0x2e, 0x76, 0x31, 0x2e,
|
||||
0x4c, 0x69, 0x73, 0x74, 0x4e, 0x6f, 0x74, 0x65, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74,
|
||||
0x1a, 0x1b, 0x2e, 0x6e, 0x6f, 0x74, 0x65, 0x73, 0x2e, 0x76, 0x31, 0x2e, 0x4c, 0x69, 0x73, 0x74,
|
||||
0x4e, 0x6f, 0x74, 0x65, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x22, 0x00, 0x30,
|
||||
0x01, 0x12, 0x40, 0x0a, 0x07, 0x41, 0x64, 0x64, 0x4e, 0x6f, 0x74, 0x65, 0x12, 0x18, 0x2e, 0x6e,
|
||||
0x6f, 0x74, 0x65, 0x73, 0x2e, 0x76, 0x31, 0x2e, 0x41, 0x64, 0x64, 0x4e, 0x6f, 0x74, 0x65, 0x52,
|
||||
0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x19, 0x2e, 0x6e, 0x6f, 0x74, 0x65, 0x73, 0x2e, 0x76,
|
||||
0x31, 0x2e, 0x41, 0x64, 0x64, 0x4e, 0x6f, 0x74, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73,
|
||||
0x65, 0x22, 0x00, 0x42, 0xbc, 0x01, 0x0a, 0x0c, 0x63, 0x6f, 0x6d, 0x2e, 0x6e, 0x6f, 0x74, 0x65,
|
||||
0x73, 0x2e, 0x76, 0x31, 0x42, 0x0a, 0x4e, 0x6f, 0x74, 0x65, 0x73, 0x50, 0x72, 0x6f, 0x74, 0x6f,
|
||||
0x50, 0x01, 0x5a, 0x5f, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x73,
|
||||
0x75, 0x6e, 0x64, 0x6f, 0x77, 0x6e, 0x64, 0x65, 0x76, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x2d, 0x61,
|
||||
0x70, 0x69, 0x2d, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f,
|
||||
0x20, 0x2d, 0x20, 0x62, 0x75, 0x66, 0x2e, 0x62, 0x75, 0x69, 0x6c, 0x64, 0x2f, 0x67, 0x6f, 0x6f,
|
||||
0x67, 0x6c, 0x65, 0x61, 0x70, 0x69, 0x73, 0x2f, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x61, 0x70,
|
||||
0x69, 0x73, 0x2f, 0x6e, 0x6f, 0x74, 0x65, 0x73, 0x2f, 0x76, 0x31, 0x3b, 0x6e, 0x6f, 0x74, 0x65,
|
||||
0x73, 0x76, 0x31, 0xa2, 0x02, 0x03, 0x4e, 0x58, 0x58, 0xaa, 0x02, 0x08, 0x4e, 0x6f, 0x74, 0x65,
|
||||
0x73, 0x2e, 0x56, 0x31, 0xca, 0x02, 0x08, 0x4e, 0x6f, 0x74, 0x65, 0x73, 0x5c, 0x56, 0x31, 0xe2,
|
||||
0x02, 0x14, 0x4e, 0x6f, 0x74, 0x65, 0x73, 0x5c, 0x56, 0x31, 0x5c, 0x47, 0x50, 0x42, 0x4d, 0x65,
|
||||
0x74, 0x61, 0x64, 0x61, 0x74, 0x61, 0xea, 0x02, 0x09, 0x4e, 0x6f, 0x74, 0x65, 0x73, 0x3a, 0x3a,
|
||||
0x56, 0x31, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
|
||||
}
|
||||
|
||||
var (
|
||||
file_notes_v1_notes_proto_rawDescOnce sync.Once
|
||||
file_notes_v1_notes_proto_rawDescData = file_notes_v1_notes_proto_rawDesc
|
||||
)
|
||||
|
||||
func file_notes_v1_notes_proto_rawDescGZIP() []byte {
|
||||
file_notes_v1_notes_proto_rawDescOnce.Do(func() {
|
||||
file_notes_v1_notes_proto_rawDescData = protoimpl.X.CompressGZIP(file_notes_v1_notes_proto_rawDescData)
|
||||
})
|
||||
return file_notes_v1_notes_proto_rawDescData
|
||||
}
|
||||
|
||||
var file_notes_v1_notes_proto_msgTypes = make([]protoimpl.MessageInfo, 5)
|
||||
var file_notes_v1_notes_proto_goTypes = []interface{}{
|
||||
(*ListNotesRequest)(nil), // 0: notes.v1.ListNotesRequest
|
||||
(*ListNotesResponse)(nil), // 1: notes.v1.ListNotesResponse
|
||||
(*AddNoteRequest)(nil), // 2: notes.v1.AddNoteRequest
|
||||
(*AddNoteResponse)(nil), // 3: notes.v1.AddNoteResponse
|
||||
(*Note)(nil), // 4: notes.v1.Note
|
||||
}
|
||||
var file_notes_v1_notes_proto_depIdxs = []int32{
|
||||
4, // 0: notes.v1.ListNotesResponse.note:type_name -> notes.v1.Note
|
||||
4, // 1: notes.v1.AddNoteResponse.note:type_name -> notes.v1.Note
|
||||
0, // 2: notes.v1.NotesService.ListNotes:input_type -> notes.v1.ListNotesRequest
|
||||
2, // 3: notes.v1.NotesService.AddNote:input_type -> notes.v1.AddNoteRequest
|
||||
1, // 4: notes.v1.NotesService.ListNotes:output_type -> notes.v1.ListNotesResponse
|
||||
3, // 5: notes.v1.NotesService.AddNote:output_type -> notes.v1.AddNoteResponse
|
||||
4, // [4:6] is the sub-list for method output_type
|
||||
2, // [2:4] is the sub-list for method input_type
|
||||
2, // [2:2] is the sub-list for extension type_name
|
||||
2, // [2:2] is the sub-list for extension extendee
|
||||
0, // [0:2] is the sub-list for field type_name
|
||||
}
|
||||
|
||||
func init() { file_notes_v1_notes_proto_init() }
|
||||
func file_notes_v1_notes_proto_init() {
|
||||
if File_notes_v1_notes_proto != nil {
|
||||
return
|
||||
}
|
||||
if !protoimpl.UnsafeEnabled {
|
||||
file_notes_v1_notes_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*ListNotesRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_notes_v1_notes_proto_msgTypes[1].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*ListNotesResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_notes_v1_notes_proto_msgTypes[2].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*AddNoteRequest); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_notes_v1_notes_proto_msgTypes[3].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*AddNoteResponse); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
file_notes_v1_notes_proto_msgTypes[4].Exporter = func(v interface{}, i int) interface{} {
|
||||
switch v := v.(*Note); i {
|
||||
case 0:
|
||||
return &v.state
|
||||
case 1:
|
||||
return &v.sizeCache
|
||||
case 2:
|
||||
return &v.unknownFields
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
type x struct{}
|
||||
out := protoimpl.TypeBuilder{
|
||||
File: protoimpl.DescBuilder{
|
||||
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
|
||||
RawDescriptor: file_notes_v1_notes_proto_rawDesc,
|
||||
NumEnums: 0,
|
||||
NumMessages: 5,
|
||||
NumExtensions: 0,
|
||||
NumServices: 1,
|
||||
},
|
||||
GoTypes: file_notes_v1_notes_proto_goTypes,
|
||||
DependencyIndexes: file_notes_v1_notes_proto_depIdxs,
|
||||
MessageInfos: file_notes_v1_notes_proto_msgTypes,
|
||||
}.Build()
|
||||
File_notes_v1_notes_proto = out.File
|
||||
file_notes_v1_notes_proto_rawDesc = nil
|
||||
file_notes_v1_notes_proto_goTypes = nil
|
||||
file_notes_v1_notes_proto_depIdxs = nil
|
||||
}
|
|
@ -0,0 +1,31 @@
|
|||
syntax = "proto3";
|
||||
|
||||
package notes.v1;
|
||||
|
||||
option go_package = "github.com/sundowndev/grpc-api-example/proto/notes/v1";
|
||||
|
||||
service NotesService {
|
||||
rpc ListNotes(ListNotesRequest) returns (stream ListNotesResponse) {};
|
||||
rpc AddNote(AddNoteRequest) returns (AddNoteResponse) {};
|
||||
// rpc ArchiveNote(ArchiveNoteRequest) returns (ArchiveNoteResponse) {};
|
||||
}
|
||||
|
||||
message ListNotesRequest {}
|
||||
|
||||
message ListNotesResponse {
|
||||
Note note = 1;
|
||||
}
|
||||
|
||||
message AddNoteRequest {
|
||||
string title = 1;
|
||||
}
|
||||
|
||||
message AddNoteResponse {
|
||||
Note note = 1;
|
||||
}
|
||||
|
||||
message Note {
|
||||
string id = 1;
|
||||
string title = 2;
|
||||
bool archived = 3;
|
||||
}
|
|
@ -0,0 +1,172 @@
|
|||
// Code generated by protoc-gen-go-grpc. DO NOT EDIT.
|
||||
// versions:
|
||||
// - protoc-gen-go-grpc v1.3.0
|
||||
// - protoc (unknown)
|
||||
// source: notes/v1/notes.proto
|
||||
|
||||
package notesv1
|
||||
|
||||
import (
|
||||
context "context"
|
||||
grpc "google.golang.org/grpc"
|
||||
codes "google.golang.org/grpc/codes"
|
||||
status "google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
// This is a compile-time assertion to ensure that this generated file
|
||||
// is compatible with the grpc package it is being compiled against.
|
||||
// Requires gRPC-Go v1.32.0 or later.
|
||||
const _ = grpc.SupportPackageIsVersion7
|
||||
|
||||
const (
|
||||
NotesService_ListNotes_FullMethodName = "/notes.v1.NotesService/ListNotes"
|
||||
NotesService_AddNote_FullMethodName = "/notes.v1.NotesService/AddNote"
|
||||
)
|
||||
|
||||
// NotesServiceClient is the client API for NotesService service.
|
||||
//
|
||||
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://pkg.go.dev/google.golang.org/grpc/?tab=doc#ClientConn.NewStream.
|
||||
type NotesServiceClient interface {
|
||||
ListNotes(ctx context.Context, in *ListNotesRequest, opts ...grpc.CallOption) (NotesService_ListNotesClient, error)
|
||||
AddNote(ctx context.Context, in *AddNoteRequest, opts ...grpc.CallOption) (*AddNoteResponse, error)
|
||||
}
|
||||
|
||||
type notesServiceClient struct {
|
||||
cc grpc.ClientConnInterface
|
||||
}
|
||||
|
||||
func NewNotesServiceClient(cc grpc.ClientConnInterface) NotesServiceClient {
|
||||
return ¬esServiceClient{cc}
|
||||
}
|
||||
|
||||
func (c *notesServiceClient) ListNotes(ctx context.Context, in *ListNotesRequest, opts ...grpc.CallOption) (NotesService_ListNotesClient, error) {
|
||||
stream, err := c.cc.NewStream(ctx, &NotesService_ServiceDesc.Streams[0], NotesService_ListNotes_FullMethodName, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
x := ¬esServiceListNotesClient{stream}
|
||||
if err := x.ClientStream.SendMsg(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := x.ClientStream.CloseSend(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return x, nil
|
||||
}
|
||||
|
||||
type NotesService_ListNotesClient interface {
|
||||
Recv() (*ListNotesResponse, error)
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
type notesServiceListNotesClient struct {
|
||||
grpc.ClientStream
|
||||
}
|
||||
|
||||
func (x *notesServiceListNotesClient) Recv() (*ListNotesResponse, error) {
|
||||
m := new(ListNotesResponse)
|
||||
if err := x.ClientStream.RecvMsg(m); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return m, nil
|
||||
}
|
||||
|
||||
func (c *notesServiceClient) AddNote(ctx context.Context, in *AddNoteRequest, opts ...grpc.CallOption) (*AddNoteResponse, error) {
|
||||
out := new(AddNoteResponse)
|
||||
err := c.cc.Invoke(ctx, NotesService_AddNote_FullMethodName, in, out, opts...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// NotesServiceServer is the server API for NotesService service.
|
||||
// All implementations should embed UnimplementedNotesServiceServer
|
||||
// for forward compatibility
|
||||
type NotesServiceServer interface {
|
||||
ListNotes(*ListNotesRequest, NotesService_ListNotesServer) error
|
||||
AddNote(context.Context, *AddNoteRequest) (*AddNoteResponse, error)
|
||||
}
|
||||
|
||||
// UnimplementedNotesServiceServer should be embedded to have forward compatible implementations.
|
||||
type UnimplementedNotesServiceServer struct {
|
||||
}
|
||||
|
||||
func (UnimplementedNotesServiceServer) ListNotes(*ListNotesRequest, NotesService_ListNotesServer) error {
|
||||
return status.Errorf(codes.Unimplemented, "method ListNotes not implemented")
|
||||
}
|
||||
func (UnimplementedNotesServiceServer) AddNote(context.Context, *AddNoteRequest) (*AddNoteResponse, error) {
|
||||
return nil, status.Errorf(codes.Unimplemented, "method AddNote not implemented")
|
||||
}
|
||||
|
||||
// UnsafeNotesServiceServer may be embedded to opt out of forward compatibility for this service.
|
||||
// Use of this interface is not recommended, as added methods to NotesServiceServer will
|
||||
// result in compilation errors.
|
||||
type UnsafeNotesServiceServer interface {
|
||||
mustEmbedUnimplementedNotesServiceServer()
|
||||
}
|
||||
|
||||
func RegisterNotesServiceServer(s grpc.ServiceRegistrar, srv NotesServiceServer) {
|
||||
s.RegisterService(&NotesService_ServiceDesc, srv)
|
||||
}
|
||||
|
||||
func _NotesService_ListNotes_Handler(srv interface{}, stream grpc.ServerStream) error {
|
||||
m := new(ListNotesRequest)
|
||||
if err := stream.RecvMsg(m); err != nil {
|
||||
return err
|
||||
}
|
||||
return srv.(NotesServiceServer).ListNotes(m, ¬esServiceListNotesServer{stream})
|
||||
}
|
||||
|
||||
type NotesService_ListNotesServer interface {
|
||||
Send(*ListNotesResponse) error
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
type notesServiceListNotesServer struct {
|
||||
grpc.ServerStream
|
||||
}
|
||||
|
||||
func (x *notesServiceListNotesServer) Send(m *ListNotesResponse) error {
|
||||
return x.ServerStream.SendMsg(m)
|
||||
}
|
||||
|
||||
func _NotesService_AddNote_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
|
||||
in := new(AddNoteRequest)
|
||||
if err := dec(in); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if interceptor == nil {
|
||||
return srv.(NotesServiceServer).AddNote(ctx, in)
|
||||
}
|
||||
info := &grpc.UnaryServerInfo{
|
||||
Server: srv,
|
||||
FullMethod: NotesService_AddNote_FullMethodName,
|
||||
}
|
||||
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
|
||||
return srv.(NotesServiceServer).AddNote(ctx, req.(*AddNoteRequest))
|
||||
}
|
||||
return interceptor(ctx, in, info, handler)
|
||||
}
|
||||
|
||||
// NotesService_ServiceDesc is the grpc.ServiceDesc for NotesService service.
|
||||
// It's only intended for direct use with grpc.RegisterService,
|
||||
// and not to be introspected or modified (even as a copy)
|
||||
var NotesService_ServiceDesc = grpc.ServiceDesc{
|
||||
ServiceName: "notes.v1.NotesService",
|
||||
HandlerType: (*NotesServiceServer)(nil),
|
||||
Methods: []grpc.MethodDesc{
|
||||
{
|
||||
MethodName: "AddNote",
|
||||
Handler: _NotesService_AddNote_Handler,
|
||||
},
|
||||
},
|
||||
Streams: []grpc.StreamDesc{
|
||||
{
|
||||
StreamName: "ListNotes",
|
||||
Handler: _NotesService_ListNotes_Handler,
|
||||
ServerStreams: true,
|
||||
},
|
||||
},
|
||||
Metadata: "notes/v1/notes.proto",
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/gofrs/uuid"
|
||||
notesv1 "github.com/sundowndev/grpc-api-example/proto/notes/v1"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type NotesService struct {
|
||||
notesv1.UnimplementedNotesServiceServer
|
||||
mu *sync.RWMutex
|
||||
notes []*notesv1.Note
|
||||
}
|
||||
|
||||
func NewNotesService() *NotesService {
|
||||
return &NotesService{}
|
||||
}
|
||||
|
||||
func (s *NotesService) ListNotes(_ *notesv1.ListNotesRequest, srv notesv1.NotesService_ListNotesServer) error {
|
||||
s.mu.RLock()
|
||||
defer s.mu.RUnlock()
|
||||
|
||||
for _, note := range s.notes {
|
||||
err := srv.Send(¬esv1.ListNotesResponse{Note: note})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *NotesService) AddNote(_ context.Context, req *notesv1.AddNoteRequest) (*notesv1.AddNoteResponse, error) {
|
||||
n := ¬esv1.Note{
|
||||
Id: uuid.Must(uuid.NewV4()).String(),
|
||||
Title: req.Title,
|
||||
Archived: false,
|
||||
}
|
||||
|
||||
s.notes = append(s.notes, n)
|
||||
|
||||
return ¬esv1.AddNoteResponse{
|
||||
Note: n,
|
||||
}, nil
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/stretchr/testify/assert"
|
||||
notesv1 "github.com/sundowndev/grpc-api-example/proto/notes/v1"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNotesService_ListNotes(t *testing.T) {
|
||||
addr := "0.0.0.0:10000"
|
||||
srv := NewServer()
|
||||
go srv.Listen(addr)
|
||||
defer srv.Close()
|
||||
|
||||
conn, err := grpc.DialContext(context.Background(), addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
client := notesv1.NewNotesServiceClient(conn)
|
||||
res, err := client.ListNotes(context.TODO(), ¬esv1.ListNotesRequest{})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.NotNil(t, res)
|
||||
|
||||
//err = res.RecvMsg()
|
||||
//if err != nil && !errors.Is(err, io.EOF) {
|
||||
// t.Fatal(err)
|
||||
//}
|
||||
}
|
||||
|
||||
func TestNotesService_AddNote(t *testing.T) {
|
||||
addr := "0.0.0.0:10000"
|
||||
srv := NewServer()
|
||||
go srv.Listen(addr)
|
||||
defer srv.Close()
|
||||
|
||||
conn, err := grpc.Dial(addr, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
client := notesv1.NewNotesServiceClient(conn)
|
||||
res, err := client.AddNote(context.TODO(), ¬esv1.AddNoteRequest{Title: "test note"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
assert.NotNil(t, res)
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
notesv1 "github.com/sundowndev/grpc-api-example/proto/notes/v1"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/credentials/insecure"
|
||||
"net"
|
||||
)
|
||||
|
||||
type Server struct {
|
||||
listener net.Listener
|
||||
srv *grpc.Server
|
||||
}
|
||||
|
||||
func NewServer() *Server {
|
||||
s := grpc.NewServer(
|
||||
// TODO: Replace with your own certificate!
|
||||
grpc.Creds(insecure.NewCredentials()),
|
||||
)
|
||||
notesv1.RegisterNotesServiceServer(s, NewNotesService())
|
||||
|
||||
srv := &Server{
|
||||
srv: s,
|
||||
}
|
||||
|
||||
return srv
|
||||
}
|
||||
|
||||
func (s *Server) Listen(addr string) error {
|
||||
lis, err := net.Listen("tcp", addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to listen: %e", err)
|
||||
}
|
||||
s.listener = lis
|
||||
return s.srv.Serve(s.listener)
|
||||
}
|
||||
|
||||
func (s *Server) Close() error {
|
||||
s.srv.GracefulStop()
|
||||
if err := s.listener.Close(); !errors.Is(err, net.ErrClosed) {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
Loading…
Reference in New Issue