A demo project for understanding Go kit
- Install PostgreSQL
- Create a database called "roster"
$ createdb roster
- Install Go, following instructions at https://golang.org/doc/install or, for Mac, you can just use homebrew:
$ brew install go
$ brew install protobuf
$ # Add the below line to your appropriate "rc" file for your shell: (bashrc, zshrc, .fishrc, etc)
$ export GOPATH=$HOME/go; export PATH=$PATH:$GOPATH/bin
Note: Older installation guides indicate setting more environment variables than necessary. The page at https://github.com/golang/go/wiki/SettingGOPATH contains current information on Go's environment variables.
- Follow instructions at https://grpc.io/docs/quickstart/go.html to install gRPC and Protocol Buffers 3. Note that, for the Protocol Buffers step, if you use homebrew on a Mac you can use:
$ brew install protobuf
- Get the code
$ go get -u github.com/hoop33/roster
$ cd $GOPATH/src/github.com/hoop33/roster
$ make deps
$ dep ensure -update
$ make
- Ensure postgress is working correctly.
$ psql #logs in as super user
\connect roster
psql=# CREATE USER <db user> WITH SUPERUSER PASSWORD <'password'>;
psql=#\q
- Run the app, which will create the
players
table
$ ROSTER_USER=<db user> ROSTER_PASSWORD=<db password> ./roster
- Follow instructions to install https://github.com/hoop33/jags
$ jags | sed 's/,,/,N\/A,/g' | sed 's/,R,/,0,/g' > players.csv
- Run the following SQL:
COPY players(name,number,position,height,weight,age,experience,college)
FROM '<path to file>/players.csv' DELIMITER ',' CSV HEADER;
If you see an error around grpc
libraries, e.g., :
package google.golang.org/grpc/grpclb/grpc_lb_v1/messages: cannot find package "google.golang.org/grpc/grpclb/grpc_lb_v1/messages" in any of:
/usr/local/Cellar/go/1.10.3/libexec/src/google.golang.org/grpc/grpclb/grpc_lb_v1/messages (from $GOROOT)
/Users/<user>/go/src/google.golang.org/grpc/grpclb/grpc_lb_v1/messages (from $GOPATH)
You may have a mismatch between the grpc
libraries in your vendored files and the ones that protoc
is picking up. To get them in sync, try getting the latest for both:
$ go get -u google.golang.org/grpc
$ cd $GOPATH/src/github.com/hoop33/roster
$ dep ensure -update google.golang.org/grpc
See a discussion at grpc/grpc-go#581
For each step, check out the tag, build the app, and run:
$ git checkout <tag>
$ make && ./roster
Nothing Go kit-related here; just a simple Go command-line service around a database table.
We use Go kit's built in logger here, but we could have used logrus or any other logger.
This is where things get a little interesting, Go kit-wise. An endpoint is a Go kit function that takes a request and returns a response, callable from a Go kit transport.
And now we have a ReST service around our database table, powered by Go kit.
Nothing new integrated into our application in this step. We define our protocol buffers.
Now we can access our data over gRPC.
The make file has various targets. To quickly build, run:
$ make quick
To run all the linters and tests, run:
$ make
To run a test coverage report, run:
$ make coverage
The accompanying presentation can be found at presentation/microservices_with_gokit.md
and is designed to be viewed with Deckset.
- Thanks to Michael Dimmitt https://github.com/michaeldimmitt for troubleshooting the installation steps
- Go kit https://gokit.io/
- sqlx http://jmoiron.github.io/sqlx/
- Testify https://github.com/stretchr/testify
- go-sqlmock https://github.com/DATA-DOG/go-sqlmock
- gRPC https://grpc.io/
Apologies for any I've missed.
Copyright © 2018 Rob Warner
Licensed under the MIT License