A simple library service (server and client).
- Service defined in Protocol Buffer v3 (proto3).
- Programming language The Go Programming Langauge (golang).
- RPC framework gRPC.
- Build tool Bazel.
The following instructions are tested on Ubuntu 16.04 (LTS).
-
Install Bazel
Bazel is our build tool. It is written in Java. Install Java and then Bazel.
$ sudo apt-get install bazel
-
Install Git
Git is our version control system. It is used by Bazel to download some of our external dependencies (like Protocol Buffers, gRPC, and Bazel's Go rules.
$ sudo apt-get install git
-
(Optional) Install Go
Go is a programming language and is very good at writting large-scale distributed systems. Our service and client are written in Go.
$ sudo apt-get install golang-go
-
(Optional) Install Gazelle
Gazelle is a helper tool that automatically generates Bazel BUILD files for Go and makes your life easier.
$ go install github.com/bazelbuild/bazel-gazelle/cmd/gazelle@latest
-
(Optional) Install Buildifier
Bazel Buildtools are helpers tools for Bazel. Buildifier cleans up and formats Bazel build files.
$ go install github.com/bazelbuild/buildtools/buildifier@latest
$ git clone [email protected]:ghasemloo/libraryservice.git
$ cd libraryservice
$ bazel build ...
$ bazel test ...
In one terminal run the server:
$ bazel run server/server -- --logtostderr
In another terminal run the client to send requests to the server:
$ bazel run client/client -- --logtostderr
$ go mod init github.com/ghasemloo/libraryservice
$ go mod tidy
Install Protobuf Compiler to generate Go packages for proto files:
$ sudo apt-get install protobuf-compiler
$ go install google.golang.org/protobuf/cmd/[email protected]
$ go install google.golang.org/grpc/cmd/[email protected]
$ protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative ./proto/api/api.proto
$ protoc --go_out=. --go_opt=paths=source_relative --go-grpc_out=. --go-grpc_opt=paths=source_relative ./proto/storage/storage.proto
$ go build ./...
$ go test ./...
In two different terminals run:
$ go run ./server/main.go
$ go run ./client/main.go
Updating WORKSPACE.bazel
from go.mod
:
$ gazelle update-repos --proto_import_prefix="github.com/ghasemloo/libraryservice" --from_file=./go.mod --prune
Creating/updating BUILD.bazel
files:
$ gazelle fix -go_prefix github.com/ghasemloo/libraryservice
You can also use Bazel (gazelle
commands and paramters are configured in the BUILD.bazel
):
$ bazel run //:gazelle-update-repos
$ bazel run //:gazelle
- Bazel: http://bazel.build
- Go: https://golang.org
- Google API Style Guide: https://cloud.google.com/apis/design/
- Proto3: https://developers.google.com/protocol-buffers/
- gRPC: https://www.grpc.io/
- gRPC Example: https://grpc.io/docs/languages/go/quickstart/
- Google RPC Status and Canonical Error Codes