Community Hangouts

29th March 2018

Dara Hayes

dara.hayes@redhat.com

@darahayess

@darahayes

I think Node is not the best system to build a massive web server. I would use Go for that. And honestly, that’s the reason why I left Node. It was the realization that: oh, actually, this is not the best server-side system ever.

Ryan Dahl - Creator of Node.js

HTTP Frameworks/Libraries

The built in net/http library is really good

But routing is hard

Gorilla Mux is our friend

net/http + gorilla/mux = $$$

Dependency Management

also known as 'vendoring'

Dependency Management (vendoring)

dep is the official experiment, but not yet the official tool

Deps are defined in Gopkg.toml and Gopkg.lock

$ dep init

$ dep ensure -add github.com/user/library

Project Structure

.
├── Gopkg.lock
├── Gopkg.toml
├── README.md
├── cmd               # Entry Points or executables in here
│   └── my-app
│       └── my-app.go
├── pkg               # App modules/business logic stuff in here
│   ├── business
│   ├── config
│   ├── db
│   └── web
└── vendor            # Dependencies are in here

Dependency Injection & Interfaces

HTTP Handlers

Business Logic

Database

App Configuration

Use environment variables and fallback to sensible defaults

The app should start (in local dev) with zero config

Testing

Testing Framework is built in!

Excellent HTTP Testing capabilities

Dependency Injection allows us to easily test components in isolation

Test files are normally kept in the same directory

Good Practice to separate integration tests and unit tests

Code Coverage

Code Coverage reporting built in!

Not easy to aggregate coverage across multiple packages

(example shown later)

go test -cover -coverprofile=coverage.out

Upload to coveralls.io as part of CI using goveralls

Build & Test Commands

Use Make for common build and test commands

$ make test
$ make test_integration
$ make build
$ make build_linux
$ make docker_build
$ make docker_release
  ...

Docker

FROM centos
ARG BINARY=./go-hello-server
EXPOSE 4000

COPY ${BINARY} /opt/go-hello-server
CMD "/opt/go-hello-server"

Build the binary first, then pass into the image

The CI environment is guaranteed to be clean.

Why?

ONE super simple, low maintenance Dockerfile

Simplest developer workflow

Works in dev and in prod

Easiest way to build any version of the project, anytime.

Aim for simplicity, only complicate things if you really need to

Keeps custom build tooling to a minimum

Continuous Integration & Release
poorly documented black magic rituals
unnecessary amounts of setup & coordination
Controversial Opinion

If your release / deploy process can't be described in a few short bullet points, you've got problems.

Let's make it ridiculously easy

CI/CD Recipe for Success

Use the concept of jobs

- build
- test
- push
- deploy

Define sequences of jobs and execute things based on conditions.

Small tasks with one purpose

smaller                    ==     Lower probability of failure

single purpose       ==     Modular and reusable

Always look for off the shelf tooling first
Pro Tip
Release process should have one entrypoint
less parameters == less room for error
git tags are fantastic
demo
end 🍕

Aerogear Community Hangout 29 March 2018

By Dara Hayes

Aerogear Community Hangout 29 March 2018

This presentation is the Golang Hello World Server on Steroids. In addition to 'Hello World' This presentation demonstrates approaches for unit tests, parsing and sending JSON, handling environment variables, using interfaces and dependency injection, Building Docker Images and Setting up automated releases in a CI/CD environment.

  • 441

More from Dara Hayes