go-grpc-pg icon indicating copy to clipboard operation
go-grpc-pg copied to clipboard

Simple service exposing a gRPC interface, with a connection to PostgreSQL on the backend

go-grpc-pg Circle CI GoDoc Go Report Card

A simple service demonstrating Go, gRPC, and PostgreSQL. Integration with CircleCI (version 1.0) included.

Building go-grpc-pg

Building go-httpd requires Go 1.8 or later. gvm is a great tool for installing and managing your versions of Go.

Download and build it like so:

mkdir go-grpc-pg # Or any directory of your choice
cd go-grpc-pg/
export GOPATH=$PWD
go get -t github.com/otoolep/go-grpc-pg/...
cd src/github.com/otoolep/go-grpc-pg
go install ./...

Some people consider using a distinct GOPATH environment variable for each project doing it wrong. In practise I, and many other Go programmers, find this actually most convenient.

Optional step to speed up testing

Unit testing actually uses SQLite, which is built as part of the test suite -- there is no need to install SQLite separately. However the compilation of SQLite is slow, and quickly becomes tedious if continually repeated. To avoid continual compilation every test run, execute the following command after performing the steps above:

go install github.com/mattn/go-sqlite3

Running go-grpc-pg

Once built as per above, launch the server as follows:

$GOPATH/bin/server

This assumes PostgreSQL is listening on localhost, port 5432. Run $GOPATH/bin/server -h to learn the full configuration the server expects of PostgreSQL, including the database, user, and password that must exist.

Generating queries

Assuming the server is up and running, execute the client as follows.

$GOPATH/bin/client

An example session is shown below.

>> CREATE TABLE bar (id INTEGER NOT NULL PRIMARY KEY, name TEXT)
Last Insert ID: -1, rows affected: 1
>> SELECT * FROM bar
>> INSERT INTO bar(id, name) VALUES(1, 'bob')
Last Insert ID: -1, rows affected: 1
>> INSERT INTO bar(id, name) VALUES(2, 'tom')
Last Insert ID: -1, rows affected: 1
>> SELECT * FROM bar
1       bob
2       tom
>>