Tit Petric's Blog, page 3

December 6, 2019

Bash: Embedding files into Go

There’s always that chance that your Go app/service will need to bundle some files in order to fulfill some requirement. A common case of that is bundling SQL migrations into an app. SQL migrations are a set of SQL queries that need to run to set up a clean database, or to change database schema over the course of the applications lifetime.

Planning database migrations

Database migrations can be a tricky thing. With common systems, people tend to write database migrations that go both...

 •  0 comments  •  flag
Share on Twitter
Published on December 06, 2019 09:30

December 5, 2019

Bash: Poor mans code generation

When it comes to generating code, I often find that the simplest code generation only needs variable replacement. There’s a cli utility that already facilitates this with environment variables, named envsubst, part of the gettext suite of tools.

In it’s most simple form, it takes a template file and replaces any variables contained within, and outputs the resulting text. It’s not super great if you have any dangling $ characters in your template, but for most purposes...

 •  0 comments  •  flag
Share on Twitter
Published on December 05, 2019 07:30

December 4, 2019

Make: Dynamic Makefile targets

A common pattern which we want to support when building our services is to build everything under rpc/{service}/{service}.proto as well as cmd/* and other locations which may be manually written or generated with various tools.

Building multiple targets with Go

Go does actually support building the whole cmd/ folder in one command:

go build -o ./build ./cmd/...

This functionality was added in Go just recently, in the 1.13.4 tag.

It will not create the target folder itself, but will build all...

 •  0 comments  •  flag
Share on Twitter
Published on December 04, 2019 08:00

December 3, 2019

Go: Introduction to Protobuf: gRPC

We implemented a Twitch RPC generator in the previous chapter, but we do have some hard core gRPC fans that requested that I should check out what gRPC produces. I am interested into comparing these two as well, so let’s start by scaffolding our gRPC handlers. Our main goal is to compare the implementation details of gRPC over Twitch RPC in more concrete terms.

We need to modify the Makefile slightly, in order to include the grpc codegen plugin, by changing a protoc option:

Under the ...

 •  0 comments  •  flag
Share on Twitter
Published on December 03, 2019 07:00

Go: Introduction to Protobuf: GRPC

We implemented a Twitch RPC generator in the previous chapter, but we do have some hard core GRPC fans that requested that I should check out what GRPC produces. I am interested into comparing these two as well, so let’s start by scaffolding our GRPC handlers. Our main goal is to compare the implementation details of GRPC over Twitch RPC in more concrete terms.

We need to modify the Makefile slightly, in order to include the grpc codegen plugin, by changing a protoc option:

Under the ...

 •  0 comments  •  flag
Share on Twitter
Published on December 03, 2019 07:00

December 2, 2019

Go: Introduction to Protobuf: Services

The next step (or possibly the first step) about implementing a microservice, is defining it’s API endpoints. What people usually do is write http handlers and resort to a routing framework like go-chi/chi. But, protocol buffers can define your service, as RPC calls definitions are built into the protobuf schema.

Protobuf service definitions

Protobuf files may define a service object, which has definitions for one or many rpc calls. Let’s extend our proto definitions for ...

 •  0 comments  •  flag
Share on Twitter
Published on December 02, 2019 12:00

December 1, 2019

Go: Introduction to Protobuf: Messages

In it’s most basic ways, protocol buffers or protobufs are an approach to serialize structured data with minimal overhead. They require that the data structures be known and compatible between the client and server, unlike JSON where the structure itself is part of the encoding.

Protobufs and Go

The most basic protobuf message definition would look something like this:

message ListThreadRequest { // session info string sessionID = 1; // pagination uint32 pageNumber = 2; uint32 pageSize...
 •  0 comments  •  flag
Share on Twitter
Published on December 01, 2019 07:00

April 15, 2019

Next level Go testing

Go makes it very easy to write tests. In fact, the test tool is built in the standard toolchain, and you can just run go test to run your tests, without installing extra dependencies or anything else. The testing package is part of the standard library, and it’s use, I’m happy to observe, is pretty wide spread.

As you’re writing your service implementations in Go, hopefully your test coverage is growing with time. With the larger scope of testing, tests run longer as well. Y...

 •  0 comments  •  flag
Share on Twitter
Published on April 15, 2019 11:00

February 4, 2019

A love song for Bash

There isn’t a day where I don’t see some kind of DevOps article that advocates some sort of higher order programming language for performing shell tasks, most recently one that uses Python to issue a POST request on a Discord Webhook API, when ever somebody logs into your server.

SSH Monitoring

The article, A SSH monitoring platform with Discord, makes this assumption that somehow issuing a HTTP API request with curl and bash is something that’s somehow worse than pulling se...

 •  0 comments  •  flag
Share on Twitter
Published on February 04, 2019 10:00

September 18, 2018

Error handling and where Go2 gets it wrong

This is an article which I’ve struggled with for the past few weeks. It started plainly enough trying to write about unit testing in go, but there was a lot of overlap with error handling and a significant portion of that was taking a hard look at the Go2 error handling draft. I’ve written and re-written this article many times, where it morphed from advocating simple assertions in tests to make unit testing easier, to actually having a detailed look at where Go2 error handling fa...

 •  0 comments  •  flag
Share on Twitter
Published on September 18, 2018 11:00